从毫秒转换为日期字符串 [英] Convert from Millisecond to String of Date

查看:404
本文介绍了从毫秒转换为日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将毫秒转换为日期字符串。但是,结果不符合我的预期。

I try to convert from Milliseconds to string of date. However, the result is not correct as my expected.

输入的时间是毫秒(例如: 1508206600485

The input is milliseconds (Ex: 1508206600485)

我的时区是 UTC +10:00

------Expected-------------------------------------------- Actual------

01:32 (PM) 17/10/2017--------------------------------02:32 (PM) 17/10/2017

这里是该方法

public static String getDate(long milliSeconds) {
    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM/yyyy");
    String dateString = formatter.format(new Date(milliSeconds));
    return dateString;
}


推荐答案

您找到了解决方案,我只想添加 Java 8 新的java.time API 。旧的类(日期日历 SimpleDateFormat 很多问题设计问题,强烈建议您尽可能切换到新的API。

Good you found a solution, I just like to add an approach with Java 8 new java.time API. The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and it's strongly recommended to switch to the new API if possible.

如果您使用的是 Java< = 7 ,您可以使用 ThreeTen Backport ,它是Java 8的新日期/时间类的绝佳反向端口。对于 Android ,您还需要 ThreeTenABP (有关如何操作的更多信息在此处)使用它。

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it here).

以下代码适用于都。
唯一的区别是软件包名称(在Java 8中为 java.time ,在ThreeTen Backport(或Android的ThreeTenABP)中为 org。 threeten.bp ),但类和方法名称是相同的。

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

将Millis值转换为在特定时区,您可以使用 Instant 类,然后使用 ZoneId 转换为时区,从而创建 ZonedDateTime
然后使用 DateTimeFormatter 对其进行格式化:

To convert the millis value to a specific timezone, you can use the Instant class, then use a ZoneId to convert to a timezone, creating a ZonedDateTime. Then you use a DateTimeFormatter to format it:

// convert millis value to a timezone
Instant instant = Instant.ofEpochMilli(1508206600485L);
ZonedDateTime z = instant.atZone(ZoneId.of("Australia/Sydney"));
// format it
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("hh:mm dd/MM/yyyy");
System.out.println(fmt.format(z)); // 01:16 17/10/2017

输出为:


01:16 17/10/2017

01:16 17/10/2017

请注意,我使用了 hh 根据javadoc ,该字母表示 am-pm的小时小时数字段(值从1到12),因此如果没有AM / PM指示符,它可能会模棱两可。也许您想添加AM / PM字段(将字母 a 添加到格式模式),或将小时数更改为 HH 小时数,值从0到23)。

Note that I used hh for the hours. According to javadoc, this lettern represents the clock-hour-of-am-pm field (values from 1 to 12), so without the AM/PM indicator, it can be ambiguous. Maybe you want to add AM/PM field (adding the letter a to the format pattern), or change the hours to HH (hour-of-day, with values from 0 to 23).

也请注意,<$ c的实际值$ c> ZonedDateTime 是 2017-10-17T13:16:40.485 + 11:00 01:16 PM ),因为在2017年10月17日 th 中,悉尼处于夏令时,因此实际偏移量为 +11:00

Also note that the actual value of the ZonedDateTime is 2017-10-17T13:16:40.485+11:00 (01:16 PM), because in October 17th 2017, Sydney is in Daylight Saving Time, so the actual offset is +11:00.

这篇关于从毫秒转换为日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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