简体   繁体   English

将字符串 ("2022-12-23T07:20:00") 时间转换为 ZonedDateTime

[英]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.

相关问题 将类型为“ 2015-23-07T00:00:00Z”的字符串转换为格式为“ 07/23 / 2015T00:00:00Z”的XMLGregorianCalender - Convert String of type “2015-23-07T00:00:00Z” to XMLGregorianCalender of format “07/23/2015T00:00:00Z” Android - 如何解析此日期[2014-04-23T14:20:00.000-07:00]? - Android - How to parse this date [ 2014-04-23T14:20:00.000-07:00 ]? 解析日期字符串,格式为[2012-07-15T20:55:33 + 00:00] - Parsing date string in Java of format [2012-07-15T20:55:33+00:00] DateTime("2019-08-07T19:20-5:00") 转换为具有偏移值的本地日期时间 - DateTime("2019-08-07T19:20-5:00") conversion to local date time with offset value 如何将“2020-12-20T00:00:00.000Z”转换为 java.util.Date? - How to convert “2020-12-20T00:00:00.000Z” to java.util.Date? java.time.Instant 无法从像 '2016-07-02T00:00:00Z' 这样的字符串解析 - java.time.Instant could not be parsed from string like '2016-07-02T00:00:00Z' 尝试使用SimpleDateFormat将字符串数字“ 7”转换为07:00格式 - Trying to convert a string number “7” to a 07:00 format with SimpleDateFormat 将 UTC 日期字符串(如“2022-07-07T08:17:12.117000”)转换为 Kotlin 和 Java 中的本地时区。 React 中可用的 Moment 库的替代方案 - Convert UTC date string like "2022-07-07T08:17:12.117000" to your local Timezone in Kotlin and Java. Alternative to Moment library available in react 将字符串转换为 java.time.ZonedDateTime - Convert String into java.time.ZonedDateTime 从字符串“ 2016-09-14T00:00:00.000-07:00 / 2016-09-15T00:00:00.000-07:00”确定时区 - Determine timezone from string “2016-09-14T00:00:00.000-07:00/2016-09-15T00:00:00.000-07:00”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM