From 6b8ee7094aee760f791a5bad1235ef4457d95809 Mon Sep 17 00:00:00 2001 From: jiangjiazhi Date: Fri, 31 Mar 2017 14:12:45 +0800 Subject: [PATCH] 1 --- .../src/main/java/com/lyms/util/DateUtil.java | 788 +++++++++++++++++++++ 1 file changed, 788 insertions(+) create mode 100644 parent/core.sdk/src/main/java/com/lyms/util/DateUtil.java diff --git a/parent/core.sdk/src/main/java/com/lyms/util/DateUtil.java b/parent/core.sdk/src/main/java/com/lyms/util/DateUtil.java new file mode 100644 index 0000000..0a437b6 --- /dev/null +++ b/parent/core.sdk/src/main/java/com/lyms/util/DateUtil.java @@ -0,0 +1,788 @@ +package com.lyms.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +import org.joda.time.DateTime; + +public class DateUtil { + private static Lock lock = new ReentrantLock(); + public static SimpleDateFormat dd = new SimpleDateFormat("dd"); + public static SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd"); + public static SimpleDateFormat y_m_d = new SimpleDateFormat("yyyy-MM-dd"); + public static SimpleDateFormat md = new SimpleDateFormat("MM-dd"); + public static SimpleDateFormat m_d = new SimpleDateFormat("MM/dd"); + public static SimpleDateFormat y_m_d_h_m_s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + public static SimpleDateFormat y_m_d_h_m1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + public static SimpleDateFormat y_m_d_h_m = new SimpleDateFormat("yyyyMMddHHmm"); + public static SimpleDateFormat yyyyMMddHHmmssSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS"); + public static final int DAY_SECONDS = 86399; + public static final int DAY_FULL_SECONDS = 86400; + + public static String getymd() { + return y_m_d.format(new Date()); + } + + public static String getymd6() { + return ymd.format(new Date()); + } + + public static String getymdhmss() { + return yyyyMMddHHmmssSSS.format(new Date()); + } + + public static Integer getDays(Date start, Date end) { + Integer days = 0; + if (start != null && end != null) { + start = getDayStartTime(start); + end = getDayStartTime(end); + Integer seconds = (int) ((end.getTime() - start.getTime()) / 1000); + days = seconds / DAY_FULL_SECONDS; + } + return days; + } + + public static Integer minute2Index(Date date) { + + DateTime dt = new DateTime(date.getTime()); + return dt.getMinuteOfDay() / 10; + } + + public static Integer getAge(Date birth) { + if (null == birth) { + return null; + } + java.util.Calendar calendar = Calendar.getInstance(); + Date date = new Date(); + calendar.setTime(date); + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + c1.setTime(date); + c2.setTime(birth); + int year1 = c1.get(Calendar.YEAR); + int year2 = c2.get(Calendar.YEAR); + + int mouth1 = c1.get(Calendar.MONTH);//0当前 + int mouth2 = c2.get(Calendar.MONTH);//4 + int i = 0; + /* if(mouth2-mouth1>=0){ + int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10 + int days2 = c2.get(Calendar.DAY_OF_MONTH);//1 + if(days1days2){ + } + }else if(mouth1-mouth2<0){ + i=i-1; + }*/ + + if (mouth2 == mouth1) { + int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10 + int days2 = c2.get(Calendar.DAY_OF_MONTH);//1 + if (days1 < days2) { + i = i - 1; + } else if (days1 > days2) { + // i=i-1; + } + } else if (mouth2 > mouth1) { + i = i - 1; + } + + // if (birth == null) { + // return null; + // } + // Date now = new Date(); + // long m = now.getTime() - birth.getTime(); + // Long age = m / 31536000000L; + // return age.intValue(); + return (Math.abs(year1 - year2) + i); + } + + public static Integer getAge(Date birth, Date fmDate) { + if (birth == null || null == fmDate) { + return null; + } + long m = fmDate.getTime() - birth.getTime(); + Long age = m / 31536000000L; + return age.intValue(); + } + + public static Integer getWeek(Date date) { + if (date == null) { + return null; + } + Date now = new Date(); + date = getDayStartTime(date); + long m = now.getTime() - date.getTime() + 86400000L; + Long week = m / 604800000L; + return week.intValue(); + } + + public static Integer getWeek(Date start, Date end) { + if (start == null || end == null) { + return null; + } + start = getDayStartTime(start); + end = getDayStartTime(end); + long m = end.getTime() - start.getTime() + 86400000L; + Long week = m / 604800000L; + return week.intValue(); + } + + public static Long getStartTime() { + 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); + return todayStart.getTime().getTime(); + } + + public static Long getEndTime() { + 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); + return todayEnd.getTime().getTime(); + } + + /** + * 获取到秒 + * + * @param date + * @return + */ + public static Integer getSecond(Date date) { + if (null == date) { + return 0; + } + long time = date.getTime(); + return (int) (time / 1000); + } + + public static Integer getInt() { + long time = new Date().getTime(); + return (int) (time / 1000); + } + + public static String getYmdhm(int day, int minute) { + DateTime dt = new DateTime(); + dt = dt.minusMillis(dt.getMillisOfDay()); + dt = dt.plusDays(day); + dt = dt.plusMinutes(minute); + return dt.toString("yyyyMMddHHmm"); + } + + public static Date getYmdhmDate(String s) { + if (s == null) { + return null; + } + try { + return y_m_d_h_m.parse(s); + } catch (Exception e) { + return null; + } + } + + public static String getymdhm(Date d) { + if (d == null) { + return null; + } + try { + return y_m_d_h_m.format(d); + } catch (Exception e) { + return null; + } + } + + /** + * @return 返回当前年月日字符串 :20131025 + */ + public static String getYmd() { + return ymd.format(new Date()); + } + + public static String getYmd(Date d) { + if (d == null) { + return null; + } + try { + return ymd.format(d); + } catch (Exception e) { + return null; + } + } + + public static Date parseYMD(String s) { + if (s == null) { + return null; + } + + try { + lock.lock(); + return y_m_d.parse(s); + } catch (Exception e) { + return null; + } finally { + lock.unlock(); + } + } + + public static Date parseYMDEnd(String s) { + if (s == null) { + return null; + } + + try { + lock.lock(); + Date d = y_m_d.parse(s); + long resultMis = d.getTime() + (1000 * 60 * 60 * 24 - 1); + return new Date(resultMis); + } catch (Exception e) { + return null; + } finally { + lock.unlock(); + } + } + + public static Date parseMD(String s) { + if (s == null) { + return null; + } + try { + return md.parse(s); + } catch (Exception e) { + return null; + } + } + + public static Date parseD(String s) { + if (s == null) { + return null; + } + try { + return dd.parse(s); + } catch (Exception e) { + return null; + } + } + + public static String getyyyy_MM_dd(Date d) { + if (d == null) { + return null; + } + try { + return y_m_d.format(d); + } catch (Exception e) { + return null; + } + } + + public static String getMMdd(Date d) { + if (d == null) { + return null; + } + try { + return md.format(d); + } catch (Exception e) { + return null; + } + } + + public static String getMM_dd(Date d) { + if (d == null) { + return null; + } + try { + return m_d.format(d); + } catch (Exception e) { + return null; + } + } + + public static String getDD(Date d) { + if (d == null) { + return null; + } + try { + return dd.format(d); + } catch (Exception e) { + return null; + } + } + + public static Date parseYMDHMS(String s) { + if (s == null) { + return null; + } + try { + return y_m_d_h_m_s.parse(s); + } catch (Exception e) { + return null; + } + } + + public static Date parseYMDHM(String s) { + if (s == null) { + return null; + } + try { + return y_m_d_h_m1.parse(s); + } catch (Exception e) { + return null; + } + } + + public static String getyyyy_MM_dd_hms(Date d) { + if (d == null) { + return null; + } + try { + return y_m_d_h_m_s.format(d); + } catch (Exception e) { + return null; + } + } + + public static Date getDayStartTime(Date startTime) { + if (startTime == null) { + return null; + } + DateTime dt = new DateTime(startTime.getTime()); + dt = dt.minusMillis(dt.getMillisOfDay()); + return dt.toDate(); + } + + public static Date getDayEndTime(Date endTime) { + if (endTime == null) { + return null; + } + DateTime dt = new DateTime(endTime.getTime()); + dt = dt.minusMillis(dt.getMillisOfDay()).plusSeconds(DAY_SECONDS); + return dt.toDate(); + } + + public static Date getMonthStart(Date time) { + if (time == null) { + return null; + } + DateTime dt = new DateTime(time.getTime()); + dt = dt.minusMillis(dt.getMillisOfDay()).minusDays(dt.getDayOfMonth() - 1); + return dt.toDate(); + } + + public static Date getMonthEndTime(Date endTime) { + if (endTime == null) { + return null; + } + DateTime dt = new DateTime(getMonthStart(new DateTime(endTime.getTime()).plusMonths(1).toDate())).minusDays(1) + .plusSeconds(DAY_SECONDS); + return dt.toDate(); + } + + public static Date getQuarterStart(Date date) { + if (date == null) { + return null; + } + DateTime dt = new DateTime(date.getTime()).minusMonths(1); + dt = dt.withMonthOfYear(dt.getMonthOfYear() / 3 * 3 + 1); + return getMonthStart(dt.toDate()); + } + + public static Date getQuarterEnd(Date date) { + if (date == null) { + return null; + } + DateTime dt = new DateTime(date.getTime()).minusMonths(1); + dt = dt.withMonthOfYear(dt.getMonthOfYear() / 3 * 3 + 3); + return getMonthEndTime(dt.toDate()); + } + + public static Date getWeekStart(Date date) { + if (date == null) { + return null; + } + DateTime dt = new DateTime(date.getTime()); + dt = dt.withDayOfWeek(1); + return getDayStartTime(dt.toDate()); + } + + public static Date getWeekEnd(Date date) { + if (date == null) { + return null; + } + DateTime dt = new DateTime(date.getTime()); + dt = dt.withDayOfWeek(1).plusDays(6); + return getDayEndTime(dt.toDate()); + } + + public static Integer getWeekNum(Date date) { + if (date == null) { + return null; + } + DateTime dt = new DateTime(date.getTime()); + return (dt.getWeekyear() % 100) * 100 + dt.getWeekOfWeekyear(); + } + + public static Date addDay(Date srcDate, int days) { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(srcDate); + rightNow.add(Calendar.DAY_OF_YEAR, days); + return rightNow.getTime(); + } + + public static Date addMonth(Date srcDate, int month) { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(srcDate); + rightNow.add(Calendar.MONTH, month); + return rightNow.getTime(); + } + + public static Date addYear(Date srcDate, int year) { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(srcDate); + rightNow.add(Calendar.YEAR, year); + return rightNow.getTime(); + } + + public static Date addWeek(Date srcDate, int weak) { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(srcDate); + rightNow.add(Calendar.WEEK_OF_YEAR, weak); + return rightNow.getTime(); + } + + /** + * 获取当天还剩多少秒 + * + * @return + */ + public static int getDaySeconds() { + Calendar curDate = Calendar.getInstance(); + + Calendar tommorowDate = new GregorianCalendar(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH), + curDate.get(Calendar.DATE) + 1, 0, 0, 0); + return (int) (tommorowDate.getTimeInMillis() - curDate.getTimeInMillis()) / 1000; + } + + /** + * 计算儿童的月龄 + * @param birth + * @param checkDate + * @return + */ + public static String getBabyMonthAge(Date birth, Date checkDate) { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(checkDate); + rightNow.add(Calendar.MONTH, -1); + if (birth.getTime() > rightNow.getTime().getTime()) { + int day = daysBetween(birth, checkDate); + return day + "天"; + } + Calendar rightNow1 = Calendar.getInstance(); + rightNow1.setTime(checkDate); + rightNow1.add(Calendar.YEAR, -1); + + if (birth.getTime() > rightNow1.getTime().getTime()) { + + int month = getMonthSpace(birth, checkDate); + Calendar yearNow = Calendar.getInstance(); + yearNow.setTime(birth); + yearNow.add(Calendar.MONTH, month); + + int day = daysBetween(yearNow.getTime(), checkDate); + String dstr = ""; + if (day != 0) { + dstr = day + "天"; + } + String ms = ""; + if (month != 0) { + ms = month + "月龄"; + } + return ms + dstr; + } + int year = getYearSpace(birth, checkDate); + rightNow.setTime(checkDate); + rightNow.add(Calendar.YEAR, year); + + int month = getMonthSpace1(birth, rightNow.getTime()); + + return year + "岁" + (month == 0 ? "" : month + "月龄"); + } + + public static int daysBetween(Date smdate, Date bdate) { + if (smdate == null || bdate == null) { + return -1; + } + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + try { + smdate = sdf.parse(sdf.format(smdate)); + bdate = sdf.parse(sdf.format(bdate)); + 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); + return Integer.parseInt(String.valueOf(between_days)); + } catch (ParseException e) { + e.printStackTrace(); + } + + return 0; + } + + public static int getMonthSpace(Date date1, Date date2) { + + int result = 0; + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + + c1.setTime(date1); + c2.setTime(date2); + + result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH); + if (result <= 0) { + result = 12 - Math.abs(result); + } + + // result = result == 0 ? 1 : Math.abs(result); + if (Integer.parseInt(getDD(date1)) > Integer.parseInt(getDD(date2))) { + result = result - 1; + } + + return result; + + } + + public static int countMonths(Date date1, Date date2) throws ParseException { + + /* Calendar c1=Calendar.getInstance(); + Calendar c2=Calendar.getInstance(); + + c1.setTime(date1); + c2.setTime(date2); + + int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR); + System.out.print(year); + //开始日期若小月结束日期 + if(year<0){ + year=-year; + return year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH); + }*/ + + // return year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH); + return 0; + + } + + public static int getBabyMonthAge1(Date birth, Date checkDate) { + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(checkDate); + rightNow.add(Calendar.MONTH, -1); + if (birth.getTime() > rightNow.getTime().getTime()) { + return 0; + } + Calendar rightNow1 = Calendar.getInstance(); + rightNow1.setTime(checkDate); + rightNow1.add(Calendar.YEAR, -1); + + if (birth.getTime() > rightNow1.getTime().getTime()) { + + int month = getMonthSpace(birth, checkDate); + Calendar yearNow = Calendar.getInstance(); + yearNow.setTime(birth); + yearNow.add(Calendar.MONTH, month); + return month; + } + int year = getYearSpace(birth, checkDate); + rightNow.setTime(checkDate); + rightNow.add(Calendar.YEAR, year); + + int month = getMonthSpace1(birth, rightNow.getTime()); + + return year * 12 + month; + } + + public static int getMonthSpace1(Date date1, Date date2) { + + int result = 0; + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + + c1.setTime(date1); + c2.setTime(date2); + + result = c2.get(Calendar.MONDAY) - c1.get(Calendar.MONTH); + if (result < 0) { + result = 12 - Math.abs(result); + } + + if (Integer.parseInt(getDD(date1)) > Integer.parseInt(getDD(date2))) { + result = result - 1; + } + + return result; + + } + + public static int getYearSpace(Date date1, Date date2) { + int result = 0; + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + + c1.setTime(date1); + c2.setTime(date2); + + result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); + if (Integer.parseInt(getMMdd(date1).split("-")[0]) > Integer.parseInt(getMMdd(date2).split("-")[0])) { + result = result - 1; + } + return result == 0 ? 1 : Math.abs(result); + + } + + public static int getBabyAgeMonth(Date d1, Date d2) { + int months = 0;//相差月份 + int y1 = d1.getYear(); + int y2 = d2.getYear(); + int dd1 = d1.getDate(); //起始日期天 + int dd2 = d2.getDate(); //结束日期天 + if (d1.getTime() < d2.getTime()) { + months = d2.getMonth() - d1.getMonth() + (y2 - y1) * 12; + if (dd2 < dd1) { + months = months - 1; + } + } + return months; + } + + /** + * 获取当前日期是星期几
+ * + * @param dt + * @return 当前日期是星期几 + */ + public static String getWeekOfDate(Date dt) { + String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; + Calendar cal = Calendar.getInstance(); + cal.setTime(dt); + System.out.println(cal.get(Calendar.DAY_OF_WEEK)); + int w = cal.get(Calendar.DAY_OF_WEEK) - 1; + if (w < 0) + w = 0; + return weekDays[w]; + } + + public static Date getRangeDate(Date initDate, int num, String type, int delayDay) { + + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(initDate); + if (type == null) + return null; + if (type.equals("年".trim())) { + rightNow.add(Calendar.YEAR, num); + + } else if (type.equals("月".trim())) { + + rightNow.add(Calendar.MONTH, num); + } else if (type.equals("周".trim())) { + rightNow.add(Calendar.WEEK_OF_YEAR, num); + } else if (type.equals("天".trim())) { + rightNow.add(Calendar.DAY_OF_YEAR, num); + } + rightNow.add(Calendar.DAY_OF_YEAR, delayDay); + String date = y_m_d.format(rightNow.getTime()); + System.out.println(date); + return rightNow.getTime(); + } + + public static Date getNewDate(int num, String type, int day) { + Date dt = new Date(); + + Calendar rightNow = Calendar.getInstance(); + rightNow.setTime(dt); + if (type == null) + return null; + if (type.trim().equals("年")) { + rightNow.add(Calendar.YEAR, num); + } else if (type.trim().equals("月")) { + rightNow.add(Calendar.MONTH, num); + } else if (type.trim().equals("周")) { + rightNow.add(Calendar.WEEK_OF_YEAR, num); + } else if (type.trim().equals("天")) { + rightNow.add(Calendar.DAY_OF_YEAR, num); + } + rightNow.add(Calendar.DAY_OF_YEAR, day); + String date = y_m_d.format(rightNow.getTime()); + System.out.println(date); + return rightNow.getTime(); + } + + public static Date formatDate(Date date) { + String time = y_m_d.format(date); + return parseYMD(time); + } + + public static String formatSimpleDate(Date date) { + if (date == null) { + return ""; + } + SimpleDateFormat md = new SimpleDateFormat("M-d"); + return md.format(date); + } + + public static void main(String[] arg) { + System.out.println(getAge(parseYMD("1990-01-9"))); + } + + /** + * @auther HuJiaqi + * @createTime 2016年11月29日 11时30分 + * @discription 这个方法是为了孕产妇围产管理-统计管理-预产期统计管理定制,请慎重使用 + */ + public static String addMonthWithout0(String date, int add) { + int year = Integer.valueOf(date.substring(0, 4)); + int month = Integer.valueOf(date.substring(5)) + add; + if (month < 13) { + date = year + "-" + month; + } else { + date = (year + 1) + "-" + (month - 12); + } + return date; + } + + /** + * @auther HuJiaqi + * @createTime 2016年11月29日 11时30分 + * @discription 这个方法是为了孕产妇围产管理-统计管理-预产期统计管理定制,请慎重使用 + */ + public static String addMonthWith0(String date, int add) { + date = addMonthWithout0(date, add); + if (date.substring(5).length() == 1) { + date = date.substring(0, 4) + "-" + 0 + date.substring(5); + } + return date; + } + + /** + * @auther HuJiaqi + * @createTime 2016年12月13日 17时10分 + * @discription 格式化传入时间的方法,注意这里是获取的前面时间的00和后面时间的59,这个方法很苛刻,必须前后都传了值,而且是2016-12-15+-+2016-12-25格式 + */ + public static Date[] getSNDate(String snDateString) { + return new Date[] { parseYMD(snDateString.substring(0, 10)), parseYMDEnd(snDateString.substring(13, 23)) }; + } + +} -- 1.8.3.1