How do I format a javax.time.Instant
as a string in the local time zone? The following translates a local Instant
to UTC, not to the local time zone as I was expecting. Removing the call to toLocalDateTime()
does the same. How can I get the local time instead?
public String getDateTimeString( final Instant instant )
{
checkNotNull( instant );
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendPattern( "yyyyMMddHHmmss" ).toFormatter();
return formatter.print( ZonedDateTime.ofInstant( instant, TimeZone.UTC ).toLocalDateTime() );
}
Note: We're using the older version 0.6.3 of the JSR-310 reference implementation.
The question was about version 0.6.3 of the JSR-310 reference implementation, long before the arrival of Java 8 and the new date library
I gave up on JSR-310 classes DateTimeFormatter
and ZonedDateTime
and instead resorted to old fashioned java.util.Date
and java.text.SimpleDateFormat
:
public String getDateTimeString( final Instant instant )
{
checkNotNull( instant );
DateFormat format = new SimpleDateFormat( "yyyyMMddHHmmss" );
Date date = new Date( instant.toEpochMillisLong() );
return format.format( date );
}