溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用java怎么實現(xiàn)一個到期提醒功能

發(fā)布時間:2021-01-22 14:31:27 來源:億速云 閱讀:6550 作者:Leah 欄目:開發(fā)技術

使用java怎么實現(xiàn)一個到期提醒功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

其實就是最常見的到期問題。 例如帳號到期,會員到期等。

字段可以命名為:

expire_date 或 valid_date

場景

所在的家電公司要做個不再提醒功能。

其實就是有效期問題,開工。

過程

數(shù)據(jù)庫設計

字段:

id
user_account 用戶帳號
create_date 創(chuàng)建時間
update_date 更新時間
expire_date 過期時間

時間類型用設置么?例如一個月,一年。

其實不用,這個參數(shù)前端傳即可,在邏輯里面轉換為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){ // 已經(jīng)過了禁用期,不再禁用,disableTip=false
 return false;
}else{ // 還未過期,繼續(xù)禁用 disableTip=true
 return true;
}

補充:java實現(xiàn)定時提醒功能

上班看股票不方便,做個股價監(jiān)控軟件

偷菜時間到了,做個定時提醒軟件

還有10分20秒,要訂票了,做個定時提醒軟件

時間任意設置,總之就是一個定時提醒軟件,比如設置5分鐘時間到了,會彈出提示窗口,顯示提示信息

我做這個軟件,也是工作比較忙,又不能盯著時間看,所以就做了這個定時監(jiān)控提醒軟件,感覺用的還比較貼心

這里貼一點核心代碼:

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("監(jiān)控");
   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);
   //設置監(jiān)聽器
   simpleListener=new Button1Listener();
   simpleListener.setResultArea(resultArea);
   simpleListener.setTextFieldA(textFieldA);
   simpleListener.setTextFieldB(textFieldB);
   simpleListener.setTextFieldC(textFieldC);
   
   //添加監(jiān)聽器
   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 數(shù)據(jù)獲取

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個字節(jié)
  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";
  }
 }

純粹興趣開發(fā)

打包成jar,然后轉成exe,windows上直接雙擊就可以用

截圖展示:

使用java怎么實現(xiàn)一個到期提醒功能

關于使用java怎么實現(xiàn)一個到期提醒功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI