From f9d2e4231ed3942dcbe20274e6ad12b7f0d367a8 Mon Sep 17 00:00:00 2001 From: yujian <yujian@123.com> Date: 星期四, 15 八月 2019 15:59:38 +0800 Subject: [PATCH] 日期计算 --- utils/src/main/java/org/yeshi/utils/DateUtil.java | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 43 insertions(+), 12 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/DateUtil.java b/utils/src/main/java/org/yeshi/utils/DateUtil.java index 3931d4b..49336da 100644 --- a/utils/src/main/java/org/yeshi/utils/DateUtil.java +++ b/utils/src/main/java/org/yeshi/utils/DateUtil.java @@ -142,6 +142,22 @@ return format.format(ca.getTime()); } + + /** + * 鎸囧畾鏃ユ湡鍔犱笂澶╂暟鍚庣殑鏃ユ湡 + * + * @param num 澧炲姞鐨勫ぉ鏁� + * @param nowDate 鍒涘缓鏃堕棿 + * @return + * @throws ParseException + */ + public static Date plusDayDate(int num, Date currdate) { + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.DATE, num); + return ca.getTime(); + } + /** * 鎸囧畾鏃ユ湡鍑忓幓澶╂暟鍚庣殑鏃ユ湡 @@ -192,16 +208,31 @@ * @return 鐩稿樊澶╂暟 * @throws ParseException calendar 瀵规棩鏈熻繘琛屾椂闂存搷浣� getTimeInMillis() 鑾峰彇鏃ユ湡鐨勬绉掓樉绀哄舰寮� */ - public static int daysBetween(Date smdate, Date bdate) throws ParseException { + public static int daysBetween(Date minDate, Date maxDate) throws ParseException { Calendar cal = Calendar.getInstance(); - cal.setTime(smdate); - long time1 = cal.getTimeInMillis(); - cal.setTime(bdate); - long time2 = cal.getTimeInMillis(); - long between_days = (time2 - time1) / (1000 * 3600 * 24); + cal.setTime(minDate); + long minTime = cal.getTimeInMillis(); + cal.setTime(maxDate); + long maxTime = cal.getTimeInMillis(); + long between_days = (maxTime - minTime) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } + + /** + * 瀛楃涓叉棩鏈熸牸寮忕殑璁$畻 + * + * @param smdate + * @param bdate + * @return 鍗曚綅澶╂暟 + * @throws ParseException + */ + public static int daysBetween2(Date minDate, Date maxDate) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return daysBetween(sdf.format(minDate), sdf.format(maxDate)); + } + + /** * 瀛楃涓叉棩鏈熸牸寮忕殑璁$畻 * @@ -210,14 +241,14 @@ * @return * @throws ParseException */ - public static int daysBetween(String smdate, String bdate) throws ParseException { + public static int daysBetween(String minDate, String maxDate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); - cal.setTime(sdf.parse(smdate)); - long time1 = cal.getTimeInMillis(); - cal.setTime(sdf.parse(bdate)); - long time2 = cal.getTimeInMillis(); - long between_days = (time2 - time1) / (1000 * 3600 * 24); + cal.setTime(sdf.parse(minDate)); + long minTime = cal.getTimeInMillis(); + cal.setTime(sdf.parse(maxDate)); + long maxTime = cal.getTimeInMillis(); + long between_days = (maxTime - minTime) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } -- Gitblit v1.8.0