Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Puzzling Parsing of Dates

raja patil
Greenhorn
Posts: 24
hello

I am new to Java and developing First Java Application. Please refer following code and the output.

System is Kubuntu 9.10 JDK 1.6 Repository Install, STS 2.3.2.

public static void main(String[] args) {

// Mon Jan 01 05:53:20 IST 1900
SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
TimeZone tz = TimeZone.getDefault();

System.out.println("RawOffset = " + tz.getRawOffset() + " useDaylightTime = " + tz.useDaylightTime());
try {
Date dt1 = fmt.parse("Mon Jan 01 05:30:00 IST 1900");
Date dt2 = fmt.parse("Mon Jan 01 05:29:59 IST 1900");

System.out.println("date = " + dt1 + " offset = " + tz.getOffset(dt1.getTime()) + " getTime = "
+ dt1.getTime());
System.out.println("date = " + dt2 + " offset = " + tz.getOffset(dt2.getTime()) + " getTime = "
+ dt2.getTime());
}
catch (ParseException e) {
e.printStackTrace();
}
}
}

RawOffset = 19800000 useDaylightTime = false
date = Mon Jan 01 05:53:20 IST 1900 offset = 21200000 getTime = -2208988800000
date = Mon Jan 01 05:29:59 IST 1900 offset = 19800000 getTime = -2208988801000

I am confused why diff of 1 sec at threshold of timezone offset of 05:30 is converting date to 05:53:20 when
the raw date millis have changed correctly by 1 sec. You can see that offset has also shifted by 23:20 so
addition of 23:20 can be understood but why should it happen ? How can I get correct time of 05:30 if its
default behavior.

Thanks and Best Regards.

Raja

Ranch Hand
Posts: 277
Hi raja, welcome to JavaRanch! Please UseCodeTags, as it makes your code easier to read and fix.

It seems like it's not parsing your first Date correctly. What happens when you manually pull the minute value out of the Date?

raja patil
Greenhorn
Posts: 24

Here is Out put

regards

Raja

sai prashanth
Greenhorn
Posts: 18
I tried to run your code and this is my output. I dont see anything wierd here:

raja patil
Greenhorn
Posts: 24
sai prashanth wrote:I tried to run your code and this is my output. I dont see anything wierd here:

Thanks Sai prashanth have you tried with IST (india not Irish i.e. GMT+0530) rather than CST timezone ?
Please noticed offset values 19800000 and 21200000 that is causing problem I believe.
another thing is that from 05:29:59 to 05:30:00 there is jump of 00:23:20 and after that
it increments linearly with input value.

Thanks and regards

Raja

raja patil
Greenhorn
Posts: 24
hello,

is really java gets confused with 3 letter timezone ? If yes then what shout I use so that java wont get confused.

Thanks and regards

Raja

David Newton
Author
Rancher
Posts: 12617
http://java.sun.com/j2se/1.5.0/docs/api/java/util/TimeZone.html

See specifically the section on three-letter timezone names.

raja patil
Greenhorn
Posts: 24
Thanks to all who contributed to this thread,

It Seems that there is No problem with IST. The history of IST (Indian Standard Time) Like that only.
Before 1900 there was no definition of IST.
initially IST was using Madras (Chennai) Time then Kolkata time then after Independence in 1947
its using Ujain i.e. Meridian Passing through almost centre of the state so there are three different
offsets the listing below clearly indicates that. During analysis of this issue I learnt that timezones
are fixed by Politicians and not by scientists thats why there are many such issues to be dealt with
by programmers by simply setting time zone to GMT+0530 all the date time issues get vanished.

with TimeZone "GMT+0530"

Thanks and best regards

Raja