Java:DateTimeFormatter和亚秒级解析

时间:2018-07-18 19:55:35

标签: java datetime java-8 java-time

为什么DateTimeFormatter可以格式化几分之一秒但不能解析它?

如果我不将fmt替换为“ yyyyMMddHHmmss”,则下面的代码将在解析时失败(最后删除SS);

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

例外是

Text '2018071819500784' could not be parsed at index 0

0 个答案:

没有答案