Code: Select all
2002-05-30T09:00:00
2002-05-30T09:30:10.5
2002-05-30T09:30:10Z
2002-05-30T09:30:10-06:00
Code: Select all
public static void main(String[] args) {
System.out.println(ZonedDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME).withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime());
}
< /code>
In den ersten beiden Werten, wenn fehlschlägt, weil es keine Zeitzone gibt: < /p>
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-12-05T10:05:19.296' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2017-12-05T10:05:19.296 of type java.time.format.Parsed
Code: Select all
public static void main(String[] args) {
System.out.println(LocalDateTime.parse("2002-05-30T09:30:10-06:00", DateTimeFormatter.ISO_DATE_TIME)); // print 2002-05-30T09:30:10 and I am in +1 timezone
}
unterstützt
Code: Select all
public static LocalDateTime parseDateTime(String strVal) {
try {
if (strVal == null || strVal.isEmpty())
return null;
return ((GregorianCalendar) (DatatypeConverter.parseDateTime(strVal))).toZonedDateTime()
.withZoneSameLocal(ZoneId.systemDefault()).toLocalDateTime();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}