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 :)
这篇关于将纪元时间转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将纪元时间转换为日期
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01