两个 Java 日期之间的天数差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3329469/
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
Difference in Days between two Java dates?
提问by Venkat
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.
我想了解两个 Java Date 对象之间的差异。我使用过 Joda-Time 库。但问题是我得到的天数差异比实际天数差异大。
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?
在这里,为什么将“06/22/2010”设为 1 月 22 日?有没有人面临类似的问题?
Help me friends.. Thanx in advance..
帮帮我的朋友.. 提前谢谢..
回答by npinti
回答by Cambium
Month is MM
月份是MM
In your case:
在你的情况下:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
DateFormat formatter = new SimpleDateFormat(" MM/dd/yyyy");
回答by Esko
Your pattern is slightly defective. mmis parsed as minutes in hour, you're looking for MMwhich is month of year.
你的图案有点瑕疵。mm被解析为hour中的分钟,您正在寻找MM哪个是year of year。
回答by Will A
mm => minutes, not months - you need MM for months - that'll resolve your Jan problem!
mm => 分钟,而不是几个月 - 您需要几个月的 MM - 这将解决您的 Jan 问题!
回答by Basil Bourque
The other answers correctly solved your specific problem.
其他答案正确解决了您的具体问题。
LocalDate
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 LocalDateclass rather than DateTime.
但是有一个更大的解决方案。如果您只从日期开始,没有时间和时区,那么您应该使用LocalDate类而不是DateTime.
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.
您的代码忽略了时区的关键问题。在尝试确定“今天”时,时区甚至对于 LocalDate 也很重要。您想要今天的约会地点是在蒙特利尔还是在巴黎。新的一天在巴黎更早开始了。当您省略时区时,您将获得 JVM 的当前默认时区。
Joda-Time Can Parse
Joda-Time 可以解析
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.
此外,让 Joda-Time 进行解析。根本不需要使用 java.util.Date 和 .Calendar。Joda-time 的格式字符几乎与 java.util.Date 相同,但不完全相同,因此请查阅文档。在这种情况下,它是相同的。
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy" );
LocalDate past = formatter.parseLocalDate( "06/22/2010" );
DateTimeZone = DateTimeZone.forID( "America/Montreal" ): // match time zone intended for that input string.
int days = Days.daysBetween( past, LocalDate.now( timeZone ) );

