In java I need to make a Calendar object from a String in the format:
yyyy-MM-dd'T'HH:mm:ss
This string will always be set as GMT time. So here’s my code:
public static Calendar dateDecode(String dateString) throws ParseException
{
TimeZone t = TimeZone.getTimeZone("GMT");
Calendar cal = Calendar.getInstance(t);
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date d = date.parse(dateString);
cal.setTime(d);
return cal;
}
And then:
Calendar cal = Calendar.getInstance();
try
{
cal = dateDecode("2002-05-30T09:30:10");
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
int month = cal.get(Calendar.MONTH)+1;
And I get the following output:
Timezone: GMT+00:00 date: 2002-5-30 time: 7:30:10
Which is as you can see wrong since the time provided is in GMT and not CET. I think what happens is that it think the time provided is in CET (which is my current timezone) and therefore converts the time from CET to GMT and therefore deducts two hours from the final result.
Could anyone help me with this?
Thanks
Btw: I do not wish to use JodaTime for different reasons.
Here is some code that could help you out with setting the timzones before parsing them:
another similar question I found might help too: How to set default time zone in Java and control the way date are stored on DB?
EDIT:
Here is a great tutorial on Java & Dates too: http://www.tutorialspoint.com/java/java_date_time.htm