[Q#28333776][A#28333829] How to parse time in correct timezone java?
https://stackoverflow.com/q/28333776
I am parsing a time stamp which is a string as follows: The date gets parsed correctly but when I print the date, it prints the following: How do I get it to print GMT instead of EST? Why is the timezone not getting set despite me setting it using the following?
Answer
https://stackoverflow.com/a/28333829
A java.util.Date object has no timezone information. It's a fixed instant in time. Dumping to the console just uses Date.toString(); which uses the JVM's timezone to perform the format. If you want to convert your fixed instant in time back to a 'human' representation in a particular timezone, just send it back through your formatter the other way.
APIzation
System.out.println(formatter.format(receivedDateObj));
package com.stackoverflow.api;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Human28333829 {
public static String formatter_format(
SimpleDateFormat formatter,
Date receivedDateObj
) {
return formatter.format(receivedDateObj);
}
}
package com.stackoverflow.api;
/**
* How to parse time in correct timezone java?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/28333829">https://stackoverflow.com/a/28333829</a>
*/
public class APIzator28333829 {
public static String parseTime(String receivedDateObj, String formatter)
throws Exception {
return formatter.format(receivedDateObj);
}
}