如何找到两个日期之间的天数? [英] How can I find the number of days between two Dates?

查看:153
本文介绍了如何找到两个日期之间的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Date s。我如何知道这两个日期之间的差异?

I have two Dates. How can I tell the difference between these two dates in days?

我听说过 SimpleDateFormat ,但我不知道如何使用它。

I have heard of SimpleDateFormat, but I don't know how to use it.

我尝试过:

String fromdate = "Apr 10 2011";

SimpleDateFormat sdf;
sdf = new SimpleDateFormat("MMM DD YYYY"); 

sdf.parse(fromdate);

Calendar cal = Calendar.getInstance();
cal.setTime(sdf);

我也尝试过:

String date1 = "APR 11 2011";
String date2 = "JUN 02 2011";
String format = "MMM DD YYYY";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = sdf.parse(date1);
Date dateObj2 = sdf.parse(date2);
long diff = dateObj2.getTime() - dateObj1.getTime();
int diffDays = (int) (diff / (24* 1000 * 60 * 60));
System.out.println(diffDays);

但是我收到异常非法模式字符Y。

but I got the exception "Illegal pattern character 'Y'."

推荐答案

其他答案正确但过时。

java .time 框架内置于Java 8及更高版本中。这些类代替旧的麻烦的日期时间类,例如 java.util.Date .Calendar ,& java.text.SimpleDateFormat Joda-Time 团队还建议迁移到java.time。

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat. The Joda-Time team also advises migration to java.time.

要了解更多信息,请参阅 Oracle教程。并搜索Stack Overflow以获取许多示例和解释。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

许多java.time功能都被后端移植到Java 6& 7在 ThreeTen-Backport 中,并进一步适应于 ThreeTenABP

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

ThreeTen-Extra 项目扩展了java.time和其他类。这个项目是未来可能添加到java.time的一个证明。

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

LocalDate 类代表只有日期的日期,没有时区,没有时区。

The LocalDate class represents a date-only value without time-of-day and without time zone.

我建议在可能的情况下,为您的字符串使用标准的 ISO 8601 格式。但在您的情况下,我们需要指定格式化模式。

I suggest using standard ISO 8601 formats for your strings where possible. But in your case we need to specify a formatting pattern.

String input = "Apr 10 2011";

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "MMM d uuuu" );
LocalDate earlier = LocalDate.parse ( input , f );

DateTimeFormatter formatterOut = DateTimeFormatter.ofLocalizedDate ( FormatStyle.MEDIUM ).withLocale ( Locale.US );
String output = earlier.format ( formatterOut );

LocalDate later = earlier.plusDays ( 13 );

long days = ChronoUnit.DAYS.between ( earlier , later );

转储到控制台。

System.out.println ( "from earlier: " + earlier + " to later: " + later + " = days: " + days + " ending: " + output );




从早期版本:2011-04-10至更高版本:2011-04 -23 =天:13结束日:2011年4月10日

from earlier: 2011-04-10 to later: 2011-04-23 = days: 13 ending: Apr 10, 2011

这篇关于如何找到两个日期之间的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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