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

在Java中求和兩個日期

Sum two dates in Java(在Java中求和兩個日期)
本文介紹了在Java中求和兩個日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

如何在 Java 中添加兩個日期?

How can I add two dates in Java?

示例:2010-01-14 19:16:17"0000-10-03 01:10:05"之和
將導致2010-11-17 20:26:22".

Example: The sum of "2010-01-14 19:16:17" "0000-10-03 01:10:05"
would result in "2010-11-17 20:26:22".

我知道如何使用日歷并逐個字段添加.

I know how to do it using Calendar and adding field by field.

還有其他方法可以一次將它們全部(年/月/日/小時/分鐘/秒)求和嗎?

Is any other way to sum them all (year/month/day/hour/minute/second) at once?

推薦答案

如果你使用的是 Date 對象,你可以這樣做:

If you are using the Date object, you can just do:

Date d1 = ...
Date d2 = ...

long sum = d1.getTime() + d2.getTime();

Date sumDate = new Date(sum);

代碼使用 .getTime() 方法返回自紀元以來的毫秒數.不用說 Date 類有很多問題,應該盡可能避免.

The code uses the .getTime() method that returns the number of milliseconds since the epoch. Needless to say the Date class has a lot of problems and should be avoided when possible.

您想對其他類型求和嗎?

Do you want to sum other types instead?

更新:對于 Calendar,我會執行以下操作(基于 javadocs):

Update: for Calendar, I would do the following (based on javadocs):

Calendar c1 = ...
Calendar c2 = ...
long sum = c1.getTimeInMillis() + c2.getTimeInMillis();
Calendar sumCalendar = (Calendar)c1.clone();
sumCalendar.setTimeInMillis(sum);

更新:正如史蒂夫所說,如果您在此處提供的日期假定第二個日期與 Java 紀元有關,則此方法有效.如果您確實想從0"年開始,那么您需要考慮這一點(通過減去您的紀元時間).

UPDATED: As Steve stated, this works if the Date you presented here assumes that the second date is with respect to the Java epoch. If you do want to start with year "0", then you need to account for that (by subtracting your epoch time).

這篇關于在Java中求和兩個日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
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 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 男女网站在线观看 | 成人综合伊人 | 成人免费视频 | 一级欧美一级日韩片免费观看 | 另类二区| 欧美在线a | 亚洲一区二区视频 | 成人激情视频在线 | 欧美一级免费看 | 男女羞羞视频在线 | 亚洲v日韩v综合v精品v | 成人av网站在线观看 | 毛片免费在线 | 国产成人精品免高潮在线观看 | 国产精品一二三区在线观看 | 国产精品久久久久无码av | 在线看av的网址 | 欧美精品乱码久久久久久按摩 | 亚洲精品一区av在线播放 | 欧美a在线看 | 欧美日日日日bbbbb视频 | 福利视频网站 | 欧美日韩中文字幕在线 | 欧美韩一区二区 | 久久免费大片 | 淫片一级国产 | www.一区二区三区 | 久久成人18免费网站 | 欧洲一区二区三区 | 色综合久久久久 | 在线91 | 国产欧美一区二区精品久导航 | 国产精品视频在线免费观看 | 91视频免费 | 欧美精 | 国产精品国产a级 | 我想看一级黄色毛片 | 国产永久免费 | 可以免费观看的av | 国产福利视频 | 欧美性生活一区二区三区 |