java 转换 GMT 日期时间字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6730797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert GMT DateTime String
提问by Marco
I am pretty new to Java and I am a little stuck with using SimpleDateFormat
and Calendar
. I have a Date-Object and want to extract a GMT datestring like yyyy-MM-dd HH:mm:ss
. I live in Germany and at the moment we are GMT +0200. My Date-Object's time is for example 2011-07-18 13:00:00
. What I need now is 2011-07-18 11:00:00
. The offset for my timezone should be calculated automatically.
我对 Java 还很陌生,我对使用SimpleDateFormat
and有点卡住了Calendar
。我有一个日期对象,想提取一个 GMT 日期字符串,如yyyy-MM-dd HH:mm:ss
. 我住在德国,目前我们是 GMT +0200。我的日期对象的时间是例如2011-07-18 13:00:00
。我现在需要的是2011-07-18 11:00:00
. 我的时区的偏移量应该自动计算。
I tried something like this, but I guess there is a fault somewhere:
我试过这样的事情,但我想某处有问题:
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timeZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(new SimpleTimeZone(timeZone.getOffset(date.getTime()), "GMT"));
sd.setCalendar(cal);
return sd.format(date);
}
On some devices the datestring is returned like I want it to. On other devices the offset isn't calculated right and I receive the date and time from the input date-object. Can you give me some tips or advices? I guess my way off getting the default timezone does not work?
在某些设备上,日期字符串会像我想要的那样返回。在其他设备上,偏移量计算不正确,我从输入日期对象接收日期和时间。你能给我一些提示或建议吗?我想我获得默认时区的方法不起作用?
采纳答案by Eli Acherkan
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sd.setTimeZone(TimeZone.getTimeZone("GMT"));
return sd.format(date);
}
You don't need to create a new SimpleTimeZone
, because you aren't inventing a new timezone - there are 2 existing timezones that come into play in your program, GMT and your default one.
您不需要创建SimpleTimeZone
新时区,因为您不是在发明新时区 - 程序中有 2 个现有时区,GMT 和默认时区。
You also don't need to modify your existing date object, because you don't want to represent a different point in time - you only want a different way to display the same point in time.
您也不需要修改现有的日期对象,因为您不想表示不同的时间点 - 您只想要一种不同的方式来显示相同的时间点。
All you need to do is tell the SimpleDateFormat
which timezone to use in formatting.
您需要做的就是告诉SimpleDateFormat
在格式化中使用哪个时区。
回答by Jigar Joshi
private String toGmtString(Date date){
//date formatter
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//getting default timeZone
TimeZone timeZone = TimeZone.getDefault();
//getting current time
Calendar cal = Calendar.getInstance()
cal.setTime(date) ;
//adding / substracting curren't timezone's offset
cal.add(Calendar.MILLISECOND, -1 * timeZone.getRawOffset());
//formatting and returning string of date
return sd.format(cal.getTime());
}