You can use the optional pattern (inside square bracket) for the missing parts. You can also use DateTimeFormatterBuilder#parseDefaulting
to default the missing parts of time to 0
or any other value.
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);
}
}
Output:
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
Update
This is an update based on the OP's request to have timezone with the date-time.
LocalDateTime
does not have timezone information. In order to have the timezone information, you need to convert it into ZonedDateTime
.
Demo:
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"));
}
}
Output:
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