Convert current GMT time into CST timezone using java

拜拜、爱过 提交于 2021-01-28 04:48:03

问题


I am trying to convert current GMT time to CST time using JDK8. But my code always returns the same time for both the timezones. I also verified getAvailableIDs() has "CST".

                Calendar cal = Calendar.getInstance();
            TimeZone timeZone = TimeZone.getTimeZone("GMT");
            cal.setTimeZone(timeZone);

            // System.out.println("GMT time = " + cal.getTime());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(timeZone);
            String gmtDateStr = sdf.format(cal.getTime());
            System.out.println("Formatted GMT time = " + gmtDateStr);

            // To CST
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TimeZone cst = TimeZone.getTimeZone("CST");
            sdf2.setTimeZone(cst);
            Date cstDate = sdf2.parse(gmtDateStr);

            // System.out.println("PARSED CST DATE = " + cstDate);

            Calendar cal2 = new GregorianCalendar();
            cal2.setTime(cstDate);
            cal2.setTimeZone(cst);
            String cstDateStr = sdf2.format(cal2.getTime());

            // System.out.println("cal2 time = " + cal2.getTime());
            System.out.println("FORMATTED CST DATE = " + cstDateStr);

What is wrong here? Can any one provide me an answer?


回答1:


You don't have to do all of this conversion to get time in your preferred timezone. You can simply do following..

        Calendar cal = Calendar.getInstance();
        TimeZone timeZone = TimeZone.getTimeZone("GMT");
        cal.setTimeZone(timeZone);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(timeZone);
        String gmtDateStr = sdf.format(cal.getTime());
        System.out.println("Formatted GMT time = " + gmtDateStr);

        // To CST
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone cst = TimeZone.getTimeZone("CST");
        sdf2.setTimeZone(cst);

        System.out.println("FORMATTED CST DATE = " + sdf2.format(cal.getTime()));



回答2:


Java 8 java.time doesn't find a zone identified by CST

ZoneId cst = ZoneId.of("CST");

results in java.time.zone.ZoneRulesException: Unknown time-zone ID: CST. This makes sense as there is Central Standard Time in North America, China Standard Time, and possibly other.

If you mean Central Standard Time in North America which is represented by America/Chicago:

ZoneId gmt = ZoneId.of("GMT");
ZoneId cst = ZoneId.of("America/Chicago");
ZonedDateTime gmtTime = ZonedDateTime.now(gmt);
ZonedDateTime cstTime = gmtTime.withZoneSameInstant(cst);



回答3:


What is wrong with your code is all those calls to Calendar.getTime(). This returns a Date instance, and those objects do not have the concept of a time zone. So it is useless to set the time zone in the Calendar instances because you then discard that info when converting to Date.

Just specify the time zone in your formatter, as in Yogesh Badke's answer.



来源:https://stackoverflow.com/questions/49532775/convert-current-gmt-time-into-cst-timezone-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!