在java中将UTC转换为IST时间在LOCAL中工作,但在CLOUD SERVER中不起作用

Vic*_*cky 5 java cloud timezone date server

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

代码链:

   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;
}
Run Code Online (Sandbox Code Playgroud)

And*_*eas 5

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

因此,转换utcDate为的整个概念ISTDate是有缺陷的。
(顺便说一句:坏名字。Java 约定说它应该是istDate

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

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);
Run Code Online (Sandbox Code Playgroud)