3

Why is DateTimeFormatter able to format a fraction of a second but not able to parse it?

The code below fails at the parse if I do not replace the fmt to "yyyyMMddHHmmss" (remove the SS at the end);

import java.time.*;
import java.time.format.*;

public static void main (String [] args) throws Exception
{    
  String fmt = "yyyyMMddHHmmssSS";
  ZonedDateTime now = ZonedDateTime.now ();
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern (fmt).withZone (ZoneOffset.UTC);
  String s = dtf.format (now);
  ZonedDateTime x = ZonedDateTime.parse (s, dtf);
}

Exception is

Text '2018071819500784' could not be parsed at index 0
shmosel
  • 49,289
  • 6
  • 73
  • 138
chris01
  • 10,921
  • 9
  • 54
  • 93

0 Answers0