如何在java中将字符串2020-09-02T12:22:53.9转换为日期格式2020-09-02T12:22:53.9?
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));
这段代码从Instant instant = Instant.parse(dateString);
行抛出一个java.time.format.DateTimeParseException: Text '2020-09-02T12:22:53.9' could not be parsed at index 21
。
发布于 2020-09-24 20:11:30
格式不正确。您使用的是mm
,而不是MM
。此外,您还遗漏了格式中的文字T
。请注意,您需要将HH
用于24小时格式化时间。
由于日期-时间字符串没有时区信息,因此必须将日期-时间字符串解析为LocalDateTime
,然后使用LocalDateTime#atZone
将其转换为ZonedDateTime
(如果需要ZonedDateTime
),如下所示:
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
,如下所示:
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
发布于 2020-09-25 00:16:30
不需要格式化程序
我认为您的日期字符串在美国/墨西哥城市时区,并且您希望输出在相同的时区,因此相同的日期和时间。我进一步认为,在程序内部,您希望将时间点表示为Instant
。从日期字符串到Instant
再返回的整个圆圈可能是这样的:
String tz = "America/Mexico_City";
ZoneId zoneId = ZoneId.of(tz);
String dateString = "2020-09-02T12:22:53.9";
Instant instant = LocalDateTime.parse(dateString).atZone(zoneId).toInstant();
System.out.println(instant);
ZonedDateTime dateTimeInTz =ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(dateTimeInTz.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
输出:
2020-09-02T17:22:53.900Z 2020-09-02T12:22:53.9
您的输入和相同的输出采用ISO 8601格式。LocalDateTime
解析ISO8601格式时不使用时区或偏移量作为其默认值,也就是说,不使用任何显式格式化程序。为了以相同的格式打印ZonedDateTime
,我们使用内置的DateTimeFormatter.ISO_LOCAL_DATE_TIME
。到目前为止,您可能已经意识到,从指定我们自己的格式模式字符串中解脱出来是一大解脱。
你的代码出了什么问题?
Arvind Kumar Avinash已经报告了格式模式字符串中的错误。更根本的是,您无法将字符串解析为Instant
,或者更准确地说,需要更多的东西才能做到这一点。原因是Java和Instant
类不知道也不能猜测您想要的时区。您的日期字符串包含不带任何时区的日期和时间。Instant
是一个时间点,概念上没有任何日期或时间。字符串可以表示不同时区中的不同时间点,因此只有通过指定时区才能确定时间点。
Instant
类可以解析的是用Z
表示的日期和时间。上面的Instant
2020-09-02T17:22:53.900Z
的输出中就有这样一个字符串的例子。由于您的字符串不是UTC格式的(因此没有Z
),这种观察对我们的情况没有帮助。
链接
https://stackoverflow.com/questions/64053655
复制