获取Java中LocalTime的所有毫秒

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

Get all milliseconds from LocalTime Java

问题

我需要知道如何将LocalTime转换为毫秒。

LocalTime lunchTime = LocalTime.parse("01:00:00",
        DateTimeFormatter.ISO_TIME);

如果我执行 `lunchTime.getMinute()`,我只会得到0而执行 `lunchTime.getHour()`,我只会得到1作为小时如何获取毫秒值
英文:

I need to know how to get LocalTime to milliseconds

    LocalTime  lunchTime = LocalTime.parse("01:00:00",
            DateTimeFormatter.ISO_TIME);

If i am going to execute lunchTime.getMinute() i only get 0 and lunchTime.getHour() i only get 1 as an hour. How to get value in milliseconds?

答案1

得分: 5

如果您想获取一天中的毫秒数换句话说自午夜以来的毫秒计数),那么`ChronoField`枚举常量正好可以做到

	LocalTime lunchTime = LocalTime.parse("01:00:00");
	int millisecondOfDay = lunchTime.get(ChronoField.MILLI_OF_DAY);
	System.out.println("午餐时间为一天中的第" + millisecondOfDay + "毫秒");

输出

> 午餐时间为一天中的第3600000毫秒

对于某些人来说上午1点可能是一个有趣的午餐时间但为了演示和使用您自己的示例。)

java.time的日期和时间类通常具有接受`TemporalField`参数的`get`方法。`LocalTime`也不例外在调用`get()`最常见的做法是传递一个`ChronoField`常量该方法非常灵活可用于从日期时间对象中获取没有提供`getXxx`方法的值

**文档链接** [`ChronoField.MILLI_OF_DAY`](https://docs.oracle.com/javase/10/docs/api/java/time/temporal/ChronoField.html#MILLI_OF_DAY)
英文:

If you want the millisecond of the day (in other words the count of milliseconds since 00:00), there is a ChronoField enum constant exactly for that:

	LocalTime lunchTime = LocalTime.parse("01:00:00");
	int millisecondOfDay = lunchTime.get(ChronoField.MILLI_OF_DAY);
	System.out.println("Lunch is at " + millisecondOfDay + " milliseconds of the day");

Output:

> Lunch is at 3600000 milliseconds of the day

(1 AM is probably a funny time for lunch for some, but for the sake of demonstration and of using your own example.)

The date and time classes of java.time generally have got a get method that accepts a TemporalField argument. LocalTime is no exception to this rule. The most common thing to do when calling get() is to pass a ChronoField constant. The method is very versatile for getting values from the date-time object for which no getXxx method is provided.

Doc link: ChronoField.MILLI_OF_DAY

答案2

得分: 4

尝试获取纳秒或秒,然后根据所处理的精度转换为毫秒。

lunchTime.toNanoOfDay() / 1e+6
lunchTime.toSecondOfDay() * 1e+3
英文:

Try getting nano seconds or seconds and converting to milliseconds (depending on what precision you're dealing with).

lunchTime.toNanoOfDay() / 1e+6
lunchTime.toSecondOfDay() * 1e+3

答案3

得分: 0

你可以使用 System.currentTimeMillis(),参考 https://currentmillis.com/

另外,请参考 https://stackoverflow.com/a/26637209/1270989 以便按照您选择的格式进行日期解析

此外,如果您正在尝试从运行在不同时区的应用程序中获取您时区的本地时间,请参考评论 https://stackoverflow.com/a/319398/1270989

英文:

You can use System.currentTimeMillis(), refer https://currentmillis.com/

Also, please refer https://stackoverflow.com/a/26637209/1270989 to followup on parsing date in your choice of format.

Also, if you trying to get local time in your timezone from an application running in different timezone please follow comment https://stackoverflow.com/a/319398/1270989

huangapple
  • 本文由 发表于 2020年4月4日 19:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61027257.html
匿名

发表评论

匿名网友

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

确定