获取Java日期或日历的时间组件 [英] Getting the Time component of a Java Date or Calendar
问题描述
我知道我可以编写自己的方法来单独抓取我感兴趣的字段,但是我会做一个静态实用程序,这是一个丑陋的方法。另外,我知道Date和Calendar对象具有毫秒精度,但是在这两种情况下,我没有看到访问毫秒组件的方法。
编辑:我不清楚这一点:使用Date :: getTime()或Calendar :: getTimeInMillis之一对我来说并不是非常有用,因为那些返回数字(表示为该日期或日历)的毫秒数,其实际上并不真正将时间与其他信息分开。
@ Jherico的答案是最接近的事情,我想,但绝对是我仍然需要自己编写的方法。这不完全是我要去的,因为它仍然包括返回的毫秒值中的小时,分钟和秒 - 尽管我可能会为了我的目的而工作。
我仍然将每个组件都认为是 ,尽管它们不是。您可以写一个时间作为任意引用日期之后的毫秒数,或者您可以写出与$ code>年/月/日小时的完全相同的时间:分钟:秒。毫秒 。
这不是出于显示的目的。我知道如何使用 DateFormat
来制作漂亮的日期字符串。
编辑2:我的原始问题来自我发现自己写的一小部分功能函数,例如:
- 检查两个
Date
是否表示同一天的日期时间; - 检查日期是否在一个范围内由另外两个日期指定,但有时根据时间分量检查,有时不检查。
Joda时间是否具有此类型的功能?
编辑3: @ Jon关于我的第二个要求的问题,只是为了澄清:第二个要求是使用我的日期有时代表整个日子的结果 - 时间部分根本不重要 - 有时代表日期时间(即IMO,最准确的词包含年/月/日
a nd hours:minutes:seconds:...
)。
当一个日期代表整个天,其时间部分为零(例如日期的时间组件是午夜),但语义表示范围检查在结束日期包括在内。因为我刚刚离开这个检查到Date :: before和Date ::之前,我必须添加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
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.
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屋!