在java中将UTC转换为IST时间在LOCAL中工作,但在CLOUD 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;
}
最佳答案
Java Date对象已经/始终为UTC.时区是格式化为文本时应用的内容.日期不能(不应该!)在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);

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...