获取Java日期或日历的时间组件 [英] Getting the Time component of a Java Date or Calendar

查看:165
本文介绍了获取Java日期或日历的时间组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单或优雅的方式来抓取一天的时间(小时/分钟/秒/毫秒)Java日期(或日历,对我真的不重要)的一部分?我正在寻找一个很好的方式来分别考虑日期(年/月/日)和时间部分,但据我所知,我被困在单独访问每个字段。



我知道我可以编写自己的方法来单独获取我感兴趣的字段,但是我会将它作为静态实用程序方法,这是丑陋的。此外,我知道日期和日历对象有毫秒的精度,但我没有看到一种方法来访问毫秒组件在任何一种情况下。






编辑:我不清楚这一点:使用Date :: getTime()或Calendar :: getTimeInMillis之一不是非常有用给我,因为那些返回自纪元以来的毫秒数(由该日期或日历表示),实际上并不将时间与其余信息分开。



@ Jherico的答案是最接近的事情,我想,但绝对是我仍然需要滚动到我自己写的方法。这不是我要去的,因为它仍然包括在返回的毫秒值的小时,分​​钟和秒 - 虽然我可能使它工作为我的目的。



我仍然认为每个组件分开,虽然当然,他们不是。您可以将时间写为自任意参考日期以来的毫秒数,或者您可以写入与年/月/日小时:minutes:seconds.milliseconds



这不是为了显示。我知道如何使用 DateFormat 来创建漂亮的日期字符串。






编辑2:我的原始问题来自一组我发现自己写的效用函数:




  • 检查两个日期是否代表同一天的日期时间;

  • 检查日期是否在两个其他日期指定的范围内,但有时根据时间组成部分检查是否包含,有时不检查。



Joda Time有这种类型的功能吗?






@ Jon关于我的第二个要求的问题,仅仅是为了澄清:第二个要求是使用我的日期有时表示整个日期 - 时间组件不重要 - 有时表示日期,时间(即IMO)中包含年/月/日 小时:分钟:seconds:...

$



当日期表示整天时,其时间部分为零日期的时间组件是午夜),但语义表明范围检查是在结束日期包含。因为我只是离开这个检查,直到Date :: before和Date :: after,我必须添加1天到结束日期 - 因此一个日期的日期时间组件为零的特殊包装。 / p>

希望没有使事情变得不那么清楚。

解决方案

一天的时间部分应该是剩余的毫秒数除以每天的毫秒数。

  long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; 
Date now = Calendar.getInstance()。getTime();
long timePortion = now.getTime()%MILLIS_PER_DAY;

或者,考虑使用joda-time,一个功能更全面的时间库。


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.

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.


Edit: 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'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.

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.

This is not for display purposes. I know how to use a DateFormat to make pretty date strings.


Edit 2: My original question arose from a small set of utility functions I found myself writing - for instance:

  • Checking whether two Dates 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.

Does Joda Time have this type of functionality?


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:...).

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.

解决方案

Extracting the time portion of the day should be a matter of getting the remainder number of milliseconds when you divide by the number of milliseconds per day.

long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
Date now = Calendar.getInstance().getTime();
long timePortion = now.getTime() % MILLIS_PER_DAY;

Alternatively, consider using joda-time, a more fully featured time library.

这篇关于获取Java日期或日历的时间组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆