I want to parse LocalDateTime 2021-11-24T15:11:38.395
to LocalDateTime 2021-11-24T15:11:38.39
. But LocalDateTime.parse() always adds zero in the end, ignoring my pattern.
public class DateTimeFormatUtils {
private static final String ISO_DATE_TIME = "yyyy-MM-dd'T'HH:mm:ss.SS";
public static LocalDateTime formatToISO(final LocalDateTime localDateTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ISO_DATE_TIME);
return LocalDateTime.parse(formatter.format(localDateTime), formatter);
}
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println(formatToISO(now));
}
}
Output:
2021-11-30T11:48:28.449195200
2021-11-30T11:48:28.440
Is there way to deal with this problem?