两个 Java 日期之间的天数差异?
我想获得两个 Java Date 对象之间的差异。我使用过 Joda-Time 库。但问题是我得到的天数差异比实际的天数差异更大。
这是我的代码片段:
DateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
Date someDate=new Date();
Date today = Calendar.getInstance().getTime();
try {
someDate = formatter.parse("06/22/2010");
}
catch(ParseException pe) {
System.out.println("Parser Exception");
}
int days = Days.daysBetween(new DateTime(someDate), new DateTime(today)).getDays();
System.out.println(" Days Between " + someDate + " : " + today + " - " + days);
这是我的输出:
Days Between Fri Jan 22 00:06:00 IST 2010 : Sun Jul 25 19:27:01 IST 2010 - 184
Here, Why does it take "06/22/2010" as Jan 22?有人面临类似的问题吗?
帮助我的朋友..提前谢谢..
I want to get the difference between two Java Date objects. I've used Joda-Time library. But the problem is that I'm getting the Days greater difference than that of actual day difference.
Here's my code snippet:
DateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
Date someDate=new Date();
Date today = Calendar.getInstance().getTime();
try {
someDate = formatter.parse("06/22/2010");
}
catch(ParseException pe) {
System.out.println("Parser Exception");
}
int days = Days.daysBetween(new DateTime(someDate), new DateTime(today)).getDays();
System.out.println(" Days Between " + someDate + " : " + today + " - " + days);
Here's my output:
Days Between Fri Jan 22 00:06:00 IST 2010 : Sun Jul 25 19:27:01 IST 2010 - 184
Here, Why does it takes "06/22/2010" as Jan 22? Does anyone face similar problem?
Help me friends.. Thanx in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来
mm
指的是分钟,而不是月份,这是MM
。请检查此处以查看适当的列表刻字:)It seems like
mm
refers to minutes, not months, which isMM
. Please check here to see the list of appropriate lettering :)月份是 MM
在您的情况下:
Month is MM
In your case:
你的图案有点缺陷。
mm
被解析为小时中的分钟,您要查找的MM
是一年中的月份。Your pattern is slightly defective.
mm
is parsed as minutes in hour, you're looking forMM
which is month of year.毫米=>只需几分钟,而不是几个月 - 您需要 MM 几个月 - 这将解决您一月的问题!
mm => minutes, not months - you need MM for months - that'll resolve your Jan problem!
其他答案正确地解决了您的具体问题。
LocalDate
但还有一个更大的解决方案。如果您一开始只有日期,没有时间和时区,那么您应该使用
LocalDate
类,而不是DateTime
。时区
您的代码忽略了时区的关键问题。当尝试确定“今天”时,时区甚至对于 LocalDate 也很重要。您想今天在蒙特利尔还是在巴黎约会?巴黎新的一天提前到来。当省略时区时,您将获得 JVM 当前的默认时区。
Joda-Time 可以解析
此外,让 Joda-Time 进行解析。无需使用 java.util.Date & .日历根本没有。 Joda-time 的格式字符几乎与 java.util.Date 相同,但不完全一样,因此请查阅文档。在这种情况下它是相同的。
The other answers correctly solved your specific problem.
LocalDate
But there is a larger solution. If you are starting with only dates, no time-of-day and no time zones, then you should be using the
LocalDate
class rather thanDateTime
.Time Zone
Your code ignores the crucial issue of time zones. Time zones matter even for LocalDate, when trying to determine "today". Do you want today's date in Montréal or in Paris. A new day dawns in Paris earlier. When you omit time zone, you get the JVM’s current default time zone.
Joda-Time Can Parse
Furthermore, let Joda-Time do the parsing. No need to be using java.util.Date & .Calendar at all. Joda-time's formatting characters are almost the same as java.util.Date but not entirely so be sire to consult the doc. In this case it it identical.