在 Java 或 Groovy 中转换 UTC 时间 T0 本地时间 [英] Convert UTC Time T0 Local Time In Java or Groovy

查看:26
本文介绍了在 Java 或 Groovy 中转换 UTC 时间 T0 本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储 createdOn (域类中的属性之一).我正在获取系统时间并存储此属性的值.. 我的时区是 (GMT+5:30 Chennai, Kolkata, Mumbai, New Delhi) 当我上传到它存储 UTC 时间的服务器时.我希望它是 IST(印度标准时间)我的应用程序在 Grails 上使用 Groovy.请帮我调整UTC/IST时差.提前致谢

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

推荐答案

不,不要那样做.永远!

No, don't do that. Ever!

如果您以本地形式存储时间,您将陷入痛苦的世界.您基本上必须同时存储本地时间和本地时区,然后时间的显示就变成了一个复杂的野兽(计算出源时区和目标时区).

If you store times 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).

所有时间都应存储为 UTC.没有例外.用户输入的时间应转换为UTC,然后再写入任何地方(尽快).

All times should be stored as UTC. No exception. Times entered by a user should be converted to UTC before being written anywhere (as soon as possible).

显示给用户的时间应尽可能晚地从 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 时间,就可以使用 SimpleDateFormat 类来转换它:

Once you have the UTC time, it's a matter of using the SimpleDateFormat class to convert it:

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));
    }
}

输出:

Time in IST is 2011-04-11 13:40:04

这与 Mirzapur 中的当前时间一致,我认为是 IST 的所在地(目前在印度并不重要,因为它只有一个时区).

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).

这篇关于在 Java 或 Groovy 中转换 UTC 时间 T0 本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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