Java 8 optional time part not working

后端 未结 3 2084
终归单人心
终归单人心 2021-02-08 05:28

I am trying to create date time format for optional time part currently I implemented this

import java.time.*;
import java.time.format.DateTimeFormatter;
import j         


        
3条回答
  •  天命终不由人
    2021-02-08 05:42

    This seems like a bug. The millisecond part of datetime parsing seems to be in general buggy in Java 8, see this issue and its duplicates.

    Relevant quote:

    Worse however is that the SSS pattern has therefore ended up using strict mode when lenient mode would be appropriate. As it currently stands, DateTimeFormatter.ofPattern("hhmmss.SSS") requires three digits for milliseconds, when it was originally intended to require 0 to 9 (the lenient behaviour).

    Given that the current implementation requires three digits for SSS, it is thus very surprising that adjacent value parsing does not apply.

    But you seem to have found a case where requirement mentioned above does not apply either. In addition, even though the issue above is in state "fixed", your example seems to have issues in both java 8 and java 9:

    >java -version
    java version "9"
    Java(TM) SE Runtime Environment (build 9+181)
    Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
    
    >javac Ideone.java
    
    >java Ideone
    null
    2017-07-01T00:00
    2017-07-01T00:00
    
    >"c:\Program Files\Java\jdk1.8.0\bin"\javac Ideone.java
    
    >"c:\Program Files\Java\jdk1.8.0\bin"\java Ideone
    null
    2017-07-01T00:00
    2017-07-01T00:00
    

    should it entertain 1,2 and 3 fractions? or only 3 fractions?

    Based on the quote, it should be only 3, though originally intended to be 0-9.

提交回复
热议问题