在 Java 或 Groovy 中转换 UTC 时间 T0 本地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5617459/
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 UTC Time T0 Local Time In Java or Groovy
提问by maaz
i need to store createdOn (One Of the Attribute in Domain Class) . i am getting the system time and storing the value for this attribute.. My Time zone is (GMT+5:30 Chennai, Kolkata,Mumbai, New Delhi ) when i upload to the server it stores UTC time . I want it to be IST (Indian Standard Time) My Application Uses Groovy on Grails. Please Help me to adjust UTC /IST Time Difference. Thanks In advance
我需要存储 createdOn (域类中的属性之一)。我正在获取系统时间并存储此属性的值.. 我的时区是 (GMT+5:30 Chennai, Kolkata, Mumbai, New Delhi) 当我上传到它存储 UTC 时间的服务器时。我希望它是 IST(印度标准时间)我的应用程序在 Grails 上使用 Groovy。请帮我调整UTC /IST时差。提前致谢
回答by paxdiablo
No, don't do that. Ever!
不,不要那样做。曾经!
If you storetimes in local form, you're in for a world of pain. You basically have to store both the local time and the local timezone and the display of the time then becomes a complex beast (working out the source and target timezones).
如果您以本地形式存储时间,您将陷入痛苦的世界。您基本上必须同时存储本地时间和本地时区,然后时间的显示就变成了一个复杂的野兽(计算出源时区和目标时区)。
All times should be storedas UTC. No exception. Times entered by a user should be converted to UTC before being written anywhere (as soon as possible).
所有时间都应存储为 UTC。没有例外。用户输入的时间应在写入任何地方之前(尽快)转换为 UTC。
Times to be shown to a user should be converted from UTC to local as late as possible.
向用户显示的时间应尽可能晚地从 UTC 转换为本地时间。
Take this advice from someone who once got bogged down in the multi-timezone quagmire. Using UTC and converting only when necessary will make your life a lot easier.
从曾经陷入多时区泥潭的人那里得到这个建议。使用 UTC 并仅在必要时进行转换将使您的生活更轻松。
Once you have the UTC time, it's a matter of using the SimpleDateFormat
class to convert it:
获得 UTC 时间后,就可以使用SimpleDateFormat
类来转换它了:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class scratch {
public static void main (String args[]) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone (TimeZone.getTimeZone ("IST"));
System.out.println ("Time in IST is " + sdf.format (now));
}
}
This outputs:
这输出:
Time in IST is 2011-04-11 13:40:04
which concurs with the current time in Mirzapur, which I think is where IST is based (not that it matters in India at the moment since it only has one timezone).
这与Mirzapur的当前时间一致,我认为这是 IST 的所在地(目前在印度并不重要,因为它只有一个时区)。
回答by Dead Programmer
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sf.format(new Date()));
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sf1.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(sf1.format(new Date()));
System.out.println(Arrays.asList(TimeZone.getDefault()));