8

This seems weird. Java 8 is formatting the output differently depending on whether the millis is zero. How do you force Java 8 (1.8.0_20) to always spit out the millis regardless of if they're zero or not?

public static void main(String[] args) {
    TemporalAccessor zeroedMillis = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse("2015-07-14T20:50:00.000Z");
    TemporalAccessor hasMillis = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse("2015-07-14T20:50:00.333Z");
    System.out.println(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zeroedMillis));
    System.out.println(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(hasMillis));
}

2015-07-14T20:50:00Z
2015-07-14T20:50:00.333Z
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Chris Kessel
  • 5,583
  • 4
  • 36
  • 55

1 Answers1

12

You don't use ISO_OFFSET_DATE_TIME, basically :)

If you follow the documentation for that, you end up in the docs of ISO_LOCAL_TIME which has:

This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended local time format. The format consists of:

  • Two digits for the hour-of-day. This is pre-padded by zero to ensure two digits.
  • A colon
  • Two digits for the minute-of-hour. This is pre-padded by zero to ensure two digits.
  • If the second-of-minute is not available then the format is complete.
  • A colon
  • Two digits for the second-of-minute. This is pre-padded by zero to ensure two digits.
  • If the nano-of-second is zero or not available then the format is complete.
  • A decimal point
  • One to nine digits for the nano-of-second. As many digits will be output as required.

If you always want exactly 3 digits, I suspect you want DateTimeFormatter.ofPattern with a pattern of yyyy-MM-dd'T'HH:mm:ss.SSSX.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Yea, unfortunately, that pattern fails to parse: "could not be parsed at index 23". Searches seem to indicate some sort of bug related to fractional (milli) parsing. – Chris Kessel Feb 03 '16 at 18:32
  • @Chris: Will have a look when I've finished tea :) – Jon Skeet Feb 03 '16 at 18:42
  • @ChrisKessel: Hmm... it works for me...that's very odd. – Jon Skeet Feb 03 '16 at 18:55
  • I'm trying this, what's your code look like? DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2016-02-03T10:40:31.457Z"); Error: Text '2016-02-03T10:40:31.457Z' could not be parsed at index 23 – Chris Kessel Feb 03 '16 at 18:58
  • @ChrisKessel: Ah, so it's parsing the *value* that's the problem. From your previous comment I thought you meant the *pattern* failed to parse, i.e. that `ofPattern` failed. Will look into that. – Jon Skeet Feb 03 '16 at 19:02
  • 2
    @ChrisKessel: Fixed it - it needs an X instead of a Z at the end. See the edited answer. – Jon Skeet Feb 03 '16 at 19:04
  • Thanks! Strange, the same format with the Z worked in joda, so I figured it'd work in java time. I guess their interpretations of the codes are different? – Chris Kessel Feb 03 '16 at 20:25
  • @Chris: Yup, every API has its own pattern oddities :) – Jon Skeet Feb 03 '16 at 20:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102524/discussion-between-chris-kessel-and-jon-skeet). – Chris Kessel Feb 03 '16 at 22:08