本文介紹了如何獲取兩個日歷實例之間的天數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如果有日期變化,我想找出兩個 Calendar
對象之間的天數差異,例如如果時鐘從 23:59-0:00 滴答作響,則應該有天數差異.
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));
}
但它不起作用,因為如果有月份差異,它只會在天之間產生差異,它毫無價值.
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());
這篇關于如何獲取兩個日歷實例之間的天數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!