skip to Main Content

I am trying to deserialize json String date to Calendar and deserialization is working however it is not what i want.

Current Behaviour

@JsonProperty("systemDt")
@JsonFormat(Pattern="yyyy-MM-dd HH:mm:ss.SSS")
Calendar responseDateTime;

Deserializes to responseDateTime=java.util.GregorianCalendar[time=1141556400000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC"......

I want a Simple Calendar Date of the format yyyy-MM-dd HH:mm:ss.SSS

2

Answers


  1. tl;dr

    LocalDateTime
    .parse( 
        "2023-11-10 23:01:18.940"
        .replace( " " , "T" ) 
    )
    

    Details

    Not enough information. Your input represents a date and a time of day but lacks the context of an offset or time zone. So you cannot parse directly as a Calendar.

    Also, Calendar is a terribly flawed obsolete legacy class.

    Parse your input as a LocalDateTime after replacing the SPACE in the middle with a T to comply with ISO 8601 standard.

    LocalDateTime.parse( "2023-11-10 23:01:18.940".replace( " " , "T" ) )
    

    I want a Simple Calendar Date of the format yyyy-MM-dd HH:mm:ss.SSS

    Date-time objects are not text. They do not have a format.

    Login or Signup to reply.
  2. You should been using LocalDateTime property in code.
    eg:

    public static void main(String[] args) {
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
            System.out.println(LocalDateTime.now().format(dateFormatter));
        }
    

    output:

    2023-11-30 10:59:47.852
    

    final:

    @JsonProperty("systemDt")
    @JsonFormat(Pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private LocalDateTime responseDateTime;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search