您可以对缺少的部分使用可选模式(方括号内)。您还可以使用DateTimeFormatterBuilder#parseDefaulting 将缺少的时间部分默认为0 或任何其他值。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
// Tests
System.out.println(getLocalDateTime("2020-01-01 12:23:30.345"));
System.out.println(getLocalDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12:23:30"));
System.out.println(getLocalDateTime("2020-01-01 12:23:30").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12:23"));
System.out.println(getLocalDateTime("2020-01-01 12:23").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12"));
System.out.println(getLocalDateTime("2020-01-01 12").format(dtfForFormating));
}
static LocalDateTime getLocalDateTime(String text) {
DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.toFormatter(Locale.ENGLISH);
return LocalDateTime.parse(text, multiFormatter);
}
}
输出:
2020-01-01T12:23:30.345
2020-01-01 12:23:30.345
2020-01-01T12:23:30
2020-01-01 12:23:30.000
2020-01-01T12:23
2020-01-01 12:23:00.000
2020-01-01T12:00
2020-01-01 12:00:00.000
更新
这是根据 OP 的要求更新时区的日期时间。
LocalDateTime 没有时区信息。为了获得时区信息,您需要将其转换为ZonedDateTime。
演示:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS z", Locale.ENGLISH);
// Tests
System.out.println(getZonedDateTime("2020-01-01 12:23:30.345"));
System.out.println(getZonedDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12:23:30"));
System.out.println(getZonedDateTime("2020-01-01 12:23:30").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12:23"));
System.out.println(getZonedDateTime("2020-01-01 12:23").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12"));
System.out.println(getZonedDateTime("2020-01-01 12").format(dtfForFormating));
}
static ZonedDateTime getZonedDateTime(String text) {
DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.toFormatter(Locale.ENGLISH);
return LocalDateTime.parse(text, multiFormatter).atZone(ZoneId.of("Etc/UTC"));
}
}
输出:
2020-01-01T12:23:30.345Z[Etc/UTC]
2020-01-01 12:23:30.345 UTC
2020-01-01T12:23:30Z[Etc/UTC]
2020-01-01 12:23:30.000 UTC
2020-01-01T12:23Z[Etc/UTC]
2020-01-01 12:23:00.000 UTC
2020-01-01T12:00Z[Etc/UTC]
2020-01-01 12:00:00.000 UTC