aspose file tools
The moose likes Java in General and the fly likes DateFormat, Calendar, and timezone - parsing a String into a Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "DateFormat, Calendar, and timezone - parsing a String into a Calendar" Watch "DateFormat, Calendar, and timezone - parsing a String into a Calendar" New topic
Author

DateFormat, Calendar, and timezone - parsing a String into a Calendar

David Brossard
Ranch Hand

Joined: Jun 03, 2004
Posts: 107
Today I needed to parse a String of the following format into a date I could then compare. I chose to use java.util.Calendar since I was familiar with it. A bit of googling got me started.

String example: 2011-04-05T11:29:14Z
(this means 5th of April 2011 at 11:29AM Zulu time which is UTC which is the equivalent of 1:29PM CEST)

I started with the following format


That failed. The reason is simple and explained in the javadocs (http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html). You need to escape characters with single quotes. I updated my code to


That still failed for an altogether different reason. The fact I put Z at the end means the formatter expects to see a time zone indicator e.g. PST / GMT...

Being smart and fresh from the previous 'T' and single quote escaping, I decided to escape the 'Z'. Logical reasoning.

However, this had a negative consequence.

My code was now


This now meant that my date was in the default time zone as set by Calendar.getInstance() which happens to be Berlin/Germany for me (though I am miles and miles away from Berlin and not even in Germany). This actually changed my date by two hours.

This is very normal behavior by the Java lib but very confusing. Does it also mean there is no way to parse 2011-04-05T11:29:14Z directly? Do I really have to strip the Z and add GMT? My fixed code does the following:


No matter what they say in Ohio, we're still first in flight!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Adding "GMT" to the date string is a clumsy solution. The straightforward solution is to call the setTimeZone method on your DateFormat object and pass GMT to it.
David Brossard
Ranch Hand

Joined: Jun 03, 2004
Posts: 107
Yes you are quite right, thanks for pointing it out:

Code amendment:

David Brossard
Ranch Hand

Joined: Jun 03, 2004
Posts: 107
My code actually fails... It parses 2011-04-10T14:23:24Z into

java.util.GregorianCalendar[time=1302438204000
areFieldsSet=true
areAllFieldsSet=true
lenient=true
zone=sun.util.calendar.ZoneInfo[id="GMT"
offset=0
dstSavings=0
useDaylight=false
transitions=0
lastRule=null]
firstDayOfWeek=2
minimalDaysInFirstWeek=4
ERA=1
YEAR=2011
MONTH=3
WEEK_OF_YEAR=14
WEEK_OF_MONTH=1
DAY_OF_MONTH=10
DAY_OF_YEAR=100
DAY_OF_WEEK=1
DAY_OF_WEEK_IN_MONTH=2
AM_PM=1
HOUR=0
HOUR_OF_DAY=12
MINUTE=23
SECOND=24
MILLISECOND=0
ZONE_OFFSET=0
DST_OFFSET=0]

Why is it giving me a 4 hour offset?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Add right before calling the parse method. That's what Paul suggested - calling setTimeZone on the DateFormat instance, not the Calendar instance. You'll need both though, because if I omit setting the time zone on the Calendar instance I get 16:23:24 instead of 14:23:24.

The results:
- set no time zones: 18:23:24
- set only the Calendar time zone: 18:23:24
- set only the DateFormat time zone: 16:23:24
- set both time zones: 14:23:24


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: DateFormat, Calendar, and timezone - parsing a String into a Calendar
 
Similar Threads
Convert the String to date format
SimpledateFormat class issue.
About timezones!
converting local date to gmt date or gmt hours
Converting string to date with extra strings