Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8141243

Unexpected timezone returned after parsing a date

    XMLWordPrintable

Details

    • b96
    • generic
    • generic
    • Verified

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.7.0_76"
        Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
        Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux adnlt043 2.6.32-504.8.1.el6.x86_64 #1 SMP Fri Dec 19 12:09:25 EST 2014 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        When parsing the virtual timezone "UTC" with java.text.SimpleDateFormat, the timezone is set to the first timezone that matches an actual timezone in the UTC group, which is Antarctica/Troll. When comparing this timezone with the result of TimeZone.getTimeZone("UTC"), we fail.

        ADDITIONAL REGRESSION INFORMATION:
        The comparison worked in JDK 1.7.0_21 and older versions of JDK.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile and execute:

        public class TimeZoneTest {

        public static void main(String[] args) throws Exception {
        String dateString = "UTC";
        String formatString = "z";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
        Date date = simpleDateFormat.parse(dateString);

        TimeZone timeZone = TimeZone.getTimeZone("UTC");

        long now = System.currentTimeMillis();

        Calendar cal = new GregorianCalendar(simpleDateFormat.getTimeZone());
        cal.setTime(date);

        if (timeZone.getOffset(now) == cal.getTimeZone().getOffset(now)) {
        System.out.println("Works as expected");
        } else {
        System.err.println("The timezone parsed with the simple date format has an offset '" +
        cal.getTimeZone().getOffset(now) + "', but the timezone offset of TimeZone.getTimeZone('UTC') is '" + timeZone.getOffset(now) + "'");
        }
        System.out.println("TimeZone.getTimeZone('UTC') : " + TimeZone.getTimeZone("UTC"));
        System.out.println("cal.getTimeZone() : " + cal.getTimeZone());
        }
        }


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Works as expected
        TimeZone.getTimeZone('UTC') : sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
        cal.getTimeZone() : sun.util.calendar.ZoneInfo[id="Etc/UCT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
        ACTUAL -
        The timezone parsed with the simple date format has an offset '7200000', but the timezone offset of TimeZone.getTimeZone('UTC') is '0'
        TimeZone.getTimeZone('UTC') : sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
        cal.getTimeZone() : sun.util.calendar.ZoneInfo[id="Antarctica/Troll",offset=0,dstSavings=7200000,useDaylight=true,transitions=67,lastRule=java.util.SimpleTimeZone[id=Antarctica/Troll,offset=0,dstSavings=7200000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        public class TimeZoneTest {

        public static void main(String[] args) throws Exception {
        String dateString = "UTC";
        String formatString = "z";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
        Date date = simpleDateFormat.parse(dateString);

        TimeZone timeZone = TimeZone.getTimeZone("UTC");

        long now = System.currentTimeMillis();

        Calendar cal = new GregorianCalendar(simpleDateFormat.getTimeZone());
        cal.setTime(date);

        if (timeZone.getOffset(now) == cal.getTimeZone().getOffset(now)) {
        System.out.println("Works as expected");
        } else {
        System.err.println("The timezone parsed with the simple date format has an offset '" +
        cal.getTimeZone().getOffset(now) + "', but the timezone offset of TimeZone.getTimeZone('UTC') is '" + timeZone.getOffset(now) + "'");
        }
        System.out.println("TimeZone.getTimeZone('UTC') : " + TimeZone.getTimeZone("UTC"));
        System.out.println("cal.getTimeZone() : " + cal.getTimeZone());
        }
        }

        ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

                okutsu Masayoshi Okutsu
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: