[英]convert string ("2022-12-23T07:20:00" ) time into ZonedDateTime
i was trying to convet string time into ZonedDateTime but not comes up with solution.我试图将字符串时间转换为 ZonedDateTime 但没有提出解决方案。 This is the string format of time "2022-12-23T07:20:00"这是时间“2022-12-23T07:20:00”的字符串格式
i have tried this approach我试过这种方法
String stringDate = "2022-12-23T07:20:00";字符串 stringDate = "2022-12-23T07:20:00";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
ZonedDateTime ztdOfDateOfPurchase = ZonedDateTime.parse(stringDate , dateTimeFormatter);
this error is coming=> java.time.format.DateTimeParseException: Text '2022-12-23T07:20:00' could not be parsed at index 19
Simply parse your date-time string using LocalDateTime#parse
and add the applicable ZoneId
to get the ZonedDateTime
.只需使用LocalDateTime#parse
解析您的日期时间字符串并添加适用的ZoneId
即可获得ZonedDateTime
。
Note that java.time
API is based on ISO 8601 and therefore you do not need a DateTimeFormatter
to parse a date-time string which is already in ISO 8601 format (eg your date-time string, 2022-12-23T07:20:00
).请注意, java.time
API 基于ISO 8601 ,因此您不需要DateTimeFormatter
来解析已经采用 ISO 8601 格式的日期时间字符串(例如,您的日期时间字符串2022-12-23T07:20:00
).
Demo :演示:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.parse("2022-12-23T07:20:00");
// Replace ZoneId.systemDefault() with the applicable zone ID e.g.
// ZoneId.of("America/New_York")
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
System.out.println(zdt);
// Alternatively,
zdt = ldt.atZone(ZoneId.systemDefault());
System.out.println(zdt);
}
}
Output in my timezone : Output 在我的时区:
2022-12-23T07:20Z[Europe/London]
2022-12-23T07:20Z[Europe/London]
Learn more about the modern Date-Time API from Trail: Date Time .从Trail:Date Time中了解有关现代日期时间 API 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.