【问题标题】:Convert UTC time in long value to GMT time in Android将长值中的 UTC 时间转换为 Android 中的 GMT 时间
【发布时间】:2019-09-06 18:54:12
【问题描述】:

我的 UTC 时间长值 = 1555415100000L。 现在我想转换为不同时区的当地时间。

示例:

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

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

您对解决这个问题有什么建议吗?

【问题讨论】:

  • long 值是以毫秒表示的时间。这是您需要的:stackoverflow.com/questions/31292032/…
  • 您可以从中创建一个Date对象,然后您可以在Android中使用Calendar对象和您选择的Locale,之后您可以使用DateFormat正确显示它在屏幕上。使用这些关键字进行一些研究会有所帮助。
  • @acarlstein:你的解决方案对我不起作用

标签: android time timezone timezone-offset


【解决方案1】:

我写了这个方法。此方法将特定 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)

【讨论】:

  • 时区怎么样:GMT+5:30
  • 您可以轻松添加。它的基本编程逻辑。 private String getSpecificGmtDate(long timeMillis, int gmtHour, int gmtMin) { if(gmtMin==0){gmtMin=60;} long time = timeMillis + (gmtHour * 1000 * 60 * gmtMin); ....
猜你喜欢
  • 2010-09-15
  • 2015-04-27
  • 2023-03-26
  • 2014-10-12
  • 2015-11-09
  • 2014-09-04
  • 2018-09-17
相关资源
最近更新 更多