java计算时间差 Java问题通用解决代码
生活随笔
收集整理的這篇文章主要介紹了
java计算时间差 Java问题通用解决代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java實現計算時間差 正式版:
? ? ??/** ?????? * 計算時間差,求出兩者相隔的時間 ?????? * ?????? *?@param?nowDate ?????? *??????????? 當前時間 ?????? *?@param?calculateDate ?????? *??????????? 計算的時間 ?????? *?@return ?????? */ ??????public?static?long?calculateTime(Date nowDate, Date?calculateDate) { ????????????long?ret = 0; ????????????try?{ ??????????????????long?t = nowDate.getTime() -?calculateDate.getTime(); ??????????????????long?day?= t / (1000 * 60 * 60 * 24); ??????????????????long?hour?= t / (1000 * 60 * 60); ??????????????????long?minute?= t / (1000 * 60); ??????????????????long?second?= t / 1000; ? ??????????????????//?ret?= ? ????????????}?catch?(Exception e) { ????????????} ????????????return?ret; ??????} ? ? ? ???/** ?????? * 計算時間差,求出時間相隔的天數,值是四舍五入?<br><pre> ?????? */ ???????public?static?long?calculateTime4Days(Date nowDate, Date calculateDate) { ?????????????long?ret = 0; ?????????????try?{ ???????????????????long?t = nowDate.getTime() - calculateDate.getTime(); //????????????????long day = d / (1000 * 60 * 60 * 24); ???????????????????// 四舍五入 ??????????????????Double td = 0.0; ??????????????????td += t; ???????????????????long?day = Math.round(td / (1000 * 60 * 60 * 24)); ?????????????????? ??????????????????ret = day; ????????????}?catch?(Exception e) { ????????????} ?????????????return?ret; ??????} ? ?
#2013-6-7補充一個計算月份的,正式版:
? ? ???//************************************************************************** ???????/** ?????? * 計算時間差,求出時間相隔的月份?<br><pre> ?????? * 編寫者:?sven ?????? * Email:swnic?@isoftstone.com ?????? * 創建時間:2013?-6-?7 下午02:21:58?</pre> ?????? *?@param?boolean isBuc 是否補充:不足一月算一月,true;不足一月不算一月,false; ?????? *?@return?long 說明 ?????? *?@throws?異常類型 說明 ?????? */ ???????//************************************************************************** ???????public?static?long?calculateTime4Month(Date startDate, Date endDate,?boolean?isBuc) { ?????????????long?monthDiff = 0; ? ?????????????try?{ //????????????????String start = "2011-06-12"; //????????????????String end = "2012-06-01"; //????????????????SimpleDateFormat?fmt?= new SimpleDateFormat("yyyy-MM-dd"); //????????????????Date startDate = fmt.parse(start); //????????????????Date endDate = fmt.parse(end); ? ??????????????????Calendar starCal = Calendar.?getInstance(); ??????????????????starCal.setTime(startDate); ? ???????????????????int?sYear = starCal.get(Calendar.YEAR); ???????????????????int?sMonth = starCal.get(Calendar.MONTH); ???????????????????int?sDay = starCal.get(Calendar.DAY_OF_MONTH); ? ??????????????????Calendar endCal = Calendar.?getInstance(); ??????????????????endCal.setTime(endDate); ???????????????????int?eYear = endCal.get(Calendar.YEAR); ???????????????????int?eMonth = endCal.get(Calendar.MONTH); ???????????????????int?eDay = endCal.get(Calendar.DAY_OF_MONTH); ? ??????????????????monthDiff = ((eYear - sYear) * 12 + (eMonth - sMonth)); ? //????????????????boolean isBuc = true; ???????????????????if?(isBuc) { ?????????????????????????// 不足一月不算一月減1,默認做法,參考公式 ?????????????????????????// 依據天數補充,不足一月算一月加1 ?????????????????????????if?(sDay < eDay) { ??????????????????????????????monthDiff = monthDiff + 1; ????????????????????????}?else?if?(sDay > eDay) { ??????????????????????????????monthDiff = monthDiff - 1; ????????????????????????} ??????????????????} ????????????}?catch?(Exception e) { ????????????} ? ?????????????return?monthDiff; ??????}
參考: cnblogs -?Java中計算時間差
轉載于:https://www.cnblogs.com/svennee/p/4082852.html
總結
以上是生活随笔為你收集整理的java计算时间差 Java问题通用解决代码的全部內容,希望文章能夠幫你解決所遇到的問題。