Đây là một trong những cách sau:
String toDate = "05/11/2010";
if (new SimpleDateFormat("MM/dd/yyyy").parse(toDate).getTime()/(1000 * 60 * 60 * 24) >= System.currentTimeMillis()/(1000 * 60 * 60 * 24)) {
System.out.println("Display report.");
} else {
System.out.println("Don't display report.");
}
Một chút dễ dàng hơn interpretable:
String toDateAsString = "05/11/2010";
Date toDate = new SimpleDateFormat("MM/dd/yyyy").parse(toDateAsString);
long toDateAsTimestamp = toDate.getTime();
long currentTimestamp = System.currentTimeMillis();
long getRidOfTime = 1000 * 60 * 60 * 24;
long toDateAsTimestampWithoutTime = toDateAsTimestamp/getRidOfTime;
long currentTimestampWithoutTime = currentTimestamp/getRidOfTime;
if (toDateAsTimestampWithoutTime >= currentTimestampWithoutTime) {
System.out.println("Display report.");
} else {
System.out.println("Don't display report.");
}
Oh, như một phần thưởng, biến những 's JodaTime:
String toDateAsString = "05/11/2010";
DateTime toDate = DateTimeFormat.forPattern("MM/dd/yyyy").parseDateTime(toDateAsString);
DateTime now = new DateTime();
if (!toDate.toLocalDate().isBefore(now.toLocalDate())) {
System.out.println("Display report.");
} else {
System.out.println("Don't display report.");
}
Câu hỏi ban đầu của bạn đã thành thật nói khủng khiếp. Tôi đã cải thiện nó. Trong tương lai cố gắng càng rõ ràng càng tốt. Đừng lặp lại mọi thứ 3 lần và/hoặc không cần thiết in đậm những thứ :) – BalusC
điều này có thể hữu ích nếu bạn cân nhắc sử dụng thư viện joda http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without- phần thời gian – ukanth
JodaTime cung cấp nhiều hơn API ngày của Java ... http://joda-time.sourceforge.net/. Nó sẽ làm cho điều này và các nhiệm vụ ngày khác của bạn dễ dàng hơn nhiều. – harschware