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

前端 未结 4 1892
庸人自扰
庸人自扰 2021-02-04 00:17

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 expec

4条回答
  •  不思量自难忘°
    2021-02-04 00:50

    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 );
    }
    

提交回复
热议问题