How do I format a javax.time.Instant as a string in the local time zone?

你说的曾经没有我的故事 提交于 2019-12-09 04:18:15

问题


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.


回答1:


Answering this question wrt the nearly finished JDK1.8 version

DateTimeFormatter formatter =
  DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.systemDefault());
return formatter.format(instant);

The key is that Instant does not have any time-zone information. Thus it cannot be formatted using any pattens based on date/time fields, such as "yyyyMMddHHmmss". By specifying the zone in the DateTimeFormatter, the instant is converted to the specified time-zone during formatting, allowing it to be correctly output.

An alternative approach is to convert to ZonedDateTime:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
return formatter.format(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));

Both approaches are equivalent, however I would generally choose the first if my data object was an Instant.




回答2:


Why would you expect it to use the local time zone? You're explicitly asking for UTC:

ZonedDateTime.ofInstant(instant, TimeZone.UTC)

Just specify your local time zone instead:

ZonedDateTime.ofInstant(instant, TimeZone.getDefault())



回答3:


Try this:

String dateTime = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(
    ZonedDateTime.ofInstant(instant, ZoneId.systemDefault())
);

This gives:

2014-08-25T21:52:07-07:00[America/Los_Angeles]

You can change the format by using something other than DateTimeFormatter.ISO_ZONED_DATE_TIME as the formatter. DateTimeFormatter has a bunch of predefined formatters, or you can define your own.




回答4:


Before you down vote my answer, please note that the question explicitly (and in bold typeface) refers to old version 0.6.3 of the JSR-310 reference implementation! I asked this question in December 2012, 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 );
}


来源:https://stackoverflow.com/questions/13752031/how-do-i-format-a-javax-time-instant-as-a-string-in-the-local-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!