tl;dr
Duration.parse( "PT1H30M" ).toMillis()
ISO 8601
No need to invent your own custom shorthand format. The ISO 8601 standard already defines a similar format for durations.
The pattern PnYnMnDTnHnMnS
uses a P
to mark the beginning, a T
to separate any years-months-days portion from any hours-minutes-seconds portion.
Examples:
- An hour and a half is
PT1H30M
.
P3Y6M4D
represents “three years, six months, four days”.
java.time
The java.time classes use the ISO 8601 formats by default when parsing/generating strings. This includes the Period
and Duration
classes for representing spans of time not attached to the timeline.
Duration
Duration d = Duration.ofHours( 1 ).plusMinutes( 30 );
String output = d.toString();
PT1H30M
And parsing.
Duration d2 = Duration.parse( "PT1H30M" );
You can ask for the duration a total number of milliseconds.
long millis = d2.toMillis();
5400000
See live code in IdeOne.com.
But remember that java.time classes have much finer resolution, nanoseconds. So you may be losing data when asking for milliseconds.
Also, I strongly suggest you stick to using the java.time objects and the ISO 8601 strings and avoid representing date-time values as a count of milliseconds or such.
Period
For years-months-days, use the Period
class.
Period p = Period.parse( "P3Y6M4D" );
…and…
String output = p.toString();
P3Y6M4D
Note the normalized
method on this class. For example, a period of "1 Year and 15 months" will be normalized to "2 years and 3 months".
Also note that a Period
is built on LocalDate
info. As such it has no concept of time zones nor any idea about anomalies such as Daylight Saving Time (DST).
This class works with a resolution of whole days. So the class does not provide an explicit way to count milliseconds.
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 and 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 SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.