Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert "Tue Feb 27 2024 16:35:30 GMT+0800" String to ZonedDateTime type

I want to convert "Tue Feb 27 2024 16:35:30 GMT+0800" String to ZonedDateTime type

I use new ZonedDateTime.ofInstant(new Date("Tue Feb 27 2024 16:35:30 GMT+0800").toInstant(), ZoneId.of("Asia/Shanghai"))

but new Date() is Deprecated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

How about using DateTimeFormatter and trying this?

 public static void main(String[] args) {
        String dateString = "Tue Feb 27 2024 16:35:30 GMT+0800";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateString, formatter);
        ZonedDateTime shanghaiTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
        System.out.println("Parsed ZonedDateTime: " + shanghaiTime);
    }
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading