资源下载转换UTC时间戳到本地日期时间时间、日期、资源、UTC

由网友(爱你没怂过)分享简介:我知道有几十个回答职位有关转换UTC时间/日期为/从本地时间了,但是不帮我找出我的问题是。我的问题是:通过让UTC时间戳,我怎么能得到当地的日期时间?这就是我现在所拥有的,但这只是转换时间戳DateTime格式。I know there are dozens of answered posts about co...

我知道有几十个回答职位有关转换UTC时间/日期为/从本地时间了,但是不帮我找出我的问题是。 我的问题是: 通过让UTC时间戳,我怎么能得到当地的日期时间? 这就是我现在所拥有的,但这只是转换时间戳DateTime格式。

I know there are dozens of answered posts about converting UTC Time/Date To/From local time already but non helped me to figure out what my problem is. My question is: By having UTC timestamp, how can i get local DateTime? This is what I have right now but this just convert the timestamp to DateTime format.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getDefault());         
sdf.format(new Date(timestamp * 1000));

编辑:我节省了云中的UTC时间戳所以每一个设备(安卓/ iOS的),可以查询并转换为它的时区

Edited: I'm saving the UTC timestamp on the cloud so every device (Android/iOS) can query and convert to it's time zone.

推荐答案

试试这个与我一起工作

public  String getDateCurrentTimeZone(long timestamp) {
        try{
            Calendar calendar = Calendar.getInstance();
            TimeZone tz = TimeZone.getDefault();
            calendar.setTimeInMillis(timestamp * 1000);
            calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date currenTimeZone = (Date) calendar.getTime();
            return sdf.format(currenTimeZone);
        }catch (Exception e) {
        }
        return "";
    }
阅读全文

相关推荐

最新文章