如何将日期字符串(2021-01-13)转换为'2020-01-13T00:00:00.000-4:00'格式而不丢失秒、微秒

How to convert Date String (2021-01-13) to '2020-01-13T00:00:00.000-4:00' format without losing seconds, microseconds

我已经使用下面的代码将字符串转换为所需的格式。但是在输出秒中,缺少微秒。

LocalDate date = LocalDat.parse(inputString, DateTimeFormatter.ISO_LOCAL_DATE);
date.atStartOfDay(ZooneId.of("America/New_York").toOffsetDateTime().toString();

根据以上代码输出:'2021-01-13T00:00-4:00'

要求输出:'2020-01-13T00:00:00.000-4:00'

您可以使用 format() 方法进一步重新格式化它。

LocalDate date = LocalDate.parse("2021-01-13", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date.atStartOfDay(ZoneId.of("America/New_York")).toOffsetDateTime().format(DateTimeFormatter.ofPattern(("yyyy-MM-dd'T'HH:mm:ss:SSSZ"))));

在此处查看可用日期格式的完整列表 https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html