将时代转化为时代 [英] convert epoch time to date

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

问题描述

我尝试了一百万种不同的方法来做到这一点,但没有效果。任何帮助将不胜感激。

  long millis = getMillisFromServer(); 
Date date = new Date(millis);
DateFormat format = new SimpleDateFormat(dd / MM / yyyy HH:mm:ss);
format.setTimeZone(TimeZone.getTimeZone(Australia / Sydney));
String formatted = format.format(date);

上述不起作用。



基本上,我想做的是获得时代,并将其转换为澳大利亚时间。我当地的时间是+05.30,但当然我不希望这是一个有助于这种转换的因素。



EDIT-



当我运行确切的代码时输出



epoch 1318388699000



Wed Oct 12 08:34:59 GMT + 05:30 2011



12/10/2011 03:04:59



12/10/2011 14:04:59

解决方案

编辑:好的,所以你不希望你的本地时间(这不是澳大利亚)对结果做出贡献,而是澳大利亚时区。您现有的代码应该是绝对好的,虽然悉尼当前是UTC + 11 ,而不是UTC + 10 ..短但完整的测试应用程序:

  import java.util。*; 
import java.text。*;

public class Test {
public static void main(String [] args)throws InterruptedException {
Date date = new Date(1318386508000L);
DateFormat format = new SimpleDateFormat(dd / MM / yyyy HH:mm:ss);
format.setTimeZone(TimeZone.getTimeZone(Etc / UTC));
String formatted = format.format(date);
System.out.println(格式化);
format.setTimeZone(TimeZone.getTimeZone(Australia / Sydney));
formatted = format.format(date);
System.out.println(格式化);
}
}

输出:

  12/10/2011 02:28:28 
12/10/2011 13:28:28

我还建议您开始使用 Joda时间这只是一个更好的日期/时间API ...



编辑:请注意,如果您的系统不知道澳大利亚/悉尼时区,它将显示UTC。例如,如果我更改了使用 TimeZone.getTimeZone(blah / blah)的代码,它将显示UTC值两次。我建议你打印 TimeZone.getTimeZone(澳大利亚/悉尼)。getDisplayName(),看看它是什么...并检查你的代码打字错误:)


I've tried a million different ways of doing this, but with no avail. Any help would be much appreciated.

long millis = getMillisFromServer();
Date date = new Date(millis);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
String formatted = format.format(date);

The above doesn't work.

basically, what I want to do is, get the epoch time and convert it to Australian time. My local time is +05.30 but of course I don't want this to be a factor which contributes to this conversion.

EDIT-

Output when I run your exact code,

epoch 1318388699000

Wed Oct 12 08:34:59 GMT+05:30 2011

12/10/2011 03:04:59

12/10/2011 14:04:59

解决方案

EDIT: Okay, so you don't want your local time (which isn't Australia) to contribute to the result, but instead the Australian time zone. Your existing code should be absolutely fine then, although Sydney is currently UTC+11, not UTC+10.. Short but complete test app:

import java.util.*;
import java.text.*;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        Date date = new Date(1318386508000L);
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        String formatted = format.format(date);
        System.out.println(formatted);
        format.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
        formatted = format.format(date);
        System.out.println(formatted);
    }
}

Output:

12/10/2011 02:28:28
12/10/2011 13:28:28

I would also suggest you start using Joda Time which is simply a much nicer date/time API...

EDIT: Note that if your system doesn't know about the Australia/Sydney time zone, it would show UTC. For example, if I change the code about to use TimeZone.getTimeZone("blah/blah") it will show the UTC value twice. I suggest you print TimeZone.getTimeZone("Australia/Sydney").getDisplayName() and see what it says... and check your code for typos too :)

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

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