1
    protected static Date getZonedTime(Date time,ZoneId toZoneId) {
        
        System.out.println("***********************************************************************************");
        ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(time.getTime()), ZoneId.of("UTC"));
        System.out.println(zdt);
        Date date = new Date(zdt.withZoneSameInstant(ZoneId.of("Asia/Kolkata")).toInstant().toEpochMilli());
        System.out.println(date);
        System.out.println("***********************************************************************************");
        return date;
    }
  • 为什么这段代码在本地机器和谷歌云功能中运行时表现不同?

代码的目标是将时间转换为客户端(http 请求)时区(时间是从存储在 UTC 中的 mysql 检索的)

在本地 JVM 中输出

输入 = 2020-09-01T16:54:20.704Z[UTC]

输出 = 2020 年 9 月 1 日星期二 22:24:20 IST

谷歌云函数的输出

输入 =调用 getZonedTime=2020-09-01 15:10:31.0

输出 =返回 getZonedTime=2020 年 9 月 1 日星期二 15:10:31 UTC

期望返回新时区的时间(与传递的日期对象的时间相同)。

谢谢

4

0 回答 0