From 98b1a0affd69bbe63223c21fdd2c404e8bedfccb Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 20 五月 2020 17:25:08 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/div' into 2.1.2 --- utils/src/main/java/org/yeshi/utils/DateUtil.java | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 213 insertions(+), 20 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/DateUtil.java b/utils/src/main/java/org/yeshi/utils/DateUtil.java index 82ef3d4..915a83b 100644 --- a/utils/src/main/java/org/yeshi/utils/DateUtil.java +++ b/utils/src/main/java/org/yeshi/utils/DateUtil.java @@ -86,6 +86,72 @@ return dateInfo; } + public static String dateDiff4(Date startTime, Date endTime) throws Exception { + + String datatime = "0鍒�"; + + long nm = 1000 * 60;// 涓�鍒嗛挓鐨勬绉掓暟 + long nh = 1000 * 60 * 60;// 涓�灏忔椂鐨勬绉掓暟 + long nd = 1000 * 24 * 60 * 60;// 涓�澶╃殑姣鏁� + + // 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮� + long diff = endTime.getTime() - startTime.getTime(); + long day = diff / nd;// 璁$畻宸灏戝ぉ + long hour = diff % nd / nh;// 璁$畻宸灏戝皬鏃� + long min = diff % nd % nh / nm;// 璁$畻宸灏戝垎閽� + + if (day > 0) { + datatime = day + "澶�"; + } else { + if (hour > 0) { + datatime = hour + "鏃�"; + } else { + if (min < 0) + min = 0; + datatime = min + "鍒�"; + } + } + return datatime; + } + + public static String dateDiff5(Date startTime, Date endTime) throws Exception { + long nm = 1000 * 60;// 涓�鍒嗛挓鐨勬绉掓暟 + long nh = 1000 * 60 * 60;// 涓�灏忔椂鐨勬绉掓暟 + long nd = 1000 * 24 * 60 * 60;// 涓�澶╃殑姣鏁� + + // 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮� + long diff = endTime.getTime() - startTime.getTime(); + long day = diff / nd;// 璁$畻宸灏戝ぉ + long hour = diff % nd / nh;// 璁$畻宸灏戝皬鏃� + long min = diff % nd % nh / nm;// 璁$畻宸灏戝垎閽� + long second = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); // 璁$畻宸灏戠 + + String datatime = ""; + if (day > 0) { + datatime = day + "澶�"; + } else if (hour > 0) { + datatime = hour + "灏忔椂"; + } else if (min > 0) { + datatime = min + "鍒嗛挓"; + } else if (second > 0) { + datatime = second + "绉�"; + } + return datatime; + } + + public static long dateDiffMin(Date startTime, Date endTime) throws Exception { + long nm = 1000 * 60;// 涓�鍒嗛挓鐨勬绉掓暟 + long nh = 1000 * 60 * 60;// 涓�灏忔椂鐨勬绉掓暟 + long nd = 1000 * 24 * 60 * 60;// 涓�澶╃殑姣鏁� + + // 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮� + long diff = endTime.getTime() - startTime.getTime(); + long day = diff / nd;// 璁$畻宸灏戝ぉ + long hour = diff % nd / nh;// 璁$畻宸灏戝皬鏃� + long min = diff % nd % nh / nm + day * 24 * 60 + hour * 60;// 璁$畻宸灏戝垎閽� + return min; + } + /** * 閫氳繃鏃堕棿绉掓绉掓暟鍒ゆ柇涓や釜鏃堕棿鐨勯棿闅� * @@ -186,6 +252,23 @@ * @return * @throws ParseException */ + public static Date plusDayReturnDate(int num, String nowDate) throws ParseException { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + Date currdate = format.parse(nowDate); + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.DATE, num); + return ca.getTime(); + } + + /** + * 鎸囧畾鏃ユ湡鍔犱笂澶╂暟鍚庣殑鏃ユ湡 + * + * @param num 澧炲姞鐨勫ぉ鏁� + * @param nowDate 鍒涘缓鏃堕棿 + * @return + * @throws ParseException + */ public static String plusDay(int num, Date currdate) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); @@ -210,9 +293,10 @@ ca.add(Calendar.DATE, num); return ca.getTime(); } - + /** * 鍔犱笂鏈堜唤 + * * @param currdate * @param num * @return @@ -223,9 +307,10 @@ ca.add(Calendar.MONTH, num); return ca.getTime(); } - + /** * 鍔犱笂骞翠唤 + * * @param currdate * @param num * @return @@ -237,6 +322,26 @@ return ca.getTime(); } + /** + * 鍑忓幓鏈堜唤 + * + * @param currdate + * @param num + * @return + */ + public static Date reduceMonth(Date currdate, int num) { + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.MONTH, -num); + return ca.getTime(); + } + + public static Date reduceDay(Date currdate, int num) { + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.DATE, -num); + return ca.getTime(); + } /** * 鎸囧畾鏃ユ湡鍑忓幓澶╂暟鍚庣殑鏃ユ湡 @@ -249,6 +354,28 @@ public static String reduceDay(int num, String nowDate) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date currdate = format.parse(nowDate); + + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.DATE, -num); // 鏃ユ湡鍑� 濡傛灉涓嶅鍑忎細灏嗘湀鍙樺姩 + + return format.format(ca.getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鍑忓幓澶╂暟鍚庣殑鏃ユ湡 + * + * @param num 鍑忓幓鐨勫ぉ鏁� + * @param nowDate 鍒涘缓鏃堕棿 + * @return + * @throws ParseException + */ + public static String reduceDay2(int num, Date date) throws ParseException { + // 璁剧疆瑕佽幏鍙栧埌浠�涔堟牱鐨勬椂闂� + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + // 鑾峰彇String绫诲瀷鐨勬椂闂� + String date_str = format.format(date); + Date currdate = format.parse(date_str); Calendar ca = Calendar.getInstance(); ca.setTime(currdate); @@ -396,20 +523,20 @@ * 璁$畻涓ゆ椂闂存湀宸� * * @param startDate <String> - * @param endDate <String> + * @param endDate <String> * @return int * @throws ParseException */ public static int getMonthSpace(String startDate, String endDate) throws ParseException { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); - return getMonthSpace(sdf.parse(startDate), sdf.parse(endDate)); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); + return getMonthSpace(sdf.parse(startDate), sdf.parse(endDate)); } /** * 璁$畻涓ゆ椂闂存湀宸� * * @param startDate <String> - * @param endDate <String> + * @param endDate <String> * @return int * @throws ParseException */ @@ -419,32 +546,98 @@ start.setTime(startDate); end.setTime(endDate); int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH); - int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12; + int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12; return Math.abs(month + result); } - public static Date getTodayStartTime() { Calendar todayStart = Calendar.getInstance(); - todayStart.set(Calendar.HOUR_OF_DAY,0); - todayStart.set(Calendar.MINUTE,0); - todayStart.set(Calendar.SECOND,0); - todayStart.set(Calendar.MILLISECOND,0); + todayStart.set(Calendar.HOUR_OF_DAY, 0); + todayStart.set(Calendar.MINUTE, 0); + todayStart.set(Calendar.SECOND, 0); + todayStart.set(Calendar.MILLISECOND, 0); return todayStart.getTime(); } - + public static Date getTodayEndTime() { Calendar todayEnd = Calendar.getInstance(); - todayEnd.set(Calendar.HOUR_OF_DAY,23); - todayEnd.set(Calendar.MINUTE,59); - todayEnd.set(Calendar.SECOND,59); - todayEnd.set(Calendar.MILLISECOND,999); + todayEnd.set(Calendar.HOUR_OF_DAY, 23); + todayEnd.set(Calendar.MINUTE, 59); + todayEnd.set(Calendar.SECOND, 59); + todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime(); } - + + /** + * 鑾峰彇鎸囧畾骞存湀鐨勭涓�澶� + * + * @param yearMonth + * @return + */ + public static String getFirstDayOfMonth(String yearMonth) { + int year = Integer.parseInt(yearMonth.split("-")[0]); // 骞� + int month = Integer.parseInt(yearMonth.split("-")[1]); // 鏈� + return getFirstDayOfMonth(year, month); + } + + /** + * 鑾峰彇鎸囧畾骞存湀鐨勭涓�澶� + * + * @param year + * @param month + * @return + */ + public static String getFirstDayOfMonth(int year, int month) { + Calendar cal = Calendar.getInstance(); + // 璁剧疆骞翠唤 + cal.set(Calendar.YEAR, year); + // 璁剧疆鏈堜唤 + cal.set(Calendar.MONTH, month - 1); + // 鑾峰彇鏌愭湀鏈�灏忓ぉ鏁� + int firstDay = cal.getMinimum(Calendar.DATE); + // 璁剧疆鏃ュ巻涓湀浠界殑鏈�灏忓ぉ鏁� + cal.set(Calendar.DAY_OF_MONTH, firstDay); + // 鏍煎紡鍖栨棩鏈� + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(cal.getTime()); + } + + /** + * 鑾峰彇鎸囧畾骞存湀鐨勬渶鍚庝竴澶� + * + * @param yearMonth + * @return + */ + public static String getLastDayOfMonth(String yearMonth) { + int year = Integer.parseInt(yearMonth.split("-")[0]); // 骞� + int month = Integer.parseInt(yearMonth.split("-")[1]); // 鏈� + return getLastDayOfMonth(year, month); + } + + /** + * 鑾峰彇鎸囧畾骞存湀鐨勬渶鍚庝竴澶� + * + * @param year + * @param month + * @return + */ + public static String getLastDayOfMonth(int year, int month) { + Calendar cal = Calendar.getInstance(); + // 璁剧疆骞翠唤 + cal.set(Calendar.YEAR, year); + // 璁剧疆鏈堜唤 + cal.set(Calendar.MONTH, month - 1); + // 鑾峰彇鏌愭湀鏈�澶уぉ鏁� + int lastDay = cal.getActualMaximum(Calendar.DATE); + // 璁剧疆鏃ュ巻涓湀浠界殑鏈�澶уぉ鏁� + cal.set(Calendar.DAY_OF_MONTH, lastDay); + // 鏍煎紡鍖栨棩鏈� + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(cal.getTime()); + } public static void main(String[] args) throws ParseException { - System.out.println(getMonthSpace("2012-02", "2012-02")); - } + System.out.println(getMonthSpace("2012-02", "2012-02")); + } } \ No newline at end of file -- Gitblit v1.8.0