How to set time zone of a java.util.Date?

Yatendra Goel picture Yatendra Goel · May 23, 2010 · Viewed 504.8k times · Source

I have parsed a java.util.Date from a String but it is setting the local time zone as the time zone of the date object.

The time zone is not specified in the String from which Date is parsed. I want to set a specific time zone of the date object.

How can I do that?

Answer

ZZ Coder picture ZZ Coder · May 23, 2010

Use DateFormat. For example,

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");