Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

前端 未结 6 618
天命终不由人
天命终不由人 2020-12-07 10:35

I\'m trying to format a date in yyyy-MM-dd\'T\'HH:mm:ss.SSSz format to yyyy-mm-dd HH:mm:ss, which should be easy but I can\'t get it to work.

A date that has to be p

6条回答
  •  臣服心动
    2020-12-07 11:16

    Gooye if it's possible to use Joda Time in your project then this code works for me:

    String dateStr = "2012-10-01T09:45:00.000+02:00";
    String customFormat = "yyyy-MM-dd HH:mm:ss";
    
    DateTimeFormatter dtf = ISODateTimeFormat.dateTime();
    LocalDateTime parsedDate = dtf.parseLocalDateTime(dateStr);
    
    String dateWithCustomFormat = parsedDate.toString(DateTimeFormat.forPattern(customFormat));
    System.out.println(dateWithCustomFormat);
    

提交回复
热议问题