将UTC字符串日期转换为日期格式 [英] Convert UTC string date to date format
问题描述
如何转换字符串 2020-09-02T12:22:53.9迄今Java中的格式2020-09-02T12:22:53.9?
How to convert string "2020-09-02T12:22:53.9" to date format 2020-09-02T12:22:53.9 in java?
String dateString = "2020-09-02T12:22:53.9";
String tz = "America/Mexico_City";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-mm hh:mm:ss.S");
ZoneId zoneId = ZoneId.of(tz);
Instant instant = Instant.parse(dateString);
ZonedDateTime dateTimeInTz =ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(dateTimeInTz.format(dtf));
此代码引发 java.time.format.DateTimeParseException:文本'2020-09-02T12 :22:53.9'无法在索引21
中从行 Instant Instant = Instant.parse(dateString);
解析。
This code throws a java.time.format.DateTimeParseException: Text '2020-09-02T12:22:53.9' could not be parsed at index 21
form the line Instant instant = Instant.parse(dateString);
.
推荐答案
您的格式不正确。您在本月中使用了 mm
而不是 MM
。另外,您还错过了格式中的文字 T
。请注意,您需要在24小时格式时间内使用 HH
。
Your format is not correct. You have used mm
, instead of MM
, for the month. Also, you have missed the literal T
in the format. Note that you need to use HH
for 24-hours format time.
由于日期时间字符串中没有时间,区域信息,您必须将日期时间字符串解析为 LocalDateTime
,然后使用 LocalDateTime#atZone
进行转换设置为 ZonedDateTime
(如果您完全需要 ZonedDateTime
),如下所示:
Since your date-time string does not have a time-zone information, you will have to parse the date-time string to LocalDateTime
and then use LocalDateTime#atZone
to convert it to ZonedDateTime
(if at all you need ZonedDateTime
) as shown below:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateString = "2020-09-02T12:22:53.9";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM'T'HH:mm:ss.S");
String tz = "America/Mexico_City";
ZoneId zoneId = ZoneId.of(tz);
// Parse the given date-time string to LocalDateTime
LocalDateTime ldt = LocalDateTime.parse(dateString, dtf);
// Convert the LocalDateTime to ZonedDateTime
ZonedDateTime dateTimeInTz = ldt.atZone(zoneId);
// Display ZonedDateTime in its default format
System.out.println(dateTimeInTz);
// Display ZonedDateTime in your custom format
System.out.println(dateTimeInTz.format(dtf));
}
}
输出:
2020-02-09T12:22:53.900-06:00[America/Mexico_City]
2020-09-02T12:22:53.9
或者,您可以将 ZoneId
与 DateTimeFormatter
本身,然后可以将给定的日期时间字符串直接解析为 ZonedDateTime
,如下所示:
Alternatively, you can use the ZoneId
with DateTimeFormatter
itself and then you can parse the given date-time string directly into ZonedDateTime
as shown below:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateString = "2020-09-02T12:22:53.9";
String tz = "America/Mexico_City";
ZoneId zoneId = ZoneId.of(tz);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM'T'HH:mm:ss.S").withZone(ZoneId.of(tz));
// Parse the given date-time string into ZonedDateTime
ZonedDateTime dateTimeInTz = ZonedDateTime.parse(dateString, dtf);
// Display ZonedDateTime in its default format
System.out.println(dateTimeInTz);
// Display ZonedDateTime in your custom format
System.out.println(dateTimeInTz.format(dtf));
}
}
输出:
2020-02-09T12:22:53.900-06:00[America/Mexico_City]
2020-09-02T12:22:53.9
这篇关于将UTC字符串日期转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!