Java 8 optional time part not working

后端 未结 3 2026
终归单人心
终归单人心 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:58

    Reason why you are getting null when you pass this is

    "2017-07-01T00:00:00.0Z"

    you have told the parser to expect 3 char format (see below .SSS)

    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]'Z'");

    If you change it to .SS, it should work, however your last input i.e. 2017-07-01T00:00:00.000Z will break. You may want to include some logic to handle different formats

提交回复
热议问题