java.time
Both the troublesome old date-time classes from the earliest versions of Java and the Joda-Time library are now legacy, supplanted by the java.time classes.
Formatting pattern
The formatting pattern codes of DateTimeFormatter
are similar to the legacy SimpleDateFormat
class, but not identical. Study the JavaDoc.
OffsetDateTime
The GMT-05:00
type of items is an offset-from-UTC so we process as an OffsetDateTime
. You could parse as a ZonedDateTime
for the simplicity of mixing these objects with those resulting from the other inputs, but it would be misleading to people reading your code.
ZonedDateTime
The UTC
is actually the name of a time zone, so we process as a ZonedDateTime
.
To be clear, GMT and UTC are practically the same thing in terms of conventional business apps. But the usage here is different. The plus or minus numbers indicate an offset-from-UTC but not a full time zone. The UTC
text alone is a zone name, a particular zone that happens to be an offset of zero, but kind of string could just as well have a zone name of America/Montreal
or Europe/Paris
.
Classes for different ways to view a moment:
- The
Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
- An
OffsetDateTime
is like an Instant
, but assigned an offset-from-UTC (a number of hours, minutes, and seconds adjusted in its wall-clock time).
- A
ZonedDateTime
is like an Instant
but assigned a time zone. A time zone is a collection of a region’s past, present, and future offsets.
Example code
List<CharSequence> inputs = new ArrayList<> ( 4 );
inputs.add ( "Mon Jul 28 11:39:29 GMT-05:00 2014" );
inputs.add ( "Mon Jul 28 13:39:29 GMT+02:00 2014" );
inputs.add ( "Thu Jul 17 00:02:02 UTC 2014" );
inputs.add ( "Fri Jul 18 14:47:01 UTC 2014" );
inputs.add ( "garbage-in garbaage-out" );
inputs.add ( "Fri Jan 23 12:34:56 UTC 2099" ); // Future.
Instant now = Instant.now (); // Capture the current moment in UTC.
DateTimeFormatter fGMT = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss OOOO uuuu" );
DateTimeFormatter fUTC = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss z uuuu" );
for ( CharSequence input : inputs ) {
if ( input.length () == "Mon Jul 28 11:39:29 GMT-05:00 2014".length () ) {
OffsetDateTime odt = OffsetDateTime.parse ( input , fGMT );
if ( odt.toInstant ().isAfter ( now ) ) {
System.out.println ( "input: " + input + " is future." );
} else {
System.out.println ( "input: " + input + " is past or present." );
}
} else if ( input.length () == "Thu Jul 17 00:02:02 UTC 2014".length () ) {
ZonedDateTime zdt = ZonedDateTime.parse ( input , fUTC );
if ( zdt.toInstant ().isAfter ( now ) ) {
System.out.println ( "input: " + input + " is future." );
} else {
System.out.println ( "input: " + input + " is past or present." );
}
} else {
System.err.println ( "ERROR - Unexpected input: " + input );
}
}
input: Mon Jul 28 11:39:29 GMT-05:00 2014 is past or present.
input: Mon Jul 28 13:39:29 GMT+02:00 2014 is past or present.
input: Thu Jul 17 00:02:02 UTC 2014 is past or present.
input: Fri Jul 18 14:47:01 UTC 2014 is past or present.
input: Fri Jan 23 12:34:56 UTC 2099 is future.
See this code run live at IdeOne.com.
Date-time math
Adding time seems like a distraction from the core issues of your Question. So I ignored that topic in example above.
But simple enough to do. The classes above have plus…
and minus…
methods for adding or subtracting an number of hours, minutes, etc.
OffsetDateTime odtLater = odt.plusHours( 1 );
ZonedDateTime zdtLater = zdt.plusMinutes( 90 );
You can define a chunk of time as a Duration
.
Duration d = Duration.ofMinutes( 90 ) ;
ZonedDateTime zdtLater = zdt.plus( d );
The java.time classes use immutable objects. So the result of a manipulation is a fresh object with values based on the original.
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.