本文介紹了如何獲取兩個日歷實例之間的天數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
如果有日期變化,我想找出兩個 Calendar
對象之間的天數(shù)差異,例如如果時鐘從 23:59-0:00 滴答作響,則應(yīng)該有天數(shù)差異.
I want to find the difference between two Calendar
objects in number of days if there is date change like If clock ticked from 23:59-0:00 there should be a day difference.
這是我寫的
public static int daysBetween(Calendar startDate, Calendar endDate) {
return Math.abs(startDate.get(Calendar.DAY_OF_MONTH)-endDate.get(Calendar.DAY_OF_MONTH));
}
但它不起作用,因為如果有月份差異,它只會在天之間產(chǎn)生差異,它毫無價值.
but its not working as it only gives difference between days if there is month difference its worthless.
推薦答案
在 Java 8 及更高版本中,我們可以簡單地使用 java.time 類.
In Java 8 and later, we could simply use the java.time classes.
hoursBetween = ChronoUnit.HOURS.between(calendarObj.toInstant(), calendarObj.toInstant());
daysBetween = ChronoUnit.DAYS.between(calendarObj.toInstant(), calendarObj.toInstant());
這篇關(guān)于如何獲取兩個日歷實例之間的天數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!