java判斷日期是不是當天:
public static boolean isToday(String str, String formatStr) throws Exception{ SimpleDateFormat format = new SimpleDateFormat(formatStr); Date date = null; try { date = format.parse(str); } catch (ParseException e) { logger.error("解析日期错误", e); } Calendar c1 = Calendar.getInstance(); c1.setTime(date); int year1 = c1.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH)+1; int day1 = c1.get(Calendar.DAY_OF_MONTH); Calendar c2 = Calendar.getInstance(); c2.setTime(new Date()); int year2 = c2.get(Calendar.YEAR); int month2 = c2.get(Calendar.MONTH)+1; int day2 = c2.get(Calendar.DAY_OF_MONTH); if(year1 == year2 && month1 == month2 && day1 == day2){ return true; } return false; }
上述程式碼中formatStr 是我們需要校驗的日期形式,例如我需要校驗「20161212」是否是當天,那麼formatStr為"yyyyMMdd"。
例如我們需要校驗“2016-12-12”是不是當天,就為“yyyy-MM-dd”,比如需要校驗“2016/12/12”的字符串,就為“ yyyy/MM/dd”,依次類推即可。
java中使用SimpleDateFormat類別的建構子SimpleDateFormat(String str)建構格式化日期的格式,
透過format(Date date)方法將指定的日期物件格式化為指定格式的字串.
更多java知識請關注java基礎教學。
以上是java判斷日期是否是今天的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器