Sure, here’s the translation: 能否从毫秒值中获取当前时间的值

huangapple go评论94阅读模式
英文:

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 代表的时间是什么?

英文:

I have a value in miliseconds 1601626934449

Which generated via https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()

but can I somehow be able to get time in human readable format, or in brief I need to be able to know what the value in miliseconds 1601626934449 is ?

答案1

得分: 3

你可以将毫秒转换为 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

英文:

You can convert millis into LocalDateTime to store time

long millis = System.currentTimeMillis();
LocalDateTime datetime = Instant.ofEpochMilli(millis)
                                .atZone(ZoneId.systemDefault()).toLocalDateTime();

Then you can print your data using toString() or your desire format using DateTimeFormatter.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
System.out.println(datetime.format(formatter));

Output: 2020-10-02 18:39:54.609

答案2

得分: 3

使用Java 8或更高版本的java.time库。使用它,轻松实现您的目标。

您基本上可以从纪元毫秒(代表时间点)创建一个Instant,然后通过应用ZoneId(在以下示例中使用的是系统默认时区)将其转换为ZonedDateTime,然后可以通过内置的DateTimeFormatter格式化输出String,或者通过创建带有所需模式的自定义格式化程序将其格式化为人类可读的格式。

以下是一个示例:

public static void main(String[] args) {
    // 您的示例毫秒数
    long currentMillis = 1601626934449L;
    // 从这些毫秒数创建一个Instant
    Instant instant = Instant.ofEpochMilli(currentMillis);
    // 使用该Instant和时区来获取一个合适的日期时间对象
    ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
    // 然后打印其(隐式调用的)toString()方法
    System.out.println(currentMillis + " 是 " + zdt);
    // 或者通过自定义格式化程序创建不同的人类可读格式
    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 是 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
英文:

Use java.time on Java 8 or higher. Using that, it's easy to reach your goal.
You basically create an Instant from the epoch milliseconds (which represent a moment in time), make it a ZonedDateTime by applying a ZoneId (my system's default in the following example) and then either format the output String by a built-in DateTimeFormatter or by creating a custom one with a desired pattern to make it as human-readable as required.

Here's an example:

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
			)
		)
	);
}

which outputs (on my system)

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

答案3

得分: 0

你可以创建一个 Date 对象并使用它来获取所需的所有信息:

https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)

英文:

You can create a Date object and use it to get all the information you need:

https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#Date(long)

huangapple
  • 本文由 发表于 2020年10月2日 20:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64171319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定