|
@@ -0,0 +1,505 @@
|
|
|
+package com.yh.saas.plugin.yiliangyiyun.util;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import com.winsea.svc.base.base.util.DateUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 计算工作时长的公用方法
|
|
|
+ *
|
|
|
+ * @author XinGuang
|
|
|
+ */
|
|
|
+public class CrewUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取年龄
|
|
|
+ *
|
|
|
+ * @param birthDay
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getAge(Date birthDay) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ if (cal.before(birthDay)) { // 出生日期晚于当前时间,无法计算
|
|
|
+ throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ int age = yearNow - yearBirth; // 计算整岁数
|
|
|
+ if (monthNow <= monthBirth) {
|
|
|
+ if (monthNow == monthBirth) {
|
|
|
+ if (dayOfMonthNow < dayOfMonthBirth)
|
|
|
+ age--;// 当前日期在生日之前,年龄减一
|
|
|
+ } else {
|
|
|
+ age--;// 当前月份在生日之前,年龄减一
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return String.valueOf(age);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取合同的下船日期
|
|
|
+ *
|
|
|
+ * @param beginDate
|
|
|
+ * @param monthNum
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getEndDate(Date beginDate, int monthNum) {
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FMT_YYYY_MM_DD);
|
|
|
+ String beginTime = sdf.format(beginDate);
|
|
|
+ String[] times = beginTime.split("-");
|
|
|
+ String year = times[0];
|
|
|
+ String month = times[1];
|
|
|
+ String day = times[2];
|
|
|
+ year = String.valueOf(Integer.parseInt(year) + (monthNum / 12));
|
|
|
+ month = String.valueOf(Integer.parseInt(month) + (monthNum % 12));
|
|
|
+ String yearMonth = year + "-" + month;
|
|
|
+ switch (month) {
|
|
|
+ case "2":
|
|
|
+ if (Integer.parseInt(day) > 28) {
|
|
|
+ return DateUtils.getMonthLastDay(yearMonth);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "4":
|
|
|
+ if ("31".equals(day)) {
|
|
|
+ return DateUtils.getMonthLastDay(yearMonth);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "6":
|
|
|
+ if ("31".equals(day)) {
|
|
|
+ return DateUtils.getMonthLastDay(yearMonth);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "9":
|
|
|
+ if ("31".equals(day)) {
|
|
|
+ return DateUtils.getMonthLastDay(yearMonth);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "11":
|
|
|
+ if ("31".equals(day)) {
|
|
|
+ return DateUtils.getMonthLastDay(yearMonth);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return sdf.parse(yearMonth + "-" + day);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 首字母转大写
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getConvert(String str) {
|
|
|
+ String first = str.substring(0, 1);
|
|
|
+ String after = str.substring(1); // substring(1),获取索引位置1后面所有剩余的字符串
|
|
|
+ first = first.toUpperCase();
|
|
|
+ str = first + after;
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取行列
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getCellRowColumn(String str) {
|
|
|
+
|
|
|
+ StringBuilder strBuilder = new StringBuilder();
|
|
|
+ StringBuilder numBuilder = new StringBuilder();
|
|
|
+
|
|
|
+ for (int i = 0; i < str.length(); i++) { // 循环遍历字符串
|
|
|
+ if (Character.isDigit(str.charAt(i))) { // 用char包装类中的判断数字的方法判断每一个字符
|
|
|
+ numBuilder.append(str.charAt(i));
|
|
|
+ }
|
|
|
+ if (Character.isLetter(str.charAt(i))) { // 用char包装类中的判断字母的方法判断每一个字符
|
|
|
+ strBuilder.append(str.charAt(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ list.add(strBuilder.toString());
|
|
|
+ list.add(numBuilder.toString());
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取列数
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getColumnNum(String str) {
|
|
|
+ str = str.toUpperCase();
|
|
|
+ char[] ch = str.toCharArray();
|
|
|
+ int a = 0;
|
|
|
+ for (int i = 0; i < ch.length; i++) {
|
|
|
+ a += getNum(ch[i]) * (int) Math.pow(26, ch.length - i - 1);
|
|
|
+ }
|
|
|
+ return a;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字母
|
|
|
+ *
|
|
|
+ * @param ch
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getNum(char ch) {
|
|
|
+ int index = (char) ch;
|
|
|
+ if (index >= 97) {
|
|
|
+ index = index - (97 - 65);
|
|
|
+ }
|
|
|
+ return index - 64;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取首字母对应的数字
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Integer getStrNum(String str) {
|
|
|
+ String first = str.substring(0, 1).toUpperCase();
|
|
|
+
|
|
|
+ int num = (int) first.getBytes()[0];
|
|
|
+ return num - 64;
|
|
|
+ }
|
|
|
+
|
|
|
+ private CrewUtils() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取时间
|
|
|
+ *
|
|
|
+ * @param nextDate 后一个时间
|
|
|
+ * @param previousDate 前一个时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getTimes(Date nextDate, Date previousDate) {
|
|
|
+ String times = "";
|
|
|
+ Calendar c1 = Calendar.getInstance();
|
|
|
+ Calendar c2 = Calendar.getInstance();
|
|
|
+ c1.setTime(nextDate);
|
|
|
+ c2.setTime(previousDate);
|
|
|
+ int[] timeArr = getTimeIntervalArray(c1, c2);
|
|
|
+ if (timeArr[0] > 0) {
|
|
|
+ times += timeArr[0] + "个月";
|
|
|
+ }
|
|
|
+ if (timeArr[0] >= 0 && timeArr[1] > 0) {
|
|
|
+ times += timeArr[1] + "天";
|
|
|
+ }
|
|
|
+ return times;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int[] getTimesArray(Date nextDate, Date previousDate) {
|
|
|
+ Calendar c1 = Calendar.getInstance();
|
|
|
+ Calendar c2 = Calendar.getInstance();
|
|
|
+ c1.setTime(nextDate);
|
|
|
+ c2.setTime(previousDate);
|
|
|
+
|
|
|
+ return getTimeIntervalArray(c1, c2);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int[] getTimeIntervalArray(Calendar nextDate, Calendar previousDate) {
|
|
|
+ int year = nextDate.get(Calendar.YEAR) - previousDate.get(Calendar.YEAR);
|
|
|
+ int month = nextDate.get(Calendar.MONTH) - previousDate.get(Calendar.MONTH);
|
|
|
+ int day = nextDate.get(Calendar.DAY_OF_MONTH) - previousDate.get(Calendar.DAY_OF_MONTH);
|
|
|
+ if (day < 0) {
|
|
|
+ // 计算截止日期的上一个月有多少天,补上去
|
|
|
+ Calendar tempDate = (Calendar) nextDate.clone();
|
|
|
+ tempDate.add(Calendar.MONTH, -1);// 获取截止日期的上一个月
|
|
|
+ day += tempDate.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+
|
|
|
+ // nextDate是月底最后一天,且day=这个月的天数,即是刚好一整个月,比如20160131~20160229,day=29,实则为1个月
|
|
|
+ if (nextDate.get(Calendar.DAY_OF_MONTH) == nextDate.getActualMaximum(Calendar.DAY_OF_MONTH)// 日期为月底最后一天
|
|
|
+ && day >= nextDate.getActualMaximum(Calendar.DAY_OF_MONTH)) {// day刚好是nextDate一个月的天数,或大于nextDate月的天数(比如2月可能只有28天)
|
|
|
+ day = 0;// 因为这样判断是相当于刚好是整月了,那么不用向 month 借位,只需将 day 置 0
|
|
|
+ } else {// 向month借一位
|
|
|
+ month--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (month < 0) {
|
|
|
+ month += 12;
|
|
|
+ year--;
|
|
|
+ }
|
|
|
+ day = day + 1;
|
|
|
+ month += year * 12;
|
|
|
+
|
|
|
+ return new int[]{month, day};
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * List 转 String
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getListTurnArray(List<String> list) {
|
|
|
+ String str = "";
|
|
|
+ if (null != list && list.size() > 0) {
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ str = str + "'" + list.get(i) + "',";
|
|
|
+ }
|
|
|
+ str = str.substring(0, str.length() - 1);
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成工作时间
|
|
|
+ *
|
|
|
+ * @param month
|
|
|
+ * @param day
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String createServingTime(int month, int day) {
|
|
|
+ String servingTime = "";
|
|
|
+ month += day / 30;
|
|
|
+ day = day % 30;
|
|
|
+ if (month > 0) {
|
|
|
+ servingTime += month + "个月";
|
|
|
+ }
|
|
|
+ if (month >= 0 && day > 0) {
|
|
|
+ servingTime += day + "天";
|
|
|
+ }
|
|
|
+ return servingTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getNowDate() {
|
|
|
+ Date date = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_FMT_YYYY_MM_DD);
|
|
|
+ String nowTime = sdf.format(date);
|
|
|
+ try {
|
|
|
+ date = sdf.parse(nowTime);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ date = new Date();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到两个日期相差的天数
|
|
|
+ *
|
|
|
+ * @param early
|
|
|
+ * @param late
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int daysBetween(Date early, Date late) {
|
|
|
+
|
|
|
+ Calendar calst = Calendar.getInstance();
|
|
|
+ Calendar caled = Calendar.getInstance();
|
|
|
+ calst.setTime(early);
|
|
|
+ caled.setTime(late);
|
|
|
+ // 设置时间为0时
|
|
|
+ calst.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calst.set(Calendar.MINUTE, 0);
|
|
|
+ calst.set(Calendar.SECOND, 0);
|
|
|
+ caled.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ caled.set(Calendar.MINUTE, 0);
|
|
|
+ caled.set(Calendar.SECOND, 0);
|
|
|
+ // 得到两个日期相差的天数
|
|
|
+ int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
|
|
|
+
|
|
|
+ return days;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getMonthFirstDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.add(Calendar.MONTH, 0);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月最后一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getMonthLastDay(Date date) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, 0);
|
|
|
+ calendar.add(Calendar.MONTH, 1);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取想要的时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getDate(String time, String str) {
|
|
|
+ Date date = new Date();
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
+ str = DateUtils.DATE_FMT_YYYY_MM_DD;
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(time)) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(str);
|
|
|
+ try {
|
|
|
+ date = sdf.parse(time);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ date = new Date();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单元格
|
|
|
+ *
|
|
|
+ * @param columnIndex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String excelColIndexToStr(int columnIndex) {
|
|
|
+ if (columnIndex <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String columnStr = "";
|
|
|
+ columnIndex--;
|
|
|
+ do {
|
|
|
+ if (columnStr.length() > 0) {
|
|
|
+ columnIndex--;
|
|
|
+ }
|
|
|
+ columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
|
|
|
+ columnIndex = ((columnIndex - columnIndex % 26) / 26);
|
|
|
+ } while (columnIndex > 0);
|
|
|
+ return columnStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getMonthFirstDay(Date date, String str) {
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
+ str = DateUtils.DATE_FMT_YYYY_MM_DD;
|
|
|
+ }
|
|
|
+ Date firstDay = getMonthFirstDay(date);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(str);
|
|
|
+ return sdf.format(firstDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取本月第一天
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getMonthLastDay(Date date, String str) {
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
+ str = DateUtils.DATE_FMT_YYYY_MM_DD;
|
|
|
+ }
|
|
|
+ Date lastDay = getMonthLastDay(date);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(str);
|
|
|
+ return sdf.format(lastDay);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断 a>b
|
|
|
+ *
|
|
|
+ * @param oneDay
|
|
|
+ * @param twoDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean compareDate(Date oneDay, Date twoDate) {
|
|
|
+ boolean flag = false;
|
|
|
+ if (null != oneDay && null != twoDate && oneDay.after(twoDate)) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取自然月天数
|
|
|
+ *
|
|
|
+ * @param yearMonth
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getMonthDays(String yearMonth) {
|
|
|
+ int days = 30;
|
|
|
+ if (StringUtils.isNotBlank(yearMonth)) {
|
|
|
+ String[] yearAndMonth = yearMonth.split("-");
|
|
|
+ if (yearAndMonth.length == 2) {
|
|
|
+ int year = Integer.parseInt(yearAndMonth[0]);
|
|
|
+ int month = Integer.parseInt(yearAndMonth[1]);
|
|
|
+ boolean flag = false;
|
|
|
+ if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ switch (yearAndMonth[1]) {
|
|
|
+ case "1":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ case "2":
|
|
|
+ if (flag) {
|
|
|
+ days = 29;
|
|
|
+ } else {
|
|
|
+ days = 28;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "3":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ case "5":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ case "7":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ case "8":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ case "10":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ case "12":
|
|
|
+ days = 31;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return days;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|