本文介紹了在CN1上獲取不帶時(shí)間的當(dāng)前日期的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我無(wú)法在沒(méi)有時(shí)間的情況下獲取日期,我想將時(shí)間設(shè)置為00:00,這樣我就可以檢查日期差異。
我到目前為止嘗試的步驟:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date now = new Date();
try {
now = dateFormat.parse(dateFormat.format(now));
} catch (ParseException e1) {
//
}
此代碼仍然需要時(shí)間,我采用了此問(wèn)題中的類(lèi)似代碼How do I get a Date without time in Java?由Shobbi回答
之前我的成績(jī)更好,但時(shí)間仍然設(shè)置為01:00。
誰(shuí)有更好的辦法解決我的問(wèn)題?
我需要將SQLite數(shù)據(jù)庫(kù)中的日期與實(shí)際日期進(jìn)行比較。
請(qǐng)記住,CN1數(shù)據(jù)庫(kù)基于JDK1.3,因此使用方法受到限制
推薦答案
為什么不使用字符串?如果使用yyyy-MM-dd
String.compareTo(otherDateString)
格式,String.compareTo(otherDateString)
應(yīng)該告訴您這兩個(gè)日期是如何相關(guān)的(一個(gè)日期在另一個(gè)日期之前、之后或同一天)
例如:
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//get the other date into a string
boolean isInThePast = (today.compareTo(otherDateString) > 0);
這篇關(guān)于在CN1上獲取不帶時(shí)間的當(dāng)前日期的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,