aspose file tools*
The moose likes Java in General and the fly likes java date/timezone odd behavior? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java date/timezone odd behavior?" Watch "java date/timezone odd behavior?" New topic
Author

java date/timezone odd behavior?

gigi sheh
Greenhorn

Joined: Oct 15, 2012
Posts: 10
Hello,

we have a web application that always takes the input of time in its UTC format as

2012-12-06T05:00:00.000Z


and here is the code that parse the date into a java util Date object

private static final Pattern PATTERN = Pattern.compile(
"(\\d{4})(?:-(\\d{2}))?(?:-(\\d{2}))?(?:[Tt](?:(\\d{2}))?(?::(\\d{2}))?(?::(\\d{2}))?(?:\\.(\\d{3}))?)?([Zz])?(?:([+-])(\\d{2}):(\\d{2}))?");


Matcher m = PATTERN.matcher(dateString);
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
int hoff = 0, moff = 0, doff = -1;
if (m.group(9) != null) {
doff = m.group(9).equals("-") ? 1 : -1;
hoff = doff * (m.group(10) != null ? Integer.parseInt(m.group(10)) : 0);
moff = doff * (m.group(11) != null ? Integer.parseInt(m.group(11)) : 0);
}
c.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
c.set(Calendar.MONTH, m.group(2) != null ? Integer.parseInt(m.group(2))-1 : 0);
c.set(Calendar.DATE, m.group(3) != null ? Integer.parseInt(m.group(3)) : 1);
c.set(Calendar.HOUR_OF_DAY, m.group(4) != null ? Integer.parseInt(m.group(4)) + hoff: 0);
c.set(Calendar.MINUTE, m.group(5) != null ? Integer.parseInt(m.group(5)) + moff: 0);
c.set(Calendar.SECOND, m.group(6) != null ? Integer.parseInt(m.group(6)) : 0);
c.set(Calendar.MILLISECOND, m.group(7) != null ? Integer.parseInt(m.group(7)) : 0);
return c.getTime();


Recently an odd thing was observed that as the application first starts, the returned date will be just correctly printed as
Thur Dec 06 00:00:00 EST 2012

since we are in EST timezone. Then after a while, after some execution, even without restart the application, the same date would be printed as
Thur Dec 06 05:00:00 UTC 2012

I have been digging down in the application and I don't see any changes that would reset the default timezone of our application. How could that happen? It has been a week since we started working on this and we are still clueless :-(

Also, is there anyway to make sure the application keeps using the system timezone as that would not be changing?


thanks a lot for any help/hints
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

gigi sheh wrote:
and here is the code that parse the date into a java util Date object

private static final Pattern PATTERN = Pattern.compile(
"(\\d{4})(?:-(\\d{2}))?(?:-(\\d{2}))?(?:[Tt](?:(\\d{2}))?(?::(\\d{2}))?(?::(\\d{2}))?(?:\\.(\\d{3}))?)?([Zz])?(?:([+-])(\\d{2}):(\\d{2}))?");


Don't do that. Use java.text.SimpleDateFormat.parse().
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
gigi sheh wrote:Also, is there anyway to make sure the application keeps using the system timezone as that would not be changing?

You don't show how the date is actually printed. My guess is you're ultimately relying on Date's toString() method, which does use the system time zone - and that system time zone is changing for some reason. I say, forget the system time zone, get a TimeZone object for whatever time zone you want to use, and use that with a DateFormat to print Dates using that specific time zone.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

in Java, Date is a four letter word. The API is badly designed and confusing.

The key thing to know (other than using SimpleDateFormat, rather than that pattern), is that a Date does not have a timezone and it doesn't have a locale.
The format has the timezone and/or locale.

Specify the desired timezone and/or locale in the format, and all will be peaches and cream.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java date/timezone odd behavior?