Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

java - What is the equivalent format string of DateTimeFormatter.ISO_OFFSET_DATE_TIME?

Do we know if there is an equivalent format string that outputs the same result as DateTimeFormatter.ISO_OFFSET_DATE_TIME?

i.e.

ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(dateTime.format(DateTimeFormatter.ofPattern(pattern)));
System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

would output the same

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This worked for me:

    ZonedDateTime dateTime = ZonedDateTime.now();
    System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")));
    System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

2018-10-03T07:24:14.772+03:00 
2018-10-03T07:24:14.772+03:00

Though it will not always produce the same result because ISO_OFFSET_DATE_TIME prints fraction of second with different length depending on nanos value, while .SSS has fixed lengh = 3

     ZonedDateTime dateTime = ZonedDateTime.of(2001, 1, 1, 0, 0, 0, 1, ZoneId.systemDefault());
     System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")));
     System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));   

     2001-01-01T00:00:00.000+02:00
     2001-01-01T00:00:00.000000001+02:00

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...