I have written following code. I want to get Date object in UTC format.
I am able to get expected date string in UTC using SimpleDateFormat
. But using same SimpleDateFormat
object, I am not able to get object in UTC format. It is returning object with IST format.
After searching, I found that Date object doesn't store timestamp info.
How can I get date object in UTC format ?
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class dddd {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Input - "+1393572325000L);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date= new Date(1393572325000L);
String dateString = formatter.format(date);
System.out.println("Converted UTC TIME (using Format method) : "+dateString);
Date date2 =null;
try {
date2 = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Parsed Date Object (using Parse method) : "+date2);
System.out.println("Expected Date Object : Fri Feb 28 07:25:25 UTC 2014");
}
}
This prints following output :
Input - 1393572325000
Converted UTC TIME (using Format method) : 2014-02-28 07:25:25
Parsed Date Object (using Parse method) : Fri Feb 28 12:55:25 IST 2014
Expected Date Object : Fri Feb 28 07:25:25 UTC 2014
question from:
https://stackoverflow.com/questions/22091107/get-date-object-in-utc-format-in-java 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…