Java 如何比较创建为 JodaTime LocalDate 和 LocalDateTime 的两个日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22376022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two dates created as JodaTime LocalDate and LocalDateTime?
提问by Klausos Klausos
LocalDate startDate = new LocalDate(2014,1,2);
LocalDateTime startDateTime = new LocalDateTime(2014,1,2,14,0);
I need to compare startDateand startDateTimewith respect to the date, something like this:
我需要比较startDate和startDateTime相对于日期,是这样的:
// boolean equalDates = startDate.isequal(startDateTime.getDate());
Is it possible to do this?
是否有可能做到这一点?
采纳答案by Aaron Digulla
If you just want to compare the date part, you can do it like so:
如果您只想比较日期部分,您可以这样做:
LocalDate startDate = new LocalDate(2014, 1, 2);
LocalDateTime startDateTime = new LocalDateTime(2014, 1, 2, 14, 0);
LocalDate forCompare = startDateTime.toLocalDate();
System.out.println("equal dates: " + forCompare.equals(startDate));
// equal dates: true
回答by Nirmal
LocalDate startDate = new LocalDate(2014,1,2);
LocalDateTime startDateTime = new LocalDateTime(2014,1,2,00,0);
System.out.println(startDate.toDate());
System.out.println(startDateTime.toDate());
if(startDate.toDate().compareTo((startDateTime.toDate()))==0){
System.out.println("equal");
}
the output will be:
输出将是:
Thu Jan 02 00:00:00 IST 2014
2014 年 1 月 2 日星期四 00:00:00 IST
Thu Jan 02 00:00:00 IST 2014
2014 年 1 月 2 日星期四 00:00:00 IST
equal
平等的
回答by mel3kings
If you want to check if say one date is in between another time frame, say is date14hrs in between date2, joda has different classes just for those scenarios you can use:
如果您想检查某个日期是否在另一个时间范围之间,例如date14 小时之间date2,则 joda 为您可以使用的那些场景提供了不同的类:
Hours h = Hours.hoursBetween(date1, date2);
Days s = Days.daysBetween(date1, date2);
Months m = Months.monthsBetween(date1,date2);
http://joda-time.sourceforge.net/apidocs/org/joda/time/base/BaseSingleFieldPeriod.html
http://joda-time.sourceforge.net/apidocs/org/joda/time/base/BaseSingleFieldPeriod.html

