将UTC日期转换为本地日期 [英] Convert UTC Date to local Date

查看:163
本文介绍了将UTC日期转换为本地日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从时代(UTC)转换为如下所示的格式。现在我尝试了不同的SO答案将 UTCDate UTC 转换为当地时间。但我没有得到当地时间。

I am converting from epoch time (which is in UTC) to a format as shown below. Now I tried different SO answers to convert UTCDate from UTC to local time. But I am not getting the local time.

任何帮助将不胜感激。

String epochTime = "1436831775043";

Date UTCDate = new Date(Long.parseLong(epochTime));
Date localDate; // How to get this?

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
String result = simpleDateFormat.format(UTCDate);

此外,转换必须在没有任何外部库的帮助下完成。

Also, the conversion has to be done without the help of any external library.

推荐答案

您可以在格式化程序中设置时区:

You can set your time zone in the formatter:

simpleDateFormat.setTimeZone(TimeZone.getDefault());

这篇关于将UTC日期转换为本地日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆