JSR-310 - parsing seconds fraction with variable length

前端 未结 2 1742
攒了一身酷
攒了一身酷 2020-11-30 07:26

Is there a way how to create JSR-310 formatter that is able to parse both following date/times with variable length of seconds fraction?

2015-05-07 13:20:22.         


        
2条回答
  •  感动是毒
    2020-11-30 08:04

    And this one works

    DateTimeFormatter formatter
    = new java.time.format.DateTimeFormatterBuilder()
            .append( java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") )
            .appendOptional(
                    new java.time.format.DateTimeFormatterBuilder()
                        .appendLiteral('.')
                        .appendValue( ChronoField.MICRO_OF_SECOND, 1, 6, SignStyle.NOT_NEGATIVE).toFormatter())
            .toFormatter();
    

提交回复
热议问题