@Test
void test() {
String date = "Mon Aug 19 22:30:00 IST 2019";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy");
ZonedDateTime formatDateTime = ZonedDateTime.parse(date, dateTimeFormatter)
.withZoneSameLocal(
ZoneId.of("UTC"));
System.out.println(formatDateTime);
}
// Output: 2019-08-19T22:30Z[UTC]
The date mentioned in the post is wrong and it will end up in
"java.time.format.DateTimeParseException: Text 'Fri Aug 19 22:30:00 IST 2019' could not be parsed: Conflict found: Field DayOfWeek 1 differs from DayOfWeek 5 derived from 2019-08-19"
2条答案
按热度按时间cedebl8k1#
niknxzdl2#
通常,下面的代码用于转换和打印
2019-04-01T11:05:30Z[GMT]
```val formatter = DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss z")
val dateTime = ZonedDateTime.parse("Mon, 1 Apr 2019 11:05:30 GMT", formatter)
println(dateTime)
(例如kotlin。但它使用与java相同的库)