如何设置Date对象指向java中的不同时区 [英] How to set Date object to point to different time zone in java

查看:183
本文介绍了如何设置Date对象指向java中的不同时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我使用日历对象将时区初始化为GMT,并相应地获取时间,但是当我放回日期对象时,它会自动转换到本地时区,即IST。日历gmt = new GregorianCalendar(TimeZone.getTimeZone(GMT));

In the code below I have used calendar object to initialize time Zone to GMT and get the time accordingly but when I put back in date object, its automatically converting to my local time zone i.e. IST.

Calendar gmt = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
Date dt=gmt.getTime();

任何人都可以提出一种方法,通过这种方式我可以保留日期对象中的GMT格式。 >

Could anyone suggest a way through which I can retain the GMT format in date object also.

推荐答案


它自动转换到我的本地时区,即IST

its automatically converting to my local time zone i.e. IST

不是没有。一个日期对象没有时区 - 这只是一个即时的时间。

No it's not. A Date object doesn't have a time zone - it's just an instant in time.

你会看到系统本地时区,如果您在日期上调用 toString(),因为不幸的是,$ code> Date.toString()做...但是时区不是存储在 Date 中的信息的一部分。

You'll see the system-local time zone if you call toString() on a Date, because that's unfortunately what Date.toString() does... but the time zone is not part of the information stored in a Date.

如果要在特定时区看到 Date 的文本表示,请使用 DateFormat 并设置要使用的时区。

If you want to see a textual representation of a Date in a particular time zone, use DateFormat and set the time zone that you want to use.

这篇关于如何设置Date对象指向java中的不同时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆