简单的 Java 日期计算
我想用Java 进行简单的日期计算。 例如,计算日期之间的天数差异(时间分量为 0)。 当然,您可以简单地减去毫秒数,然后除以每天的毫秒数,这样效果很好 - 直到夏令时进入场景。 我意识到对“天数差异”的不同解释是可能的,特别是无论是否应该考虑时间成分。 为了简单起见,我们假设我们有一个 0 时间分量。
这种计算似乎是一种非常常见的需求,我发现对解决该问题的不同方法进行讨论很高兴。
I would like to do simple date calculations in Java. For example, compute the difference in days between to dates (having a 0 time component). Of course you could do a simple substraction of milliseconds, divided by the number of milliseconds per day, which works fine - until daylight saving times enter the scene.
I am conscious that different interpretations of the "difference in days" are possible, in particuliar, whether one should take into account the time component or not. Let us assume we have a 0 time component for simplicity.
This kind of calculation seems to be a very common need, and I would find it nice to have a discussion on different approaches to the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会看看 Joda 日期/时间库,特别是 ReadableInterval 类。
Joda 使在 Java 中操作日期/时间时变得更加容易,我相信它是新 Java JSR 310 wrt。 日期/时间。 即它(或非常类似的东西)将出现在 Java 的未来版本中。
I would have a look at the Joda date/time library, and in particular the ReadableInterval class.
Joda makes life a lot easier when manipulating dates/times in Java, and I believe it's the foundation of the new Java JSR 310 wrt. dates/times. i.e. it (or something very similar) will be present in a future version of Java.
看起来 Date、Calendar 和 DateUtils 可以完成您想要的一切。
http://commons.apache.org/ lang/api/org/apache/commons/lang/time/DateUtils.html
DateUtils 允许截断或舍入时间,这样您就可以只处理日期。 您还提到了夏令时。 主要问题是,当有人说他们在 DST 更改的一天的 01:30 做了某事时……那是哪一个 01:30? 第一个还是第二个? 幸运的是,这可以由 Calendar 类处理,因为它存储时区和 DST 偏移量。 有关所有详细信息,请查看:
http://www.xmission.com/~goodhill/dates/deltaDates.html< /a>
It looks like Date, Calendar, and DateUtils will do everything you're looking for.
http://commons.apache.org/lang/api/org/apache/commons/lang/time/DateUtils.html
DateUtils allows truncating or rounding the time so you can just deal with only the dates. You also mentioned daylight savings time. The main problem is when someone says they did something at 01:30 on a day where there was DST change... which 01:30 was it? The first one or the second? Luckily this can be handled by the Calendar class since it stores the timezone and DST offset. For the specifics on all that, check this out:
http://www.xmission.com/~goodhill/dates/deltaDates.html
更简单的选择是使用 Joda 中的 Days、Weeks、Months 和 Years 类。
这样,您就可以做一些简单的事情,例如:
The simpler option is to use the Days, Weeks, Months and Years classes in Joda.
That way, you can do something as simple as:
使用 java.time
现代方法是使用 java.time 类,取代麻烦的旧遗留日期时间类。
LocalDate
类表示仅日期值,没有时间和时区。ChronoUnit
< /a> 枚举有一些方便的方法,例如之间</code>
。
在定义时间跨度时,java.time 类使用半开放方法,其中开始是包含,而结束是排除。
您可以使用
句点
类。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。 这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。 并在 Stack Overflow 上搜索许多示例和解释。 规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。 该项目是 java.time 未来可能添加的内容的试验场。 您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。Using java.time
The modern approach is with the java.time classes, supplanting the troublesome old legacy date-time classes.
The
LocalDate
class represents a date-only value without time-of-day and without time zone.The
ChronoUnit
enum has some handy methods such asbetween
.In defining spans of time, the java.time classes use the Half-Open approach where the beginning is inclusive while the ending is exclusive.
You can represent that span of time as an object, using the
Period
class.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.这是一篇文章讨论使用 java 中的日期/日历工具来计算经过的时间。
Here is an article discussing the use of the Date/Calendar facilities in java to calculate elapsed time.