[英]Storing a date in .property and comparing with current date
如何將未來日期存儲在.property文件中,並將該日期與當前日期進行比較?
你可以:
屬性文件:
myDate=25/01/2012
myDate.pattern=dd/MM/yyyy
然后,您可以在程序中通過以下方式加載日期:
// choose one of two lines!
String pattern = "dd/MM/yyyy"; // for a fixed format
String pattern = propertyFile.getProperty("myDate.pattern");
String formattedDate = propertyFile.getProperty("myDate");
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date myDate = format.parse(formattedDate);
編輯 :與當前比較...
1. boolean isFuture = myDate.after(new Date())
2. boolean isPast = myDate.before(new Date())
3. int compare = myDate.compareTo(new Date());
// compare < 0 => before
// compare == 0 => equal
// compare > 0 => after
好吧,這里有幾件事情要考慮:
如果您只是處理一個瞬間,而又不關心任何人都可以從屬性文件中了解它是哪個瞬間,那么我很想存儲long
的字符串表示形式(即,在java.util.Date
使用millis java.util.Date
),然后對其進行檢查,只需解析該字符串並將結果與System.currentTimeMillis
進行比較。 它避免了日期和時間的整個混亂格式和解析。
對於任何更復雜的事情,我將強烈建議您使用Joda Time ,它將使您更容易理解您真正處理的概念(例如LocalDate
, LocalTime
或DateTime
),並且比JDK具有更好的格式和解析支持(IMO) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.