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
  • 45,768
  • 6
  • 62
  • 130
chris01
  • 9,483
  • 8
  • 45
  • 75

0 Answers0