From cde91db7a91dbd1406b3eb700880ff8343de7ccb Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Tue, 26 Jan 2021 16:15:58 +0800
Subject: [PATCH] modify

---
 zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java |  218 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 182 insertions(+), 36 deletions(-)

diff --git a/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java b/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java
index 1f5e97c..07a0984 100644
--- a/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java
+++ b/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java
@@ -5,6 +5,7 @@
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -12,11 +13,24 @@
 
 /**
  * 日期格式化,转换工具类
- * 
+ *
  * @author Ron
  * @createTime 2014.08.30
  */
 public class DateUtil {
+	private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23,
+			23, 23, 24, 23, 22 };
+	private final static String[] constellationArr = new String[] { "摩羯座",
+			"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",
+			"天蝎座", "射手座", "摩羯座" };
+
+	public static final String DATE_FORMAT_STS = "yyyy-MM-dd'T'HH:mm:ss";
+
+	public static final String DATE_FORMAT_ST = "yyyy-MM-dd'T'HH:mm";
+
+	public static final String HH_mm = "HH:mm";
+
+	public static final String HHmm = "HHmm";
 
 	public static final String MONTH = "yyyy-MM";
 
@@ -24,11 +38,12 @@
 
 	public static final String DATE_FORMAT_MONGO = "yyyy-MM-dd'T'HH:mm:ss.SSS";
 
-	public static final String DATE_FORMAT_STS = "yyyy-MM-dd'T'HH:mm:ss";
+
 
 	public static final String DATE_FORMAT_MM = "yyyy-MM-dd HH:mm";
 
 	public static final String DATE_FORMAT_HH = "yyyy-MM-dd HH";
+
 
 	public static final String DATE_FORMAT_DD = "yyyy-MM-dd";
 
@@ -76,7 +91,7 @@
 
 	/**
 	 * 返回年份
-	 * 
+	 *
 	 * @param date
 	 *            日期
 	 * @return 返回年份
@@ -94,8 +109,36 @@
 	}
 
 	/**
+	 * Java通过生日计算星座
+	 *
+	 * @param month
+	 * @param day
+	 * @return
+	 */
+	public static String getConstellation(int month, int day) {
+		return day < dayArr[month - 1] ? constellationArr[month - 1]
+				: constellationArr[month];
+	}
+
+
+
+	/**
+	 *
+	 * @Title: getNextNMinute  获取date之后N分钟的时间
+	 * @Description: TODO
+	 * @author:罗凯
+	 * @param date
+	 * @return
+	 * Date    返回类型
+	 * @date 2016年8月3日 下午2:48:51
+	 * @throws
+	 */
+	public static Date getNextNMinute(Date date,int n){
+		return new Date(date.getTime()+(n*60*1000));
+	}
+	/**
 	 * 返回月份
-	 * 
+	 *
 	 * @param date
 	 *            日期
 	 * @return 返回月份
@@ -107,14 +150,14 @@
 			c.setTime(date);
 			return c.get(Calendar.MONTH) + 1;
 		} catch (Exception e) {
-			
+
 		}
 		return 0;
 	}
 
 	/**
 	 * 日期转字符串
-	 * 
+	 *
 	 * @param date
 	 * @param format
 	 * @return
@@ -130,7 +173,7 @@
 
 	/**
 	 * 字符串转日期
-	 * 
+	 *
 	 * @param dateStr
 	 * @param format
 	 * @return
@@ -141,15 +184,23 @@
 		try {
 			return fmt.parse(dateStr);
 		} catch (ParseException e) {
-			
+
 		}
 		return null;
 	}
- 
+
+	/**
+	 * 获取一个编码
+	 * 获取一个当前时间并且加入了四位的随机字母
+	 * @return
+	 */
+	public static String getTimeCode() {
+		return dateToString(new Date(), "yyyyMMddssSSS")+StringUtils.getRandomString(4);
+	}
 
 	/**
 	 * 判断给定的日期是一周中的第几天,注意:按照中国的习惯,周日是第七天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -175,7 +226,7 @@
 
 	/**
 	 * 指定时间的下一天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -191,7 +242,7 @@
 			cal.add(Calendar.DATE, 1);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -199,7 +250,7 @@
 
 	/**
 	 * 指定时间的前一天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -215,7 +266,7 @@
 			cal.add(Calendar.DATE, -1);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -223,7 +274,7 @@
 
 	/**
 	 * 指定时间的下N天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -239,7 +290,7 @@
 			cal.add(Calendar.DATE, nDay);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -247,7 +298,7 @@
 
 	/**
 	 * 指定时间的前N天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -263,7 +314,7 @@
 			cal.add(Calendar.DATE, -nDay);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -271,7 +322,7 @@
 
 	/**
 	 * 获取一天的起始时间
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -288,7 +339,7 @@
 		try {
 			return fmt.parse(dateStr);
 		} catch (ParseException e) {
-			
+
 		}
 
 		return date;
@@ -296,7 +347,7 @@
 
 	/**
 	 * 获取一天的结束时间
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -313,7 +364,7 @@
 		try {
 			return fmt.parse(dateStr);
 		} catch (ParseException e) {
-			
+
 		}
 
 		return date;
@@ -321,7 +372,7 @@
 
 	/**
 	 * currentDat是否在referenceDate日期之前
-	 * 
+	 *
 	 * @param referenceDate
 	 * @param currentDat
 	 * @return
@@ -339,7 +390,7 @@
 
 	/**
 	 * currentDat是否在referenceDate日期之后
-	 * 
+	 *
 	 * @param referenceDate
 	 * @param currentDat
 	 * @return
@@ -354,10 +405,39 @@
 		}
 		return currentDate.after(referenceDate);
 	}
+	public static long getDifTimeMin(Date beginTime,Date endTime){
+		return (endTime.getTime()-beginTime.getTime())/1000/ 60;
+	}
+	public static List<Date> getFutureDay(Date date, int start,int end){
+		List<Date> dates=new ArrayList<Date>();
+		for (int i = 0; i < end-start; i++) {
+			dates.add(DateUtil.nextNDate(date,i));
+		}
+		return dates;
+	}
+	/**
+	 * 字符串转日期 如果转换失败就返回一个当前时间date
+	 *
+	 * @param dateStr
+	 * @param format
+	 * @return
+	 */
+	public static Date stringToDateNew(String dateStr, String format) {
 
+		if (StringUtils.isBlank(dateStr) || StringUtils.isBlank(format)) {
+			return new Date();
+		}
+		DateFormat fmt = new SimpleDateFormat(format);
+		try {
+			return fmt.parse(dateStr);
+		} catch (ParseException e) {
+
+		}
+		return new Date();
+	}
 	/**
 	 * 判断currentDate是否在startDate和endDate之间,不包括startDate和endDate
-	 * 
+	 *
 	 * @param startDate
 	 * @param endDate
 	 * @param currentDate
@@ -375,11 +455,11 @@
 		return false;
 	}
 
-	 
+
 
 	/**
 	 * 获取startDate到endDate之间的星期day(中文星期)不包括startDate和endDate
-	 * 
+	 *
 	 * @param startDate
 	 * @param endDate
 	 * @param day
@@ -404,11 +484,11 @@
 		return listDate;
 	}
 
-	 
+
 
 	/**
 	 * date转换成Timestamp
-	 * 
+	 *
 	 * @param date
 	 * @param format
 	 * @return
@@ -428,7 +508,7 @@
 
 	/**
 	 * 获取早中晚
-	 * 
+	 *
 	 * @param time
 	 * @return
 	 */
@@ -451,7 +531,7 @@
 
 	/**
 	 * 获取早中晚的开始时间
-	 * 
+	 *
 	 * @param date
 	 * @param time
 	 * @return
@@ -475,7 +555,7 @@
 
 	/**
 	 * 获取早中晚的结束时间
-	 * 
+	 *
 	 * @param date
 	 * @param time
 	 * @return
@@ -499,7 +579,7 @@
 
 	/**
 	 * 得到几天前的时间
-	 * 
+	 *
 	 * @param d
 	 * @param day
 	 * @return
@@ -513,7 +593,7 @@
 
 	/**
 	 * 得到几天后的时间
-	 * 
+	 *
 	 * @param d
 	 * @param day
 	 * @return
@@ -527,7 +607,7 @@
 
 	/**
 	 * 将日期类型格式化成字符串
-	 * 
+	 *
 	 * @param date
 	 * @return 格式化后日期字符串
 	 * @throws ParseException
@@ -543,7 +623,7 @@
 
 	/**
 	 * 获取时间戳
-	 * 
+	 *
 	 * @return
 	 */
 	public static String getTimeMark() {
@@ -615,4 +695,70 @@
 		}
 		return um;
 	}
+
+	/**
+	 * 根据单位计算目标日期
+	 *
+	 * @param num 距离
+	 * @param unit 日期单位 Y/M/D
+	 * @return
+	 */
+	public static Date calDate(Integer num, String unit) {
+	    Calendar calendar = Calendar.getInstance();
+	    Date targetDate = null;
+	    switch (unit) {
+            case "Y":
+                calendar.add(Calendar.YEAR, num);
+                targetDate = calendar.getTime();
+                break;
+            case "M":
+                calendar.add(Calendar.MONTH, num);
+                targetDate = calendar.getTime();
+                break;
+            case "D":
+                calendar.add(Calendar.DAY_OF_MONTH, num);
+                targetDate = calendar.getTime();
+                break;
+            default:
+                targetDate = stringToDate("9999-12-31", DATE_FORMAT_DD);
+
+        }
+
+		return targetDate;
+	}
+
+	public static String getAgeForBirthDay(Date birthDay) {
+		Calendar cal = Calendar.getInstance();
+		if (birthDay == null) {
+		    return "-";
+        }
+		// 出生日期晚于当前时间,无法计算
+		if (cal.before(birthDay)) {
+			return "-";
+		}
+        // 当前年份
+		int yearNow = cal.get(Calendar.YEAR);
+        // 当前月份
+		int monthNow = cal.get(Calendar.MONTH);
+        // 当前日期
+		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
+		cal.setTime(birthDay);
+		int yearBirth = cal.get(Calendar.YEAR);
+		int monthBirth = cal.get(Calendar.MONTH);
+		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
+        // 计算整岁数
+		Integer age = yearNow - yearBirth;
+		if (monthNow <= monthBirth) {
+			if (monthNow == monthBirth) {
+				if (dayOfMonthNow < dayOfMonthBirth) {
+					// 当前日期在生日之前,年龄减一
+					age--;
+				}
+			} else {
+				age--;
+			}
+		}
+
+		return age.toString();
+	}
 }

--
Gitblit v1.9.1