pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

獲取 Java 日期或日歷的時(shí)間組件

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

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

是否有一種簡(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 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)于我的第二個(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)!

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

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 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)前星期幾的值)
主站蜘蛛池模板: 螺杆真空泵_耐腐蚀螺杆真空泵_水环真空泵_真空机组_烟台真空泵-烟台斯凯威真空 | 特种电缆厂家-硅橡胶耐高温电缆-耐低温补偿导线-安徽万邦特种电缆有限公司 | 行业分析:提及郑州火车站附近真有 特殊按摩 ?2025实地踩坑指南 新手如何避坑不踩雷 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 电车线(用于供电给电车的输电线路)-百科 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 济南网站策划设计_自适应网站制作_H5企业网站搭建_济南外贸网站制作公司_锐尚 | 企业微信营销_企业微信服务商_私域流量运营_艾客SCRM官网 | 交联度测试仪-湿漏电流测试仪-双85恒温恒湿试验箱-常州市科迈实验仪器有限公司 | 西安展台设计搭建_西安活动策划公司_西安会议会场布置_西安展厅设计西安旭阳展览展示 | 蜘蛛车-登高车-高空作业平台-高空作业车-曲臂剪叉式升降机租赁-重庆海克斯公司 | 消电检公司,消电检价格,北京消电检报告-北京设施检测公司-亿杰(北京)消防工程有限公司 | 警方提醒:赣州约炮论坛真的安全吗?2025年新手必看的网络交友防坑指南 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 劳动法网-专业的劳动法和劳动争议仲裁服务网 | 金属波纹补偿器厂家_不锈钢膨胀节价格_非金属伸缩节定制-庆达补偿器 | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 智能案卷柜_卷宗柜_钥匙柜_文件流转柜_装备柜_浙江福源智能科技有限公司 | 智慧食堂_食堂管理系统_食堂订餐_食堂消费系统—客易捷 | 深圳南财多媒体有限公司介绍| 山东成考网-山东成人高考网 | 苹果售后维修点查询,苹果iPhone授权售后维修服务中心 – 修果网 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 广州各区危化证办理_危险化学品经营许可证代办 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 冷凝锅炉_燃气锅炉_工业燃气锅炉改造厂家-北京科诺锅炉 | 密集架-手摇-智能-移动-价格_内蒙古档案密集架生产厂家 | 双相钢_双相不锈钢_双相钢圆钢棒_双相不锈钢报价「海新双相钢」 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 24位ADC|8位MCU-芯易德科技有限公司 | 电动球阀_不锈钢电动球阀_电动三通球阀_电动调节球阀_上海湖泉阀门有限公司 | 数显水浴恒温振荡器-分液漏斗萃取振荡器-常州市凯航仪器有限公司 | 工业机械三维动画制作 环保设备原理三维演示动画 自动化装配产线三维动画制作公司-南京燃动数字 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 |