两个 Java 日期之间的天数差异?

发布于 2024-09-11 11:21:46 字数 762 浏览 2 评论 0原文

我想获得两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三生池水覆流年 2024-09-18 11:21:46

看起来mm指的是分钟,而不是月份,这是MM。请检查此处以查看适当的列表刻字:)

It seems like mm refers to minutes, not months, which is MM. Please check here to see the list of appropriate lettering :)

别把无礼当个性 2024-09-18 11:21:46

月份是 MM

在您的情况下:

DateFormat 格式化程序 = 新
SimpleDateFormat("MM/dd/yyyy");

Month is MM

In your case:

DateFormat formatter = new
SimpleDateFormat("MM/dd/yyyy");

樱&纷飞 2024-09-18 11:21:46

你的图案有点缺陷。 mm 被解析为小时中的分钟,您要查找的 MM一年中的月份

Your pattern is slightly defective. mm is parsed as minutes in hour, you're looking for MM which is month of year.

绻影浮沉 2024-09-18 11:21:46

毫米=>只需几分钟,而不是几个月 - 您需要 MM 几个月 - 这将解决您一月的问题!

mm => minutes, not months - you need MM for months - that'll resolve your Jan problem!

花间憩 2024-09-18 11:21:46

其他答案正确地解决了您的具体问题。

LocalDate

但还有一个更大的解决方案。如果您一开始只有日期,没有时间和时区,那么您应该使用 LocalDate 类,而不是 DateTime

时区

您的代码忽略了时区的关键问题。当尝试确定“今天”时,时区甚至对于 LocalDate 也很重要。您想今天在蒙特利尔还是在巴黎约会?巴黎新的一天提前到来。当省略时区时,您将获得 JVM 当前的默认时区。

Joda-Time 可以解析

此外,让 Joda-Time 进行解析。无需使用 java.util.Date & .日历根本没有。 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 ) );

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 than 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.

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.

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 ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文