I’m trying to parse a String that represents a date using GMT, but it prints out in my timezone on my PC (pacific). When I run the below I get the below output. Any ideas on how to get the parse to parse and return a GMT date? If you look below I’m setting the timezone using format.setTimeZone(TimeZone.getTimeZone(“GMT”)); but its not producing the desired result.
output from below code:
Mon Oct 29 05:57:00 PDT 2012
package javaapplication1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class JavaApplication1 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.parse("2012-10-29T12:57:00-0000"));
}
}
You are using
format.setTimeZone(TimeZone.getTimeZone("GMT"));in the formatter, which is being used in formatting the string into date i.e.is parsed treating
2012-10-29T12:57:00-0000was aGMTvalue, but you are printingdatewhich uses local timezome in printing hence you are noticing the difference.If you want to print the date back in
GMT, please use:and print the
formattedDate. This will beGMT.