使用java代碼實(shí)現(xiàn)一個(gè)月內(nèi)不再提醒,通用到期的問題
其實(shí)就是最常見的到期問題。 例如帳號到期,會員到期等。
字段可以命名為:
expire_date 或 valid_date
場景
所在的家電公司要做個(gè)不再提醒功能。
其實(shí)就是有效期問題,開工。
過程
數(shù)據(jù)庫設(shè)計(jì)
字段:
id user_account 用戶帳號 create_date 創(chuàng)建時(shí)間 update_date 更新時(shí)間 expire_date 過期時(shí)間
時(shí)間類型用設(shè)置么?例如一個(gè)月,一年。
其實(shí)不用,這個(gè)參數(shù)前端傳即可,在邏輯里面轉(zhuǎn)換為expire_date即可。
設(shè)置過期時(shí)間
推薦使用java8 date,非常好用,如下為一個(gè)月后為過期時(shí)間代碼:
LocalDateTime date = LocalDateTime.now(); // java8 當(dāng)前時(shí)間 LocalDateTime oneMonthLater = date.plusMonths(1); // 一個(gè)月之后的時(shí)間 Date expireDate = Date.from(oneMonthLater.atZone(ZoneId.systemDefault()).toInstant()); // LocalDateTime 轉(zhuǎn)換為 Date
判斷邏輯
date是自帶compareTo方法,只需now和expire比較即可:
Date expireDate = getExpireDate(); if(null==expireDate){ // 沒有設(shè)置禁用期 那么不禁用 return false; } int i = new Date().compareTo(expireDate); if(i>0){ // 已經(jīng)過了禁用期,不再禁用,disableTip=false return false; }else{ // 還未過期,繼續(xù)禁用 disableTip=true return true; }
補(bǔ)充:java實(shí)現(xiàn)定時(shí)提醒功能
上班看股票不方便,做個(gè)股價(jià)監(jiān)控軟件
偷菜時(shí)間到了,做個(gè)定時(shí)提醒軟件
還有10分20秒,要訂票了,做個(gè)定時(shí)提醒軟件
時(shí)間任意設(shè)置,總之就是一個(gè)定時(shí)提醒軟件,比如設(shè)置5分鐘時(shí)間到了,會彈出提示窗口,顯示提示信息
我做這個(gè)軟件,也是工作比較忙,又不能盯著時(shí)間看,所以就做了這個(gè)定時(shí)監(jiān)控提醒軟件,感覺用的還比較貼心
這里貼一點(diǎ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("下跌價(jià)格至")); upPanel.add(textFieldB); upPanel.add(new JLabel("上漲價(jià)格至")); upPanel.add(textFieldC); upPanel.add(caculateBtn); add(upPanel,BorderLayout.NORTH);//將上面板加到該窗口的上部分 add(new JScrollPane(resultArea),BorderLayout.CENTER);//將結(jié)果的多行輸出加入滾動面板,再把滾動面板加入該窗口的中部分 setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setBounds(100,100,460,260); //設(shè)置監(jiān)聽器 simpleListener=new Button1Listener(); simpleListener.setResultArea(resultArea); simpleListener.setTextFieldA(textFieldA); simpleListener.setTextFieldB(textFieldB); simpleListener.setTextFieldC(textFieldC); //添加監(jiān)聽器 caculateBtn.addActionListener(simpleListener); } }
2 設(shè)置
public void paintComponent(Graphics comp) { ArrayList<String> arrayList = new ArrayList<>(); try { FileReader fr = new FileReader("C:\\Users\\19391\\Desktop\\Java課程設(shè)計(jì)\\select.txt");//把這個(gè)地址換為你想要讀入的文本文件地址 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中存儲的字符串進(jìn)行處理 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);//設(shè)置字體 comp2D.setPaint(gp); GradientPaint gp2=new GradientPaint(0,0,Color.blue,0,getSize().height,Color.orange,false);//字體顏色漸變(橙-->藍(lán)) comp2D.fillRect(0, 0, getSize().width, getSize().height); comp2D.setPaint(gp2); for (int i = 0; i < headlines.length; i++)//設(shè)置每一行字的位置 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);//默認(rèn)22個(gè)字節(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,然后轉(zhuǎn)成exe,windows上直接雙擊就可以用
截圖展示:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
spring的applicationContext.xml文件與NamespaceHandler解析
這篇文章主要介紹了spring的applicationContext.xml文件與NamespaceHandler解析,Spring容器啟動,在創(chuàng)建BeanFactory時(shí),需要加載和解析當(dāng)前ApplicationContext對應(yīng)的配置文件applicationContext.xml,從而獲取bean相關(guān)的配置信息,需要的朋友可以參考下2023-12-12springboot集成junit編寫單元測試實(shí)戰(zhàn)
在做單元測試時(shí),代碼覆蓋率常常被拿來作為衡量測試好壞的指標(biāo),本文主要介紹了springboot集成junit編寫單元測試實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02