7

I'm familiar with the the date and time classes in the JDK and their associated formatting methods. I may be blind, but I cannot find an equivalent set of classes for processing time intervals. For example, I would like to display the number of days for a given long value of milliseconds. I realize that the method to do these conversions is quite simple, however when you factor in internationalization and localization support this becomes less trivial.

I'm surprised that the JDK is missing support for interval processing. However, databases such as Postgresql support it.

Basically what I'm looking for in the JDK (if I'm too blind to see it) or in a third party library is the following functionality:

  • Time calculation methods. Such as milliseconds to weeks or seconds to nanoseconds. Although the math is simple for these operations, having an API to go through seems more self-documenting to me.
  • Time interval formatting functions that format according to the passed Locale like DateFormat works. For example, in EN_US I would assume that 10 days is well "10 days" and in Japanese I would want "10日" returned.

Is there anything out there or is this a canidate for a new open-source project?

Community
  • 1
  • 1
Elijah
  • 13,368
  • 10
  • 57
  • 89

4 Answers4

9

Have you tried Joda Time?

jassuncao
  • 4,695
  • 3
  • 30
  • 35
  • +1 Joda Time looks pretty awesome. It has interval processing in addition to good localized calendar support. – Elijah Mar 18 '09 at 11:12
  • 1
    This is becoming almost a cliché. (See the comments on http://stackoverflow.com/questions/635935/how-can-i-calculate-a-time-span-in-java-and-format-the-output and http://stackoverflow.com/questions/643402/simple-java-date-calculations, and probably every question in the "related" section.) – Michael Myers Mar 18 '09 at 13:47
  • Maybe this should tell the java developers something about an obvious omission in the jdk. Hey - you guys listening? – Elijah Apr 20 '09 at 06:36
  • Everybody knows the date time classes provided by the JDK lack a lot of features. Sun recognizes this. I think the developer of Joda Time is now working for sun. Hopefully, the next version of Java will have an improved time api. – jassuncao Apr 20 '09 at 08:27
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 28 '17 at 08:14
3

have a look at http://commons.apache.org/ and the DateUtils class there

Tobias
  • 7,238
  • 10
  • 46
  • 77
1

tl;dr

Duration.ofMillis( yourMillisecondsLongNumberGoesHere )
        .toDays()

Using java.time

The troublesome old date-time classes have been supplanted by the java.time classes.

I would like to display the number of days for a given long value of milliseconds

For a span of time involving hours, minutes, and seconds (and days defined as chunks of 24-hours), use the Duration class.

Duration d = Duration.ofMillis( yourMillisecondsLongNumberGoesHere );

To get the number of days, defined by defining the total number of whole seconds by 86,400L, call toDays.

long days = d.toDays();

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Unfortunately there is nothing like this in the Standard Library, but as Tobiask points out the Apache Commons have a useful tool for this.

DurationFormatUtils has a few predefined formats (Hour:Minutes:Second, ISO) and allows you to specify your own.

Kris
  • 14,426
  • 7
  • 55
  • 65