tl;dr
Instant.ofEpochSeconds( 962_392_935L ) // Convert from a count of whole seconds since 1970-01-01T00:00Z.
.toString() // Generate a string in standard ISO 8601 format.
2000-06-30T19:22:15Z
Details
The other Answers are needlessly complicated.
The Instant
class represents a point on the timeline in UTC with a resolution of nanoseconds.
Instant.now() // Current moment in UTC.
If you have a count of seconds since the epoch reference date of the first moment of 1970 in UTC, 1970-01-01T00:00Z, then simply use the Instant.ofEpochSecond
factory method.
Instant instant = Instant.ofEpochSeconds( mySeconds ) ;
To generate a String in standard ISO format, simply call Instant::toString
.
String output = instant.toString() ;
2000-06-30T19:22:15Z
To go the other direction, while correcting your example’s June 31st error to June 30th as pointed out by Elliott Frisch:
long secondsSinceEpoch = Instant.parse( "2000-06-30T19:22:15Z" ).toEpochSecond() ; // Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
962392935
Avoid legacy date-time classes always
Do not use the troublesome old date-time classes such as Date
. They are supplanted entirely by the java.time classes built into Java 8 and later.
Avoid LocalDateTime
for your purpose
The suggestions seen elsewhere on this page to use the LocalDateTime
class are ill-advised.
That class purposely lacks any concept of time zone or offset-from-UTC. As such, this class does not represent a specific point on the timeline. You would be discarding the vital information about your value being in UTC.