aspose file tools*
The moose likes Java in General and the fly likes Puzzling Parsing of Dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Puzzling Parsing of Dates " Watch "Puzzling Parsing of Dates " New topic
Author

Puzzling Parsing of Dates

raja patil
Greenhorn

Joined: May 26, 2008
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
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

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?



SCJP 6 || SCWCD 5
raja patil
Greenhorn

Joined: May 26, 2008
Posts: 24
Thanks Michael Angstadt,

Here is Out put

regards

Raja
sai prashanth
Greenhorn

Joined: May 04, 2010
Posts: 18
I tried to run your code and this is my output. I dont see anything wierd here:
raja patil
Greenhorn

Joined: May 26, 2008
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

Joined: May 26, 2008
Posts: 24
hello,

I just found this http://72.5.124.102/thread.jspa?messageID=10811150 which explains things about IST timezone
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

Joined: Sep 29, 2008
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

Joined: May 26, 2008
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Puzzling Parsing of Dates