【发布时间】:2019-12-27 07:23:35
【问题描述】:
我搜索了解决方案但仍然无法正常工作,我正在尝试将日期对象的本地时区转换为 UTC +0,但是当我将日期对象格式化为 UTC 时,它正在工作。但是当我想再次将转换后的字符串转换为日期时,格式会发生变化,并且 UTC 在我将其存储在火存储之前会返回到 GMT+8。代码有什么问题?
这是我得到的当前日期对象
Calendar time = Calendar.getInstance();
time.setTimeZone(TimeZone.getTimeZone("UTC"));
Date current_time = time.getTime();
如果打印出来
Thu Aug 22 10:09:55 GMT+08:00 2019
然后我将其转换为 UTC
String dismissal_time_firestore;
Log.i(TAG, "Current time when swiped from phone time.getTime() "+current_time);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dismissal_time_firestore = dateFormat.format(current_time);
知道了
Thu, 22 Aug 2019 02:09:55 +0000
但是当我将此字符串转换为日期对象时
try {
current_time = dateFormat.parse(dismissal_time_firestore);
} catch (ParseException e) {
e.printStackTrace();
}
我明白了
Thu Aug 22 10:09:55 GMT+08:00 2019
【问题讨论】:
-
你不能。老式的
Date对象不能有时区或偏移量。请考虑通过 ThreeTenABP 使用现代 java.time。
标签: java datetime timezone utc