Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.util.Calender and Date showing wrong time offsets

843811Apr 5 2005 — edited Apr 6 2005
System: Windows XP
Java version: 1.4.2
TimeZone: US Central Time
When Happend: From April 3, 2005
The system time is changed to GMT-5 HRS, but running Java program with Calender and Date instances shows: GMT-6 HRS!!

I have a small test class below which should show the following values but actual values displayed when the program is run, are in parenthesis. The system time of the computer is showing correct time though.

4-4-2005 (shows 4-4-2005)
11:35:57 (shows 10:35:57)
Calender OFFSET is: -6 (shows -6)
Calender DayLight Savings offset is:1 (shows 0 (zero))
-----------------

Date: 5 Apr 16:35:57 GMT (shows 5 Apr 16:35:57 GMT)
offset: 5 (shows 6)

DateCurrent.toString() Mon Apr 04 11:35:57 CDT 2005 (shows Mon Apr 04 10:35:57 CDT 2005)

DateCurrent.TimeZoneOffset() 5 (shows 6)

I would really appreciate it if anyone can help me with this. Thanks very much in advance.

dd007

import java.io.*;
import java.util.*;
public class CheckTime {
	public CheckTime(){
		Calendar c = Calendar.getInstance();
		Date DateCurrent = new Date(c.getTimeInMillis());
		System.out.println( 
				(c.get(Calendar.MONTH) + 1) + "-" + 
				(c.get(Calendar.DAY_OF_MONTH)) + "-" +
				(c.get(Calendar.YEAR)) + "\n" +
				(c.get(Calendar.HOUR_OF_DAY)) + ":" +
				(c.get(Calendar.MINUTE)) + ":" +
				(c.get(Calendar.SECOND)) );
		System.out.println(" Calender OFFSET is:"+ ((c.get(Calendar.ZONE_OFFSET)/3600000) % 24));
		System.out.println(" Calender Daylight Savings offset is:"+ ((c.get(Calendar.DST_OFFSET)/3600000) % 24));
		System.out.println("---------------\n\nDate: " + DateCurrent.toGMTString() +"\n offset: " +
					(DateCurrent.getTime()- Date.UTC(DateCurrent.getYear(),DateCurrent.getMonth(),
					DateCurrent.getDate(),DateCurrent.getHours(),DateCurrent.getMinutes(),
					DateCurrent.getSeconds()) )/(60*60 * 1000));
		System.out.println("\n DateCurrent.toString(): "+ DateCurrent.toString());
		System.out.println("\n DateCurrent.TimeZoneOffset(): "+ (DateCurrent.getTimezoneOffset()/60) );

		
	}

	public static void main(String[] args){
		System.out.println("Checking System time: \n");
		CheckTime ct = new CheckTime();
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 4 2005
Added on Apr 5 2005
1 comment
278 views