我可以从毫秒值获取当前时间的值吗
Can I get value of Curren time from miliseconds value
我有一个以毫秒为单位的值1601626934449
通过https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()
生成
但是我能否以某种方式获得人类可读格式的时间,或者简而言之,我需要能够知道以毫秒为单位的值是多少 1601626934449 是什么?
您可以创建一个 Date 对象并使用它来获取您需要的所有信息:
https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)
可以将毫秒转换成LocalDateTime
来存储时间
long millis = System.currentTimeMillis();
LocalDateTime datetime = Instant.ofEpochMilli(millis)
.atZone(ZoneId.systemDefault()).toLocalDateTime();
然后您可以使用 toString()
或您想要的格式使用 DateTimeFormatter
打印数据。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
System.out.println(datetime.format(formatter));
输出:2020-10-02 18:39:54.609
在 Java 8 或更高版本上使用 java.time
。使用它,很容易达到您的目标。
您基本上从纪元毫秒(代表一个时刻)创建一个 Instant
,通过应用 ZoneId
(我的系统在以下示例中的默认值)使其成为 ZonedDateTime
,然后通过 built-in DateTimeFormatter
格式化输出 String
或通过创建具有所需模式的自定义输出以根据需要将其设置为 human-readable。
这是一个例子:
public static void main(String[] args) {
// your example millis
long currentMillis = 1601626934449L;
// create an instant from those millis
Instant instant = Instant.ofEpochMilli(currentMillis);
// use that instant and a time zone in order to get a suitable datetime object
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
// then print the (implicitly called) toString() method of it
System.out.println(currentMillis + " is " + zdt);
// or create a different human-readable formatting by means of a custom formatter
System.out.println(
zdt.format(
DateTimeFormatter.ofPattern(
"EEEE, dd. 'of' MMMM uuuu 'at' HH:mm:ss 'o''clock in' VV 'with an offset of' xxx 'hours'",
Locale.ENGLISH
)
)
);
}
哪个输出(在我的系统上)
1601626934449 is 2020-10-02T10:22:14.449+02:00[Europe/Berlin]
Friday, 02. of October 2020 at 10:22:14 o'clock in Europe/Berlin with an offset of +02:00 hours
我有一个以毫秒为单位的值1601626934449
通过https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()
生成但是我能否以某种方式获得人类可读格式的时间,或者简而言之,我需要能够知道以毫秒为单位的值是多少 1601626934449 是什么?
您可以创建一个 Date 对象并使用它来获取您需要的所有信息:
https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)
可以将毫秒转换成LocalDateTime
来存储时间
long millis = System.currentTimeMillis();
LocalDateTime datetime = Instant.ofEpochMilli(millis)
.atZone(ZoneId.systemDefault()).toLocalDateTime();
然后您可以使用 toString()
或您想要的格式使用 DateTimeFormatter
打印数据。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
System.out.println(datetime.format(formatter));
输出:2020-10-02 18:39:54.609
在 Java 8 或更高版本上使用 java.time
。使用它,很容易达到您的目标。
您基本上从纪元毫秒(代表一个时刻)创建一个 Instant
,通过应用 ZoneId
(我的系统在以下示例中的默认值)使其成为 ZonedDateTime
,然后通过 built-in DateTimeFormatter
格式化输出 String
或通过创建具有所需模式的自定义输出以根据需要将其设置为 human-readable。
这是一个例子:
public static void main(String[] args) {
// your example millis
long currentMillis = 1601626934449L;
// create an instant from those millis
Instant instant = Instant.ofEpochMilli(currentMillis);
// use that instant and a time zone in order to get a suitable datetime object
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
// then print the (implicitly called) toString() method of it
System.out.println(currentMillis + " is " + zdt);
// or create a different human-readable formatting by means of a custom formatter
System.out.println(
zdt.format(
DateTimeFormatter.ofPattern(
"EEEE, dd. 'of' MMMM uuuu 'at' HH:mm:ss 'o''clock in' VV 'with an offset of' xxx 'hours'",
Locale.ENGLISH
)
)
);
}
哪个输出(在我的系统上)
1601626934449 is 2020-10-02T10:22:14.449+02:00[Europe/Berlin]
Friday, 02. of October 2020 at 10:22:14 o'clock in Europe/Berlin with an offset of +02:00 hours