Simple Java Date Calculations

孤者浪人 提交于 2019-12-10 13:26:25

问题


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.


回答1:


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.




回答2:


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




回答3:


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

That way, you can do something as simple as:

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



回答4:


Using java.time

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

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

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

The ChronoUnit enum has some handy methods such as between.

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

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 Period class.

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

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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

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.




回答5:


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



来源:https://stackoverflow.com/questions/643402/simple-java-date-calculations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!