将给定时间转换为 GMT
我曾尝试将给定日期 Mon Jul 04 00:00:00 IST 2011
转换为 GMT,如下所示:2011-07-04 18:10:47 GMT+00:00 2011< /code> 但它显示
3/7/11 6:30 PM
这是我的代码:
java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat res_format = new SimpleDateFormat("dd/mm/yyyy HH:mm");
java.util.Date date1 = format.parse("2011-07-04 00:00:00");
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Current Time: "+date1);
System.out.println("Time:"+gmtFormat.format(date1));
I had tried to convert the given date Mon Jul 04 00:00:00 IST 2011
to GMT like this: 2011-07-04 18:10:47 GMT+00:00 2011
but it displays 3/7/11 6:30 PM
This is my code:
java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat res_format = new SimpleDateFormat("dd/mm/yyyy HH:mm");
java.util.Date date1 = format.parse("2011-07-04 00:00:00");
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT+00");
gmtFormat.setTimeZone(gmtTime);
System.out.println("Current Time: "+date1);
System.out.println("Time:"+gmtFormat.format(date1));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
This works for me:
对代码进行最小更改的快速解决方案是将: 替换
为:
Quick solution with minimal change in your code is to replace:
To:
你的想法是正确的。如果您费心再次查看代码,您就会知道自己做错了什么。
你的代码工作正常。但是您没有看到您想要的内容,因为您(很可能)已经
对输出
进行了初始化,并且您使用全新的
SimpleDateFormat
,突然设置时区和显示。如果您更改
为
您会得到您想要的。
(还要注意格式的变化。它是
dd/MM/yyyy
而不是dd/mm/yyyy
)您的代码会更容易阅读(对于您和其他人)如果您已对块进行了逻辑分组,并且没有对(几乎)所有类使用完全限定名称
Your idea is correct. If you had bothered to look into the code once more, you would have known what you have done wrong.
Your code is working fine. But you don't see what you want because you have (most likely) initialized
for the output
and you use a brand new
SimpleDateFormat
, out of the blue to set the timezone and display.If you change
to
You get what you want.
(Note the change in the format as well. It is
dd/MM/yyyy
and notdd/mm/yyyy
)You code would have been a lot more easier to read (for you, and others) if you had logically grouped the blocks, and not used fully qualified names for (almost) all classes