简单的 Java 日期计算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/643402/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:11:12  来源:igfitidea点击:

Simple Java Date Calculations

javadate-arithmetic

提问by mikemax

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.

我想用 Java 做简单的日期计算。例如,计算日期之间的天数差异(具有 0 时间分量)。当然,你可以做一个简单的毫秒减法,除以每天的毫秒数,这很好用 - 直到夏令时进入场景。我意识到对“天数差异”的不同解释是可能的,尤其是是否应该考虑时间部分。为简单起见,让我们假设我们有一个 0 时间分量。

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.

这种计算似乎是一个非常普遍的需求,我会发现对这个问题的不同方法进行讨论会很好。

回答by Brian Agnew

I would have a look at the Jodadate/time library, and in particular the ReadableInterval class.

我会看看Joda日期/时间库,特别是ReadableInterval类。

Joda makes life a lot easier when manipulating dates/times in Java, and I believe it's the foundation of the new Java JSR 310wrt. dates/times. i.e. it (or something very similar) will be present in a future version of Java.

在 Java 中操作日期/时间时,Joda 使生活变得更加轻松,我相信它是新的 Java JSR 310wrt 的基础。日期/时间。即它(或非常相似的东西)将出现在 Java 的未来版本中。

回答by user77978

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

看起来 Date、Calendar 和 DateUtils 可以满足您的所有需求。 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

DateUtils 允许截断或舍入时间,因此您只能处理日期。您还提到了夏令时。主要问题是当有人说他们在 DST 更改的那一天 01:30 做了某事时……那是哪个 01:30?第一个还是第二个?幸运的是,这可以由 Calendar 类处理,因为它存储时区和 DST 偏移量。有关所有这些的详细信息,请查看:http: //www.xmission.com/~goodhill/dates/deltaDates.html

回答by Sarhanis

The simpler option is to use the Days, Weeks, Months and Years classes in Joda.

更简单的选择是使用 Joda 中的 Days、Weeks、Months 和 Years 类。

That way, you can do something as simple as:

这样,你可以做一些简单的事情:

int daysDifference = Days.daysBetween(startInstant, endInstant).getDays();

回答by Basil Bourque

Using java.time

使用 java.time

The modern approach is with the java.time classes, supplanting the troublesome old legacy date-time classes.

现代方法是使用 java.time 类,取代麻烦的旧的遗留日期时间类。

compute the difference in days between to dates (having a 0 time component)

计算日期之间的天数差异(具有 0 时间分量)

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

LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

The ChronoUnitenum has some handy methods such as between.

ChronoUnit枚举具有如一些方便的方法between

In defining spans of time, the java.time classes use the Half-Open approach where the beginning is inclusivewhile the ending is exclusive.

在定义时间跨度时,java.time 类使用半开放方法,其中开始是包含的,而结束是不包含的

LocalDate start = LocalDate.of( 2017 , Month.JANUARY , 23 );
LocalDate stop = LocalDate.of( 2017 , Month.FEBRUARY , 17 );
long daysBetween = ChronoUnit.DAYS.between( start , stop );

You can represent that span of time as an object, using the Periodclass.

您可以使用Period该类将该时间跨度表示为一个对象。

Period p = Period.between( start , stop );


About java.time

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

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

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject 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.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答by James Van Huis

Here is an articlediscussing the use of the Date/Calendar facilities in java to calculate elapsed time.

这是一篇讨论在 Java 中使用日期/日历工具来计算经过时间的文章。