這篇文章將為大家詳細(xì)講解有關(guān)java判斷日期是否是當(dāng)天的方法,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

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("解析日期錯(cuò)誤", e);
}
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
int year1 = c1.get(Calendar.YEAR);
int month2 = 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 month3 = c2.get(Calendar.MONTH)+1;
int day2 = c2.get(Calendar.DAY_OF_MONTH);
if(year1 == year2 && month2 == month3 && day1 == day2){
return true;
}
return false;
}上述代碼中 formatStr 是我們需要校驗(yàn)的日期形式,比如我需要校驗(yàn) “20161212”是否是當(dāng)天,那么formatStr為"yyyyMMdd"。
比如我們需要校驗(yàn)“2016-12-12”是不是當(dāng)天,就為“yyyy-MM-dd”,比如需要校驗(yàn)“2016/12/12”的字符串,就為“yyyy/MM/dd”,依次類(lèi)推即可。
java中使用SimpleDateFormat類(lèi)的構(gòu)造函數(shù)SimpleDateFormat(String str)構(gòu)造格式化日期的格式,
通過(guò)format(Date date)方法將指定的日期對(duì)象格式化為指定格式的字符串.
關(guān)于java判斷日期是否是當(dāng)天的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前名稱(chēng):java判斷日期是否是當(dāng)天的方法-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://www.chinadenli.net/article2/iicic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、搜索引擎優(yōu)化、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站導(dǎo)航、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容