問(wèn)題描述
是否有一種簡(jiǎn)單或優(yōu)雅的方法來(lái)僅獲取 Java 日期(或日歷,對(duì)我來(lái)說(shuō)真的不重要)的一天中的時(shí)間(小時(shí)/分鐘/秒/毫秒)部分?我正在尋找一種單獨(dú)考慮日期(年/月/日)和時(shí)間部分的好方法,但據(jù)我所知,我堅(jiān)持單獨(dú)訪問(wèn)每個(gè)字段.
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.
我知道我可以編寫(xiě)自己的方法來(lái)單獨(dú)獲取我感興趣的字段,但我會(huì)將其作為靜態(tài)實(shí)用程序方法來(lái)執(zhí)行,這很難看.另外,我知道 Date 和 Calendar 對(duì)象具有毫秒精度,但在這兩種情況下我都看不到訪問(wèn)毫秒組件的方法.
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.
我不清楚這一點(diǎn):使用 Date::getTime() 或 Calendar::getTimeInMillis 之一對(duì)我來(lái)說(shuō)并不是非常有用,因?yàn)樗鼈兎祷氐暮撩霐?shù)紀(jì)元(由該日期或日歷表示),它實(shí)際上并未將一天中的時(shí)間與其余信息分開(kāi).
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 的答案是最接近的,我認(rèn)為,但絕對(duì)是我仍然必須將其融入我自己編寫(xiě)的方法中.這不完全是我想要的,因?yàn)樗匀辉诜祷氐暮撩胫抵邪r(shí)、分鐘和秒 - 盡管我可能可以讓它為我的目的工作.
@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.
我仍然認(rèn)為每個(gè)組件都是獨(dú)立的,當(dāng)然,它們不是.您可以將時(shí)間寫(xiě)為自任意參考日期以來(lái)的毫秒數(shù),或者您可以寫(xiě)出與 year/month/day hours:minutes:seconds.milliseconds
完全相同的時(shí)間.
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
來(lái)制作漂亮的日期字符串.
This is not for display purposes. I know how to use a DateFormat
to make pretty date strings.
編輯 2: 我最初的問(wèn)題來(lái)自我發(fā)現(xiàn)自己編寫(xiě)的一小部分實(shí)用程序函數(shù) - 例如:
Edit 2: My original question arose from a small set of utility functions I found myself writing - for instance:
- 檢查兩個(gè)
Date
是否代表同一天的日期時(shí)間; - 檢查某個(gè)日期是否在其他兩個(gè)日期指定的范圍內(nèi),但有時(shí)會(huì)包含在內(nèi),有時(shí)則不包含在內(nèi),具體取決于時(shí)間組件.
- 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 關(guān)于我的第二個(gè)要求的問(wèn)題,只是為了澄清:第二個(gè)要求是使用我的 Dates 有時(shí)代表一整天的結(jié)果 - 其中時(shí)間部分并不重要all - 有時(shí)表示日期時(shí)間(即,IMO,對(duì)于包含 year/month/day
and hours:minutes 的事物的最準(zhǔn)確詞:秒:...
).
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:...
).
當(dāng) Date 表示一整天時(shí),它的時(shí)間部分為零(例如,Date 的時(shí)間部分"是午夜),但語(yǔ)義規(guī)定范圍檢查是在結(jié)束日期包含在內(nèi)完成的.因?yàn)槲抑皇菍⒋藱z查保留到 Date::before 和 Date::after,所以我必須將 1 天添加到結(jié)束日期 - 因此日期的時(shí)間部分為零時(shí)的特殊情況.
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.
希望這沒(méi)有讓事情變得不那么清楚.
Hope that didn't make things less clear.
推薦答案
好吧,我知道這是一個(gè)可預(yù)測(cè)的答案,但是...使用 喬達(dá)時(shí)間.它對(duì)日期"、瞬間"、一天中的某個(gè)時(shí)間"等有單獨(dú)的表示.它是一個(gè)比內(nèi)置類(lèi) 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.
如果這是您感興趣的唯一位日期/時(shí)間操作,那么它可能有點(diǎn)矯枉過(guò)正......但如果您使用內(nèi)置日期/時(shí)間 API 來(lái)處理任何事情重要的是,我強(qiáng)烈建議您盡快離開(kāi)它,轉(zhuǎn)而使用 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.
順便說(shuō)一句,您應(yīng)該考慮您感興趣的時(shí)區(qū).Calendar
具有關(guān)聯(lián)的時(shí)區(qū),但 Date
沒(méi)有(它只是表示時(shí)間的瞬間,從 Unix 紀(jì)元開(kāi)始以毫秒為單位).
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).
這篇關(guān)于獲取 Java 日期或日歷的時(shí)間組件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!