TimeZone problem in Java

java, timezone

Solution

I'm not sure if this answers your question, but this is one way to get "now" in GMT.

import java.text.*
import java.util.* 

Calendar cal = new GregorianCalendar();
Date date = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(formatter.format(date));

See the Javadoc on SimpleDateFormat for different patterns. Also, you may want to consider Joda Time as it is far superior for dates and times.

Problem

I am trying to instantiate GregorianCalendar with TimeZone GMT, but whenever I call the getTime() method, it gives me time in local TimeZone. Here is my code: ``` Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); System.out.println(cal.getTime()); ``` The output I am getting is this: ``` Sat Nov 28 19:55:49 PKT 2009 ``` Please help!

Original source