获取 Java 日期或日历的时间组件
是否有一种简单或优雅的方法来仅获取 Java 日期(或日历,对我来说真的不重要)的一天中的时间(小时/分钟/秒/毫秒)部分?我正在寻找一种单独考虑日期(年/月/日)和时间部分的好方法,但据我所知,我坚持单独访问每个字段.
Is there a simple or elegant way to grab only the time of day (hours/minutes/seconds/milliseconds) part of a Java Date (or Calendar, it really doesn't matter to me)? I'm looking for a nice way to separately consider the date (year/month/day) and the time-of-day parts, but as far as I can tell, I'm stuck with accessing each field separately.
我知道我可以编写自己的方法来单独获取我感兴趣的字段,但我会将其作为静态实用程序方法来执行,这很难看.另外,我知道 Date 和 Calendar 对象具有毫秒精度,但在这两种情况下我都看不到访问毫秒组件的方法.
I know I could write my own method to individually grab the fields I'm interested, but I'd be doing it as a static utility method, which is ugly. Also, I know that Date and Calendar objects have millisecond precision, but I don't see a way to access the milliseconds component in either case.
我不清楚这一点:使用 Date::getTime() 或 Calendar::getTimeInMillis 之一对我来说并不是非常有用,因为它们返回的毫秒数纪元(由该日期或日历表示),它实际上并未将一天中的时间与其余信息分开.
I wasn't clear about this: using one of the Date::getTime() or Calendar::getTimeInMillis is not terribly useful to me, since those return the number of milliseconds since the epoch (represented by that Date or Calendar), which does not actually separate the time of day from the rest of the information.
@Jherico 的答案是最接近的,我认为,但绝对是我仍然必须将其融入我自己编写的方法中.这不完全是我想要的,因为它仍然在返回的毫秒值中包含小时、分钟和秒 - 尽管我可能可以让它为我的目的工作.
@Jherico's answer is the closest thing, I think, but definitely is something I'd still have to roll into a method I write myself. It's not exactly what I'm going for, since it still includes hours, minutes, and seconds in the returned millisecond value - though I could probably make it work for my purposes.
我仍然认为每个组件都是独立的,当然,它们不是.您可以将时间写为自任意参考日期以来的毫秒数,或者您可以写出与 year/month/day hours:minutes:seconds.milliseconds
完全相同的时间.
I still think of each component as separate, although of course, they're not. You can write a time as the number of milliseconds since an arbitrary reference date, or you could write the exact same time as year/month/day hours:minutes:seconds.milliseconds
.
这不是为了显示目的.我知道如何使用 DateFormat
来制作漂亮的日期字符串.
This is not for display purposes. I know how to use a DateFormat
to make pretty date strings.
编辑 2: 我最初的问题来自我发现自己编写的一小部分实用程序函数 - 例如:
Edit 2: My original question arose from a small set of utility functions I found myself writing - for instance:
- 检查两个
Date
是否代表同一天的日期时间; - 检查某个日期是否在其他两个日期指定的范围内,但有时会包含在内,有时则不包含在内,具体取决于时间组件.
- Checking whether two
Date
s represent a date-time on the same day; - Checking whether a date is within a range specified by two other dates, but sometimes checking inclusively, and sometimes not, depending on the time component.
Joda Time 有这种功能吗?
Does Joda Time have this type of functionality?
编辑 3: @Jon 关于我的第二个要求的问题,只是为了澄清:第二个要求是使用我的 Dates 有时代表一整天的结果 - 其中时间部分并不重要all - 有时表示日期时间(即,IMO,对于包含 year/month/day
and hours:minutes 的事物的最准确词:秒:...
).
Edit 3: @Jon's question regarding my second requirement, just to clarify: The second requirement is a result of using my Dates to sometimes represent entire days - where the time component doesn't matter at all - and sometimes represent a date-time (which is, IMO, the most accurate word for something that contains year/month/day
and hours:minutes:seconds:...
).
当 Date 表示一整天时,它的时间部分为零(例如,Date 的时间部分"是午夜),但语义规定范围检查是在结束日期包含在内完成的.因为我只是将此检查保留到 Date::before 和 Date::after,所以我必须将 1 天添加到结束日期 - 因此日期的时间部分为零时的特殊情况.
When a Date represents an entire day, its time parts are zero (e.g. the Date's "time component" is midnight) but the semantics dictate that the range check is done inclusively on the end date. Because I just leave this check up to Date::before and Date::after, I have to add 1 day to the end date - hence the special-casing for when the time-of-day component of a Date is zero.
希望这没有让事情变得不那么清楚.
Hope that didn't make things less clear.
推荐答案
好吧,我知道这是一个可预测的答案,但是...使用 乔达时间.它对日期"、瞬间"、一天中的某个时间"等有单独的表示.它是一个比内置类 IMO 更丰富且通常更理智的 API.
Okay, I know this is a predictable answer, but... use Joda Time. That has separate representations for "a date", "an instant", "a time of day" etc. It's a richer API and a generally saner one than the built-in classes, IMO.
如果这是您感兴趣的唯一位日期/时间操作,那么它可能有点矫枉过正......但如果您使用内置日期/时间 API 来处理任何事情重要的是,我强烈建议您尽快离开它,转而使用 Joda.
If this is the only bit of date/time manipulation you're interested in then it may be overkill... but if you're using the built-in date/time API for anything significant, I'd strongly recommend that you move away from it to Joda as soon as you possibly can.
顺便说一句,您应该考虑您感兴趣的时区.Calendar
具有关联的时区,但 Date
没有(它只是表示时间的瞬间,从 Unix 纪元开始以毫秒为单位).
As an aside, you should consider what time zone you're interested in. A Calendar
has an associated time zone, but a Date
doesn't (it just represents an instant in time, measured in milliseconds from the Unix epoch).
相关文章