在Android中将长时间的UTC时间转换为GMT时间

问题描述 投票:-2回答:1

我有长时间的UTC时间= 1555415100000L。现在我想转换到不同时区的本地时间。

例:

1555415100000L = 2019/04/16 18:45(GMT + 7)

1555415100000L = 2019/04/16 14:45(GMT + 3)......

你有什么建议可以解决这个问题吗?

android time timezone timezone-offset
1个回答
0
投票

我写了这个方法。此方法将特定GMT作为格式化字符串返回。你应该给这个方法留出毫秒和GMT值的时间。

private String getSpecificGmtDate(long timeMillis, int gmt) {
    long time = timeMillis + (gmt * 1000 * 60 * 60);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    return (sdf.format(new Date(time)) + " (GMT " + gmt + ")");
}

输出:

System.out.println(getSpecificGmtDate(1555415100000L, 0));
16/04/2019 11:45 (GMT 0)
System.out.println(getSpecificGmtDate(1555415100000L, 3));
16/04/2019 14:45 (GMT 3)
System.out.println(getSpecificGmtDate(1555415100000L, -3));
16/04/2019 08:45 (GMT -3)
© www.soinside.com 2019 - 2024. All rights reserved.