使用java代碼實(shí)現(xiàn)一個(gè)月內(nèi)不再提醒,通用到期的問(wèn)題
其實(shí)就是最常見(jiàn)的到期問(wèn)題。 例如帳號(hào)到期,會(huì)員到期等。
字段可以命名為:
expire_date 或 valid_date場(chǎng)景
所在的家電公司要做個(gè)不再提醒功能。
其實(shí)就是有效期問(wèn)題,開(kāi)工。
過(guò)程數(shù)據(jù)庫(kù)設(shè)計(jì)
字段:
iduser_account 用戶帳號(hào)create_date 創(chuàng)建時(shí)間update_date 更新時(shí)間expire_date 過(guò)期時(shí)間
時(shí)間類型用設(shè)置么?例如一個(gè)月,一年。
其實(shí)不用,這個(gè)參數(shù)前端傳即可,在邏輯里面轉(zhuǎn)換為expire_date即可。
設(shè)置過(guò)期時(shí)間推薦使用java8 date,非常好用,如下為一個(gè)月后為過(guò)期時(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){ // 沒(méi)有設(shè)置禁用期 那么不禁用 return false;}int i = new Date().compareTo(expireDate);if(i>0){ // 已經(jīng)過(guò)了禁用期,不再禁用,disableTip=false return false;}else{ // 還未過(guò)期,繼續(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í)間到了,會(huì)彈出提示窗口,顯示提示信息
我做這個(gè)軟件,也是工作比較忙,又不能盯著時(shí)間看,所以就做了這個(gè)定時(shí)監(jiān)控提醒軟件,感覺(jué)用的還比較貼心
這里貼一點(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é)果的多行輸出加入滾動(dòng)面板,再把滾動(dòng)面板加入該窗口的中部分 setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setBounds(100,100,460,260); //設(shè)置監(jiān)聽(tīng)器 simpleListener=new Button1Listener(); simpleListener.setResultArea(resultArea); simpleListener.setTextFieldA(textFieldA); simpleListener.setTextFieldB(textFieldB); simpleListener.setTextFieldC(textFieldC); //添加監(jiān)聽(tīng)器 caculateBtn.addActionListener(simpleListener); } }2 設(shè)置
public void paintComponent(Graphics comp) { ArrayList<String> arrayList = new ArrayList<>(); try { FileReader fr = new FileReader('C:Users19391DesktopJava課程設(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(); } // 對(duì)ArrayList中存儲(chǔ)的字符串進(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);//字體對(duì)象 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'; } }
純粹興趣開(kāi)發(fā)
打包成jar,然后轉(zhuǎn)成exe,windows上直接雙擊就可以用
截圖展示:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
