使用java代码实现一个月内不再提醒,通用到期的问题

2022-07-25,,,,

其实就是最常见的到期问题。 例如帐号到期,会员到期等。

字段可以命名为:

expire_date 或 valid_date

场景

所在的家电公司要做个不再提醒功能。

其实就是有效期问题,开工。

过程

数据库设计

字段:

id
user_account 用户帐号
create_date 创建时间
update_date 更新时间
expire_date 过期时间

时间类型用设置么?例如一个月,一年。

其实不用,这个参数前端传即可,在逻辑里面转换为expire_date即可。

设置过期时间

推荐使用java8 date,非常好用,如下为一个月后为过期时间代码

localdatetime date = localdatetime.now(); // java8 当前时间
localdatetime onemonthlater = date.plusmonths(1); // 一个月之后的时间
date expiredate = date.from(onemonthlater.atzone(zoneid.systemdefault()).toinstant()); // localdatetime 转换为 date

判断逻辑

date是自带compareto方法,只需now和expire比较即可:

date expiredate = getexpiredate();
if(null==expiredate){ // 没有设置禁用期 那么不禁用
 return false;
}
int i = new date().compareto(expiredate);
if(i>0){ // 已经过了禁用期,不再禁用,disabletip=false
 return false;
}else{ // 还未过期,继续禁用 disabletip=true
 return true;
}

补充:java实现定时提醒功能

上班看股票不方便,做个股价监控软件

偷菜时间到了,做个定时提醒软件

还有10分20秒,要订票了,做个定时提醒软件

时间任意设置,总之就是一个定时提醒软件,比如设置5分钟时间到了,会弹出提示窗口,显示提示信息

我做这个软件,也是工作比较忙,又不能盯着时间看,所以就做了这个定时监控提醒软件,感觉用的还比较贴心

这里贴一点核心代码:

1 面板

public class window extends jframe {
  private jtextfield textfielda;
  private jtextfield textfieldb;
  private jtextfield textfieldc;
  private jtextarea resultarea;
  private jbutton caculatebtn;
  //listener
  private button1listener simplelistener;
 
  public window()
  {
   //gui部分
   setlayout(new borderlayout());//使用东南西北中布局
   
   textfielda=new jtextfield(5);
   textfieldb=new jtextfield(5);
   textfieldc=new jtextfield(5);
   resultarea=new jtextarea();//
   caculatebtn=new jbutton("监控");
   jpanel uppanel=new jpanel();//上面板
   uppanel.add(new jlabel("代码"));
   uppanel.add(textfielda);
   uppanel.add(new jlabel("下跌价格至"));
   uppanel.add(textfieldb);
   uppanel.add(new jlabel("上涨价格至"));
   uppanel.add(textfieldc);
   uppanel.add(caculatebtn);
   add(uppanel,borderlayout.north);//将上面板加到该窗口的上部分
   add(new jscrollpane(resultarea),borderlayout.center);//将结果的多行输出加入滚动面板,再把滚动面板加入该窗口的中部分
   setvisible(true);
   setdefaultcloseoperation(dispose_on_close);
   
   setbounds(100,100,460,260);
   //设置监听器
   simplelistener=new button1listener();
   simplelistener.setresultarea(resultarea);
   simplelistener.settextfielda(textfielda);
   simplelistener.settextfieldb(textfieldb);
   simplelistener.settextfieldc(textfieldc);
   
   //添加监听器
   caculatebtn.addactionlistener(simplelistener);
  }
 
}

2 设置

public void paintcomponent(graphics comp) {
  arraylist<string> arraylist = new arraylist<>();
  try {
 
   filereader fr = new filereader("c:\\users\\19391\\desktop\\java课程设计\\select.txt");//把这个地址换为你想要读入的文本文件地址
   bufferedreader bf = new bufferedreader(fr);
   string str;
   // 按行读取字符串
   while ((str = bf.readline()) != null) {
    arraylist.add(str);
   }
   bf.close();
   fr.close();
  }
 catch (ioexception e) {
   e.printstacktrace();
  }
  // 对arraylist中存储的字符串进行处理
  int length = arraylist.size();int n=length;
  string[] headlines = new string[length];
  for (int i = 0; i < length; i++) {
   headlines[i]= arraylist.get(i);
  }
  graphics2d comp2d = (graphics2d)comp;
  font type = new font("楷体", font.bold, 20);//字体对象
  gradientpaint gp=new gradientpaint(0,0,color.yellow,0,getsize().height,color.white,false);//背景颜色渐变(黄-->白)
  comp2d.setfont(type);//设置字体
  comp2d.setpaint(gp);
  gradientpaint gp2=new gradientpaint(0,0,color.blue,0,getsize().height,color.orange,false);//字体颜色渐变(橙-->蓝)
  comp2d.fillrect(0, 0, getsize().width, getsize().height);
  comp2d.setpaint(gp2);
  for (int i = 0; i < headlines.length; i++)//设置每一行字的位置
   comp2d.drawstring(headlines[i], 100, y + (20 * i));
 }

3 数据获取

public static string getcurrentprice() {
  string result = "";
  webresource webresource = client.resource("http://hq.sinajs.cn/list=sz"+code);
  webresource webresource1 = client.resource("http://hq.sinajs.cn/list=sh"+code);
  webresource webresource2 = client.resource("http://hq.sinajs.cn/list=hk"+code);  
  
  string res = webresource.accept(mediatype.application_atom_xml).get(string.class);//默认22个字节
  string res1 = webresource1.accept(mediatype.application_atom_xml).get(string.class);
  string res2 = webresource2.accept(mediatype.application_atom_xml).get(string.class);
  system.out.println(res.length()+"::"+res1.length()+"::"+res2.length() );
  if(res.length() > 24) {
   system.out.println("sz:"+res);
   result = res.split("=")[1];
   return result.split(",")[3];
  }else if(res1.length() > 24) {
   system.out.println("sh:"+res1);
   result = res1.split("=")[1];
   return result.split(",")[3];
  }else if(res2.length() > 24) {
   system.out.println("hk:"+res2);
   result = res2.split("=")[1];
   return result.split(",")[3];
  }else {
   system.out.println("输入代码异常,非sz/sh/hk");
   return "输入代码异常,非sz/sh/hk";
  }
 }

纯粹兴趣开发

打包成jar,然后转成exe,windows上直接双击就可以用

截图展示:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

《使用java代码实现一个月内不再提醒,通用到期的问题.doc》

下载本文的Word格式文档,以方便收藏与打印。