convert epoch time to date(将纪元时间转换为日期)
问题描述
我已经尝试了上百万种不同的方法,但都无济于事.任何帮助将非常感激.
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);
以上都行不通.
基本上,我想做的是,获取纪元时间并将其转换为澳大利亚时间.我的当地时间是 +05.30,但我当然不希望这成为促成这种转换的因素.
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.
编辑-
当我运行你的确切代码时输出,
Output when I run your exact code,
纪元 1318388699000
epoch 1318388699000
2011 年 10 月 12 日星期三 08:34:59 GMT+05:30
Wed Oct 12 08:34:59 GMT+05:30 2011
2011 年 12 月 10 日 03:04:59
12/10/2011 03:04:59
12/10/2011 14:04:59
12/10/2011 14:04:59
推荐答案
好的,所以您不希望您的 本地 时间(不是澳大利亚)对结果做出贡献,而是澳大利亚时区.那么您现有的代码应该绝对没问题,尽管 悉尼目前是 UTC+11,不是 UTC+10.. 简短但完整的测试应用程序:
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);
}
}
输出:
12/10/2011 02:28:28
12/10/2011 13:28:28
我还建议您开始使用 Joda Time,这是一个更好的日期/时间 API...
I would also suggest you start using Joda Time which is simply a much nicer date/time API...
请注意,如果您的系统不知道 Australia/Sydney
时区,它会显示 UTC.例如,如果我更改要使用 TimeZone.getTimeZone("blah/blah")
的代码,它将显示两次 UTC 值.我建议您打印 TimeZone.getTimeZone("Australia/Sydney").getDisplayName()
并查看它的内容...并检查您的代码是否有错别字:)
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 :)
这篇关于将纪元时间转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将纪元时间转换为日期


- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01