United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4844441 In Calendar, the order of calls to setTimeZone() and setTime() affects getTime()
4844441 : In Calendar, the order of calls to setTimeZone() and setTime() affects getTime()

Details
Type:
Bug
Submit Date:
2003-04-07
Status:
Closed
Updated Date:
2003-04-23
Project Name:
JDK
Resolved Date:
2003-04-23
Component:
core-libs
OS:
linux
Sub-Component:
java.util:i18n
CPU:
x86
Priority:
P4
Resolution:
Duplicate
Affected Versions:
1.4.1
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

Description

Name: jl125535			Date: 04/07/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION :

glibc-2.2-12

Linux walrus.sockeye.com 2.2.19-7.0.1 #1 Tue Apr 10
01:56:16 EDT 2001 i686 unknown

Red Hat Linux release 7.0 (Guinness)


A DESCRIPTION OF THE PROBLEM :

If you change some Calendar fields by calling set(),
the day will be off by one if you had called setTime()
*before* calling setTimeZone().  If you call setTimeZone()
as the first thing, there's no problem.

This bug only happens for time zones east of GMT (or at
least Europe/Paris).



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Just reverse the two lines of code as the source code
says.
2.
3.

EXPECTED VERSUS ACTUAL BEHAVIOR :
expected:

before: 07 Nov 2002 01:00 CET
after: 07 Nov 2002 00:00 CET

After reversing the two lines noted, the following
incorrect output is generated:

before: 07 Nov 2002 01:00 CET
after: 06 Nov 2002 00:00 CET

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/*

This program normalizes a date to midnight in the CET time zone.
The expected output is:

before: 07 Nov 2002 01:00 CET
after: 07 Nov 2002 00:00 CET

After reversing the two lines noted, the following
incorrect output is generated:

before: 07 Nov 2002 01:00 CET
after: 06 Nov 2002 00:00 CET
 */
public class Bug {

    public static void main(String[] args) {
        TimeZone tz = TimeZone.getTimeZone("Europe/Paris");

        Date date = new Date(1036627200000L);

        Calendar cal = Calendar.getInstance();

        // Reversing the following two lines causes the
        // subsequent code to compute the wrong day (Nov 6 when
        // it should be Nov 7).
        cal.setTimeZone(tz);
        cal.setTime(date);

        SimpleDateFormat formatter = 
            new SimpleDateFormat("dd MMM yyyy HH:mm zzz");
        formatter.setTimeZone(tz);

        System.out.println("before: " + formatter.format(cal.getTime()));

        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        System.out.println("after: " + formatter.format(cal.getTime()));
    }
}

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

CUSTOMER WORKAROUND :
Make sure you call setTimeZone() before calling
setTime().
(Review ID: 166807) 
======================================================================

                                    

Comments
EVALUATION

Calendar documentation should clarify the setTimeZone behavior. Closing this bug report as a duplicate of 4827490.
###@###.### 2003-04-23
                                     
2003-04-23



Hardware and Software, Engineered to Work Together