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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…