【发布时间】:2017-08-29 18:06:57
【问题描述】:
我一直在寻找将时间从 GMT 转换为本地时间的最简单方法。当然,要考虑正确的 DST 日期并尽可能标准。
我能想到的最直接的代码是:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String inpt = "2011-23-03 16:40:44";
Date inptdate = null;
try {
inptdate = sdf.parse(inpt);
} catch (ParseException e) {e.printStackTrace();}
Calendar tgmt = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
tgmt.setTime(inptdate);
Calendar tmad = new GregorianCalendar(TimeZone.getTimeZone("Europe/Madrid"));
tmad.setTime(inptdate);
System.out.println("GMT:\t\t" + sdf.format(tgmt.getTime()));
System.out.println("Europe/Madrid:\t" + sdf.format(tmad.getTime()));
但我认为我没有正确理解 getTime 将返回的内容。
【问题讨论】:
-
你如何让
TimeZone.getTimeZone("Europe/Madrid")工作?我收到错误The method getTimeZone(String) is undefined for the type TimeZone -
没什么花哨的...这是一个 sn-p 实际工作代码:pastebin.com/5u79mBMW 每天在 Java 1.5 上运行几次。
-
抱歉,写这篇文章后我发现我使用的是 GWT 版本的 TimeZone,并且它不允许字符串作为参数!
-
Timezone conversion的可能重复
-
所以你的日期是 2011 年第 23 个月的第 3 天??你打算
2011-03-23 16:40:44吗?