java 为什么 Joda Time 中 LocalTime 的 getLocalMillis() 是受保护的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15513996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:52:54  来源:igfitidea点击:

Why is getLocalMillis() of LocalTime in Joda Time is a protected method?

javajodatime

提问by Jedi Knight

I wanted to convert joda time LocalTime to millis or milliseconds. I saw that the getLocalMillis is a protected method. Looks like there is no method to get the millis value of LocalTime.

我想将 joda 时间 LocalTime 转换为毫秒或毫秒。我看到 getLocalMillis 是一种受保护的方法。看起来没有方法可以获取LocalTime的毫秒值。

So, do I have to get the values of each field in millis and then add them up to get the total millis ? Why does Joda Time not have a public method for getting Local Millis ?

那么,我是否必须以毫秒为单位获取每个字段的值,然后将它们相加以获得总毫秒数?为什么 Joda Time 没有获取 Local Millis 的公共方法?

回答by cheeken

A LocalTimedoes not represent an absolute instant in time, but rather it describes a time of any day for an arbitrary timezone.

ALocalTime不代表时间上的绝对瞬间,而是描述任意时区的任何一天的时间。

Render your LocalTimeinto an DateTimevia LocalTime#toDateTimeToday()or LocalTime#toDateTimeToday(DateTimeZone)if you're looking for the moment described by that time today. Or if you want that moment on another day, construct the appropriate LocalDateand see the LocalDate#toDateTime(...)methods. Then call DateTime#getMillis().

将您渲染LocalTime成一个DateTime通孔,LocalTime#toDateTimeToday()或者LocalTime#toDateTimeToday(DateTimeZone)如果您正在寻找今天那个时间所描述的时刻。或者,如果您希望在另一天的那一刻,构建适当的LocalDate并查看LocalDate#toDateTime(...)方法。然后调用DateTime#getMillis()

回答by Nickolay Savchenko

You can use also .toDate().getTime()

你也可以使用 .toDate().getTime()

回答by twalthr

If you really need the time milliseconds, you can also do:

如果你真的需要时间毫秒,你也可以这样做:

LocalTime#get(DateTimeFieldType.millisOfDay())