使用Joda时间比较两个日期 [英] Comparing two dates using Joda time

查看:385
本文介绍了使用Joda时间比较两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个日期,但我遇到麻烦。 1日期是从 java.util.date 对象创建的,另一个是手工制作的。以下代码是一个例子:

I want to compare two dates, however I'm running into trouble. 1 date is created from a java.util.date object and the other is manually crafted. The following code is an example:

Date ds = new Date();
DateTime d = new DateTime(ds);

DateTime e = new DateTime(2012,12,07, 0, 0);
System.out.println(d.isEqual(e));

但测试结果是 false 。我猜这是因为时间。我如何检查这两个日期是否相等(我的意思是年份,月份,日期是否相同)?

However the test turns out false. I am guessing that it is because of the time. How can I check if these two dates are equal to each other (I mean the Year, month, date are identical)?

推荐答案

System.out.println(d.toDateMidnight().isEqual(e.toDateMidnight()));

System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));

这篇关于使用Joda时间比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆