在Java中将UTC转换为IST时间在LOCAL中有效,但在CLOUD SERVER中则无效 [英] Conversion of UTC to IST time in java is working in LOCAL but not in CLOUD SERVER

查看:93
本文介绍了在Java中将UTC转换为IST时间在LOCAL中有效,但在CLOUD SERVER中则无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java进行日期转换,因为我正在使用以下代码片段将UTC时间转换为IST格式。当我在本地运行它但在服务器中部署它而不转换时,它在本地正常工作,它只显示utc时间本身。服务器端是否需要任何配置。请帮帮我。

I am working in date conversion in java in that i am using following code snippet to convert the UTC time to IST format.It is working properly in the local when i run it but when i deploy it in server its not converting , its displaying only the utc time itself.Is there any configuaration is needed in server side.Please help me out.

代码片段:

   DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    String pattern = "dd-MM-yyyy HH:mm:ss";
    SimpleDateFormat formatter;
    formatter = new SimpleDateFormat(pattern);

    try {
        String formattedDate = formatter.format(utcDate);
        Date ISTDate = sdf.parse(formattedDate);
String ISTDateString = formatter.format(ISTDate);
            return ISTDateString;
}


推荐答案

Java Date 对象已经/总是在UTC中。时区是在格式化文本时应用的内容。 Date 不能(应该!)不能位于UTC以外的任何时区。

Java Date objects are already/always in UTC. Time Zone is something that is applied when formatting to text. A Date cannot (should not!) be in any time zone other than UTC.

因此,整个概念将 utcDate 转换为 ISTDate 是有缺陷的。

(BTW:不好的名字。Java约定说现在应该是 istDate

So, the entire concept of converting utcDate to ISTDate is flawed.
(BTW: Bad name. Java conventions says it should be istDate)

现在,如果您希望代码将日期返回为IST时区中的文本,那么您需要请求:

Now, if you want the code to return the date as text in IST time zone, then you need to request that:

DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // Or whatever IST is supposed to be
return formatter.format(utcDate);

这篇关于在Java中将UTC转换为IST时间在LOCAL中有效,但在CLOUD SERVER中则无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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