TL; DR
Use plain text in UTC in the standard ISO 8601 format .
Instant.now().toString()
2018-01-23T01: 23: 45.123456Z
Instant.parse( "2018-01-23T01:23:45.123456Z" )
ISO 8601
The ISO 8601 standard is a well-designed, practical set of text formats for representing date and time values.
2018-01-14T03: 57: 05.744850Z
The java.time classes use these standard default formats when parsing / generating strings. The ZonedDateTime class intelligently extends the standard to add the time zone name in square brackets.
ZoneId z = ZoneId.of( "America/Los_Angeles" ) ; ZonedDateTime zdt = ZonedDateTime.now( z ) ; String output = zdt.toString() ;
2018-01-13T19: 56: 26.318984-08: 00 [America / Los_Angeles]
java.time
The java.util.Calendar class is part of the problematic old time classes associated with the earliest versions of Java. These inherited classes are terrifying and should be avoided.
Now superseded by industry-leading java.time classes.
UTC
As a general rule, it is best to report the time using UTC rather than a specific time zone.
The standard format for UTC is YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ , where T separates the year-month-day from the hour-minute-second. Z at the end is short for Zulu and means UTC .
Instant
Instant class represents a moment on the UTC timeline with a nanosecond resolution (up to nine (9) decimal digits).
Instant instant = Instant.now() ; String output = instant.toString() ;
2018-01-14T03: 57: 05.744850Z
If a specific time zone is critical, use ZonedDateTime as shown above.
Syntactic
These strings in standard format can be parsed to instantiate java.time objects.
Instant instant = Instant.parse( "2018-01-14T03:57:05.744850Z" ) ; ZonedDateTime zdt = ZonedDateTime.parse( "2018-01-13T19:56:26.318984-08:00[America/Los_Angeles]" ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .