java - 将时间更改为特定时区的最简单方法

标签 java android time timezone

有两个字符串

String date = "9/13/2012";
String time = "5:48pm";

时间是 GMT+0,我想将其更改为 GMT+8,将时间更改为特定时区的最简单方法是什么

最佳答案

  • 使用设置为 UTC 时区的 SimpleDateFormat 对其进行解析
  • 使用设置为您感兴趣的时区的 SimpleDateFormat 来格式化解析的 Date 值。(它可能不仅仅是“UTC+8”) - 您应该找出您真正想要的TZDB时区ID。

例如:

SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy h:mma", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC");
Date date = inputFormat.parse(date + " " + time);

// Or whatever format you want...
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
outputFormat.setTimeZone(targetTimeZone);
String outputText = outputFormat.format(date);

(如果您可以使用 Joda Time 来代替,那就太好了 - 但我知道它对于 Android 应用程序来说相当大。)

关于java - 将时间更改为特定时区的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12415764/

相关文章:

android - 错误通知 : The given region must intersect with the Bitmap's dimensions

angularjs - 在 Angular 中使用 momentjs 来格式化时间

java - log4j.properties 到 log4j2 log2.xml

android - 与混合应用程序的改造集成

Android SQLite - 是否可以检索按周/月分组的数据?

c++ - 即使有时间延迟,cout 也不打印

android webview测量所有资源的时间

java - DAO 的设计策略应该如何进行更新操作?

java - 使用 CAS 设置 session

Java 检查数字月份