Skip to Main Content

Hi, I am working with TimeZones in Java. Recently I got an issue in Java.

User_31RFZDec 19 2022 — edited May 5 2023

That is for timezone "America/Chihuahua" Java is giving current offset as -07:00 but the current offset for that timezone -06:00.
Can someone help me with this?
Code:

        SimpleDateFormat FORMATTER = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mma z");
        // In Default Timezone
        Date currentDate = new Date();
        DateTime currentDateTime = new DateTime();

        // Date in current timezone
        System.out.println(FORMATTER.format(currentDate));

        // In UTC Timezone
        TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
        FORMATTER.setTimeZone(utcTimeZone);
        String sDateInUTC = FORMATTER.format(currentDate);
        System.out.println(sDateInUTC);
        // In America/Chihuahua Timezone
        TimeZone tz = TimeZone.getTimeZone("America/Chihuahua");
        FORMATTER.setTimeZone(tz);
        String sDateInChihuahua = FORMATTER.format(currentDate);
        System.out.println(sDateInChihua);

Output:
Screenshot from 2022-12-19 19-30-20.pngScreenshot from 2022-12-19 19-24-51.png

Comments
Post Details
Added on Dec 19 2022
2 comments
97 views