久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取 Java 日期或日歷的時間組件

Getting the Time component of a Java Date or Calendar(獲取 Java 日期或日歷的時間組件)
本文介紹了獲取 Java 日期或日歷的時間組件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

是否有一種簡單或優(yōu)雅的方法來僅獲取 Java 日期(或日歷,對我來說真的不重要)的一天中的時間(小時/分鐘/秒/毫秒)部分?我正在尋找一種單獨考慮日期(年/月/日)和時間部分的好方法,但據(jù)我所知,我堅持單獨訪問每個字段.

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.

我知道我可以編寫自己的方法來單獨獲取我感興趣的字段,但我會將其作為靜態(tài)實用程序方法來執(zhí)行,這很難看.另外,我知道 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 之一對我來說并不是非常有用,因為它們返回的毫秒數(shù)紀(jì)元(由該日期或日歷表示),它實際上并未將一天中的時間與其余信息分開.

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)為,但絕對是我仍然必須將其融入我自己編寫的方法中.這不完全是我想要的,因為它仍然在返回的毫秒值中包含小時、分鐘和秒 - 盡管我可能可以讓它為我的目的工作.

@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)為每個組件都是獨立的,當(dāng)然,它們不是.您可以將時間寫為自任意參考日期以來的毫秒數(shù),或者您可以寫出與 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: 我最初的問題來自我發(fā)現(xiàn)自己編寫的一小部分實用程序函數(shù) - 例如:

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

  • 檢查兩個Date是否代表同一天的日期時間;
  • 檢查某個日期是否在其他兩個日期指定的范圍內(nèi),但有時會包含在內(nèi),有時則不包含在內(nèi),具體取決于時間組件.
  • 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.

Joda Time 有這種功能嗎?

Does Joda Time have this type of functionality?

編輯 3: @Jon 關(guān)于我的第二個要求的問題,只是為了澄清:第二個要求是使用我的 Dates 有時代表一整天的結(jié)果 - 其中時間部分并不重要all - 有時表示日期時間(即,IMO,對于包含 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 表示一整天時,它的時間部分為零(例如,Date 的時間部分"是午夜),但語義規(guī)定范圍檢查是在結(jié)束日期包含在內(nèi)完成的.因為我只是將此檢查保留到 Date::before 和 Date::after,所以我必須將 1 天添加到結(jié)束日期 - 因此日期的時間部分為零時的特殊情況.

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.

推薦答案

好吧,我知道這是一個可預(yù)測的答案,但是...使用 喬達(dá)時間.它對日期"、瞬間"、一天中的某個時間"等有單獨的表示.它是一個比內(nè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.

如果這是您感興趣的唯一位日期/時間操作,那么它可能有點矯枉過正......但如果您使用內(nèi)置日期/時間 API 來處理任何事情重要的是,我強烈建議您盡快離開它,轉(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.

順便說一句,您應(yīng)該考慮您感興趣的時區(qū).Calendar 具有關(guān)聯(lián)的時區(qū),但 Date 沒有(它只是表示時間的瞬間,從 Unix 紀(jì)元開始以毫秒為單位).

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 日期或日歷的時間組件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 91在线网站| 免费视频二区 | av免费成人 | 91精品国产高清一区二区三区 | 国产成人免费视频 | 超碰av在线| 久久国际精品 | 久久大陆| 成人在线免费观看视频 | 成人免费视频观看视频 | 欧美黑人体内she精在线观看 | 久久天堂 | 一区二区三区视频在线观看 | 成人免费视频网站在线观看 | 精品亚洲永久免费精品 | 女人毛片a毛片久久人人 | 国产精品免费视频一区 | 视频一区二区三区在线观看 | 盗摄精品av一区二区三区 | 国产日韩精品视频 | 久久国产欧美日韩精品 | 欧美精品电影一区 | 日本黄视频在线观看 | 成人久久18免费网站图片 | 成人av电影在线 | 久久精品毛片 | 亚洲综合天堂 | 亚洲第一色站 | 免费成人在线网站 | 午夜久久久 | 国产精品久久九九 | 日韩一级免费电影 | 亚洲国产精品成人无久久精品 | 91综合网 | 久久精品小短片 | 久久久精品视频免费看 | 久久中文字幕一区 | 国产视频二区在线观看 | 最新av中文字幕 | 国产精品国产三级国产aⅴ原创 | 国产9999精品 |