java 實(shí)現(xiàn)當(dāng)前時(shí)間加減30分鐘的時(shí)間代碼
如代碼所示:
SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss'); Date now = new Date(); System.out.println('當(dāng)前時(shí)間:' + sdf.format(now));
方法一:
long time = 30*60*1000;//30分鐘 Date afterDate = new Date(now .getTime() + time);//30分鐘后的時(shí)間 Date beforeDate = new Date(now .getTime() - time);//30分鐘前的時(shí)間 System.out.println(sdf.format(afterDate )); System.out.println(sdf.format(beforeDate));
方法二:
Calendar nowTime = Calendar.getInstance(); nowTime.add(Calendar.MINUTE, 30);//30分鐘后的時(shí)間 System.out.println(sdf.format(nowTime.getTime())); Calendar nowTime2 = Calendar.getInstance(); nowTime2.add(Calendar.MINUTE, -30);//30分鐘前的時(shí)間 System.out.println(sdf.format(nowTime2.getTime()));
Date1.after(Date2),當(dāng)Date1大于Date2時(shí),返回TRUE,當(dāng)小于等于時(shí),返回false;
Date1.before(Date2),當(dāng)Date1小于Date2時(shí),返回TRUE,當(dāng)大于等于時(shí),返回false;
如果業(yè)務(wù)數(shù)據(jù)存在相等的時(shí)候,而且相等時(shí)也需要做相應(yīng)的業(yè)務(wù)判斷或處理時(shí),請(qǐng)注意。
如果有這樣的需求,在某個(gè)日期內(nèi)的業(yè)務(wù)check,那么你需要使用:!Date1.after(Date2)
補(bǔ)充知識(shí):Java 實(shí)現(xiàn) Date日期+1天
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.DAY_OF_MONTH,-1); //利用Calendar 實(shí)現(xiàn) Date日期+1天 end_date = c.getTime();
同理,日期前后天數(shù)可用正負(fù)來(lái)獲取。
以上這篇java 實(shí)現(xiàn)當(dāng)前時(shí)間加減30分鐘的時(shí)間代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP基礎(chǔ)知識(shí)Command對(duì)象講解2. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容3. JavaScrip簡(jiǎn)單數(shù)據(jù)類(lèi)型隱式轉(zhuǎn)換的實(shí)現(xiàn)4. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題5. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢(xún)6. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁(yè)7. ASP中實(shí)現(xiàn)字符部位類(lèi)似.NET里String對(duì)象的PadLeft和PadRight函數(shù)8. XHTML 1.0:標(biāo)記新的開(kāi)端9. JSP 中request與response的用法詳解10. asp知識(shí)整理筆記4(問(wèn)答模式)
