将纪元时间转换为日期
我已经尝试了一百万种不同的方法来做到这一点,但没有成功。任何帮助将不胜感激。
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,但我当然不希望它成为促成此转换的一个因素。
编辑-
当我运行你的确切代码时的输出,
纪元1318388699000
Wed Oct 12 08:34:59 GMT+05:30 2011
12/10/2011 03:04:59
12/10/2011 14:04:59
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:好的,所以您不希望您的本地时间(不是澳大利亚)影响结果,而是澳大利亚时区。那么您现有的代码应该绝对没问题,尽管 悉尼目前是 UTC+11,不是 UTC+10.. 简短但完整的测试应用程序:
输出:
我还建议您开始使用 Joda Time 这只是一个更好的日期/时间 API...
编辑:请注意,如果您的系统不知道
澳大利亚/悉尼
时区,它会显示 UTC。例如,如果我更改要使用 TimeZone.getTimeZone("blah/blah") 的代码,它将显示 UTC 值两次。我建议您打印TimeZone.getTimeZone("Australia/Sydney").getDisplayName()
并查看它的内容...并检查您的代码是否有拼写错误:)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:
Output:
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 useTimeZone.getTimeZone("blah/blah")
it will show the UTC value twice. I suggest you printTimeZone.getTimeZone("Australia/Sydney").getDisplayName()
and see what it says... and check your code for typos too :)这是现代答案(从 2014 年起有效)。 2011 年接受的答案是一个非常好的答案。现在我建议没有人使用
Date
、DateFormat
和SimpleDateFormat
类。使用现代 Java 日期和时间 API,一切都变得更加自然。要从 millis 获取日期时间对象:
如果
millis
等于1318388699000L
,则为2011-10-12T14:04:59+11:00[澳大利亚/悉尼]
。如果代码以某种奇怪的方式最终出现在不知道澳大利亚/悉尼时区的 JVM 上,您一定会收到异常通知。如果您希望以字符串格式显示日期时间:
结果:
PS 我不知道您所说的“以上不起作用”是什么意思。在我的计算机上,问题中的代码也打印
12/10/2011 14:04:59
。Here’s the modern answer (valid from 2014 and on). The accepted answer was a very fine answer in 2011. These days I recommend no one uses the
Date
,DateFormat
andSimpleDateFormat
classes. It all goes more natural with the modern Java date and time API.To get a date-time object from your millis:
If
millis
equals1318388699000L
, this gives you2011-10-12T14:04:59+11:00[Australia/Sydney]
. Should the code in some strange way end up on a JVM that doesn’t know Australia/Sydney time zone, you can be sure to be notified through an exception.If you want the date-time in your string format for presentation:
Result:
PS I don’t know what you mean by “The above doesn't work.” On my computer your code in the question too prints
12/10/2011 14:04:59
.请注意纪元时间以秒为单位,并且 Date 对象接受以毫秒为单位的 Long 值。
因此,您必须将 epoch 值乘以 1000 才能将其用作 long 值。
如下所示:-
Please take care that the epoch time is in second and Date object accepts Long value which is in milliseconds.
Hence you would have to multiply epoch value with 1000 to use it as long value .
Like below :-