Achaius
Achaius

Reputation: 6124

Convert current GMT time into CST timezone using java

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?

Upvotes: 3

Views: 11628

Answers (3)

gpeche
gpeche

Reputation: 22514

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.

Upvotes: 2

Karol Dowbecki
Karol Dowbecki

Reputation: 44962

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);

Upvotes: 3

Yogesh Badke
Yogesh Badke

Reputation: 4587

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()));

Upvotes: 4

Related Questions