| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.DateFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.*; |
| | | |
| | | import cn.hutool.core.date.DateUnit; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | | public class DateUtils { |
| | | |
| | | |
| | | public static DateTimeFormatter format_ymdhms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | |
| | | private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | |
| | | public static DateTimeFormatter format_ymd = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | |
| | | public static DateTimeFormatter format_ymdhms_string = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | |
| | | public static DateTimeFormatter format_ymdhms_yyyyMMddmmHHssSSS = DateTimeFormatter.ofPattern("yyyyMMddmmHHssSSS"); |
| | | public static DateTimeFormatter format_ymdhms_no_signal = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | |
| | | public static SimpleDateFormat yyyyMMdd_format = new SimpleDateFormat("yyyy-MM-dd"); |
| | | |
| | | public static SimpleDateFormat yyyy_MM_dd_format = new SimpleDateFormat("yyyy/MM/ddHH:mm:ss"); |
| | | |
| | | public static SimpleDateFormat ymdhms_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | public static SimpleDateFormat ymdhm_format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); |
| | | |
| | | public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); |
| | | |
| | | |
| | | public static SimpleDateFormat format_yyymmdd = new SimpleDateFormat("yyyyMMdd"); |
| | | public static SimpleDateFormat format_yyyy = new SimpleDateFormat("yyyy"); |
| | | public static SimpleDateFormat format_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| | | private static DateTimeFormatter format_yms = DateTimeFormatter.ofPattern("yyyyMM"); |
| | | private static DateTimeFormatter format_y = DateTimeFormatter.ofPattern("yyyy"); |
| | | |
| | | /** |
| | | * 将日期对象格式化成指定的字符串格式 |
| | | * |
| | | * @param date 日期对象 |
| | | * @param formatString 格式化字符串 |
| | | * @param date |
| | | * 日期对象 |
| | | * @param formatString |
| | | * 格式化字符串 |
| | | * @return String |
| | | */ |
| | | public static String getDateFormatString(Date date, String formatString) { |
| | |
| | | |
| | | /** |
| | | * 字符串转date |
| | | * @param date 时间戳 |
| | | * @param format format |
| | | * |
| | | * @param date |
| | | * 时间戳 |
| | | * @param format |
| | | * format |
| | | * @return 时间 |
| | | */ |
| | | public static Date stringToDate(String date,SimpleDateFormat format){ |
| | | public static Date stringToDate(String date, SimpleDateFormat format) { |
| | | try { |
| | | if(StringUtils.isBlank(date)){ |
| | | if (StringUtils.isBlank(date)) { |
| | | return null; |
| | | } |
| | | return format.parse(date); |
| | | } catch (ParseException e) { |
| | | log.error(e.getMessage(),e); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 字符串转date |
| | | * @param date 时间戳 |
| | | * |
| | | * @param date |
| | | * 时间戳 |
| | | * @return 时间 |
| | | */ |
| | | public static Date stringToDateStandard (String date){ |
| | | public static Date stringToDateStandard(String date) { |
| | | SimpleDateFormat format = ymdhms_format; |
| | | if(StringUtils.isNotBlank(date)){ |
| | | if(date.split("-").length==3){ |
| | | if(date.split(":").length!=3){ |
| | | if (StringUtils.isNotBlank(date)) { |
| | | if (date.split("-").length == 3) { |
| | | if (date.split(":").length != 3) { |
| | | format = yyyyMMdd_format; |
| | | } |
| | | }else if(date.split("-").length==2){ |
| | | } else if (date.split("-").length == 2) { |
| | | format = new SimpleDateFormat("yyyy-MM"); |
| | | } |
| | | try { |
| | | return format.parse(date); |
| | | } catch (ParseException e) { |
| | | log.error(e.getMessage(),e); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | return null; |
| | |
| | | |
| | | /** |
| | | * 字符串转date |
| | | * @param date 时间戳 |
| | | * |
| | | * @param date |
| | | * 时间戳 |
| | | * @return 时间 |
| | | */ |
| | | public static Date stringToDateStandardByYYYY_MM_dd (String date){ |
| | | public static Date stringToDateStandardByYYYY_MM_dd(String date) { |
| | | SimpleDateFormat format = yyyy_MM_dd_format; |
| | | if(StringUtils.isNotBlank(date)){ |
| | | if (StringUtils.isNotBlank(date)) { |
| | | try { |
| | | return format.parse(date); |
| | | } catch (ParseException e) { |
| | | log.error(e.getMessage(),e); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间 |
| | | * |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static LocalDateTime getCurrentDate(){ |
| | | public static LocalDateTime getCurrentDate() { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | return now; |
| | | } |
| | | |
| | | /** |
| | | * 返回当前事件的时间戳 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Long getCurrentDateTimeStamp(){ |
| | | return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); |
| | | public static Long getCurrentDateTimeStamp() { |
| | | return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyy-MM-dd HH:mm:ss |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateStr(){ |
| | | String nowtime=getCurrentDate().format(format_ymdhms); |
| | | public static String getCurrentDateStr() { |
| | | String nowtime = getCurrentDate().format(format_ymdhms); |
| | | return nowtime; |
| | | } |
| | | |
| | | public static String getCurrentDateymd(){ |
| | | String nowtime=getCurrentDate().format(format_ymdhms_string); |
| | | public static String getCurrentDateymd() { |
| | | String nowtime = getCurrentDate().format(format_ymdhms_string); |
| | | return nowtime; |
| | | } |
| | | |
| | | public static String getCurrentDateyymd(){ |
| | | String nowtime=getCurrentDate().format(format_ymd_String); |
| | | public static String getCurrentDateyymd() { |
| | | String nowtime = getCurrentDate().format(format_ymd_String); |
| | | return nowtime; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyyMMdd |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateString(){ |
| | | String nowtime=getCurrentDate().format(format_ymds); |
| | | public static String getCurrentDateString() { |
| | | String nowtime = getCurrentDate().format(format_ymds); |
| | | return nowtime; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间字符串,格式为yyyy-MM-dd |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateStrymd(){ |
| | | String nowtime=getCurrentDate().format(format_ymd); |
| | | public static String getCurrentDateStrymd() { |
| | | String nowtime = getCurrentDate().format(format_ymd); |
| | | return nowtime; |
| | | } |
| | | |
| | | public static String getCurrentDateStr_MS(){ |
| | | String nowtime=getCurrentDate().format(format_ymdhmssss); |
| | | public static String getCurrentDateStr_MS() { |
| | | String nowtime = getCurrentDate().format(format_ymdhmssss); |
| | | return nowtime; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间long值 |
| | | * |
| | | * @return |
| | | */ |
| | | public static long getCurrentDataLong(){ |
| | | public static long getCurrentDataLong() { |
| | | return new Date().getTime(); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取当前时间 |
| | | * |
| | | * @param format |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static Date getCurrentDate(SimpleDateFormat format){ |
| | | public static Date getCurrentDate(SimpleDateFormat format) { |
| | | String tempDate = format.format(new Date()); |
| | | try { |
| | | return format.parse(tempDate); |
| | | } catch (ParseException e) { |
| | | log.error(e.getMessage(),e); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | |
| | | try { |
| | | return newFormat.parse(tempDate); |
| | | } catch (ParseException e) { |
| | | log.error(e.getMessage(),e); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static String format(Date date, SimpleDateFormat format) { |
| | | if(null == date){ |
| | | if (null == date) { |
| | | return ""; |
| | | } |
| | | return format.format(date); |
| | |
| | | public static String formatSecond_ZH_HMS(Integer second) { |
| | | String result = "0秒"; |
| | | if (null != second) { |
| | | int hours = (int) (second / (60 * 60)); |
| | | int minutes = (int) (second / 60 - hours * 60); |
| | | int seconds = (int) (second - minutes * 60 - hours * 60 * 60); |
| | | int hours = second / (60 * 60); |
| | | int minutes = second / 60 - hours * 60; |
| | | int seconds = second - minutes * 60 - hours * 60 * 60; |
| | | |
| | | String format; |
| | | Object[] array; |
| | | |
| | | if (hours > 0) { |
| | | format = "%1$,d时%2$,d分%3$,d秒"; |
| | | array = new Object[]{hours, minutes, seconds}; |
| | | array = new Object[] {hours, minutes, seconds}; |
| | | } else if (minutes > 0) { |
| | | format = "%1$,d分%2$,d秒"; |
| | | array = new Object[]{minutes, seconds}; |
| | | array = new Object[] {minutes, seconds}; |
| | | } else { |
| | | format = "%1$,d秒"; |
| | | array = new Object[]{seconds}; |
| | | array = new Object[] {seconds}; |
| | | } |
| | | result = String.format(format, array); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 对日期进行加法操作 |
| | | * |
| | | * @param date |
| | | * @param hours |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * 对日期的分钟进行加法操作 |
| | | * |
| | | * @param date |
| | | * @param minutes |
| | | * @return |
| | |
| | | return 0; |
| | | } |
| | | if (null == startDate) { |
| | | //当前时间大于endDate |
| | | // 当前时间大于endDate |
| | | if (dateCompare(now, endDate) == 1) { |
| | | return 1; |
| | | } else { |
| | |
| | | } |
| | | } else { |
| | | if (null == endDate) { |
| | | //当前时间小于startDate |
| | | // 当前时间小于startDate |
| | | if (dateCompare(startDate, now) == 1) { |
| | | return -1; |
| | | } else { |
| | |
| | | |
| | | /** |
| | | * 计算两个日期先差多少秒 |
| | | * @param pre 前一个日期 |
| | | * @param after 后一个日期 |
| | | * |
| | | * @param pre |
| | | * 前一个日期 |
| | | * @param after |
| | | * 后一个日期 |
| | | * @return |
| | | */ |
| | | public static long calTimeDifference(Date pre,Date after){ |
| | | return (after.getTime()-pre.getTime())/1000; |
| | | public static long calTimeDifference(Date pre, Date after) { |
| | | return (after.getTime() - pre.getTime()) / 1000; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 计算两个时间相差的天数 |
| | | * |
| | | * @param stardate |
| | | * @param enddate |
| | | * @return |
| | | */ |
| | | public static int getHour(Date stardate,Date enddate){ |
| | | public static int getHour(Date stardate, Date enddate) { |
| | | long nd = 1000 * 24 * 60 * 60; |
| | | long nh = 1000 * 60 * 60; |
| | | long nm = 1000 * 60; |
| | | long diff=enddate.getTime()-stardate.getTime(); |
| | | long diff = enddate.getTime() - stardate.getTime(); |
| | | |
| | | long day = diff / nd; |
| | | |
| | | long hour = diff % nd / nh; |
| | | long hour = diff / nh; |
| | | |
| | | long min = diff % nd % nh / nm; |
| | | long min = diff / nm; |
| | | |
| | | return (int)day; |
| | | return (int)hour; |
| | | } |
| | | |
| | | /** |
| | | * 结束时间与当前时间计算相差的月数 |
| | | * |
| | | * @param enddate |
| | | * @return |
| | | */ |
| | | public static int getMonthSpace(String enddate, DateTimeFormatter format) { |
| | | LocalDate formatted = LocalDate.parse(enddate,format); |
| | | LocalDate formatted = LocalDate.parse(enddate, format); |
| | | LocalDate today = LocalDate.now(); |
| | | Period per = Period.between(today,formatted); |
| | | if(per.getYears() == 0 ){ |
| | | Period per = Period.between(today, formatted); |
| | | if (per.getYears() == 0) { |
| | | return per.getMonths(); |
| | | }else if(per.getYears() > 0 ){ |
| | | } else if (per.getYears() > 0) { |
| | | return (per.getYears() * 12) + per.getMonths(); |
| | | } |
| | | |
| | |
| | | |
| | | /** |
| | | * 获取某个时间段之前的时间点 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getSomeTimeStapStr(String sign){ |
| | | LocalDateTime beginDateTime =null; |
| | | if (sign.equals("day")) {//一天前时间 |
| | | beginDateTime =LocalDateTime.now().minusDays(1L); |
| | | }else if(sign.equals("week")){ |
| | | beginDateTime =LocalDateTime.now().minusWeeks(1L); |
| | | }else if (sign.equals("month")) { |
| | | beginDateTime =LocalDateTime.now().minusMonths(1L); |
| | | }else if (sign.equals("year")) { |
| | | beginDateTime =LocalDateTime.now().minusYears(1L); |
| | | public static String getSomeTimeStapStr(String sign) { |
| | | LocalDateTime beginDateTime = null; |
| | | if (sign.equals("day")) {// 一天前时间 |
| | | beginDateTime = LocalDateTime.now().minusDays(1L); |
| | | } else if (sign.equals("week")) { |
| | | beginDateTime = LocalDateTime.now().minusWeeks(1L); |
| | | } else if (sign.equals("month")) { |
| | | beginDateTime = LocalDateTime.now().minusMonths(1L); |
| | | } else if (sign.equals("year")) { |
| | | beginDateTime = LocalDateTime.now().minusYears(1L); |
| | | } |
| | | String beginTime=beginDateTime.format(format_ymdhms); |
| | | String beginTime = beginDateTime.format(format_ymdhms); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | try { |
| | | long timestap = sdf.parse(beginTime).getTime(); |
| | | beginTime = timestap+""; |
| | | beginTime = timestap + ""; |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return beginTime; |
| | | } |
| | | |
| | | public static String getSomeTimeDateStr(String sign){ |
| | | String beginTime=""; |
| | | LocalDateTime beginDateTime =null; |
| | | if (sign.equals("day")) {//一天前时间 |
| | | beginDateTime =LocalDateTime.now().minusDays(1L); |
| | | }else if(sign.equals("week")){ |
| | | beginDateTime =LocalDateTime.now().minusWeeks(1L); |
| | | }else if (sign.equals("month")) { |
| | | beginDateTime =LocalDateTime.now().minusMonths(1L); |
| | | }else if (sign.equals("year")) { |
| | | beginDateTime =LocalDateTime.now().minusYears(1L); |
| | | public static String getSomeTimeDateStr(String sign) { |
| | | String beginTime = ""; |
| | | LocalDateTime beginDateTime = null; |
| | | if (sign.equals("day")) {// 一天前时间 |
| | | beginDateTime = LocalDateTime.now().minusDays(1L); |
| | | } else if (sign.equals("week")) { |
| | | beginDateTime = LocalDateTime.now().minusWeeks(1L); |
| | | } else if (sign.equals("month")) { |
| | | beginDateTime = LocalDateTime.now().minusMonths(1L); |
| | | } else if (sign.equals("year")) { |
| | | beginDateTime = LocalDateTime.now().minusYears(1L); |
| | | } |
| | | if (beginDateTime!=null) { |
| | | if (beginDateTime != null) { |
| | | beginTime = beginDateTime.format(format_ymdhms); |
| | | } |
| | | return beginTime; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取失效时间点(在什么时间失效) |
| | | * @param minutes 有效分钟数 |
| | | * |
| | | * @param minutes |
| | | * 有效分钟数 |
| | | * @return |
| | | */ |
| | | public static LocalDateTime getInvalidLocalDateTime(Long minutes){ |
| | | public static LocalDateTime getInvalidLocalDateTime(Long minutes) { |
| | | return LocalDateTime.now().minusMinutes(minutes); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前年份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentYear(){ |
| | | public static String getCurrentYear() { |
| | | Calendar instance = Calendar.getInstance(); |
| | | return instance.get(Calendar.YEAR)+""; |
| | | return instance.get(Calendar.YEAR) + ""; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前月份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentMonth(){ |
| | | public static String getCurrentMonth() { |
| | | Calendar instance = Calendar.getInstance(); |
| | | int month = instance.get(Calendar.MONTH); |
| | | String result; |
| | | if (month < 10){ |
| | | if (month < 10) { |
| | | result = "0" + month; |
| | | }else { |
| | | result = month+""; |
| | | } else { |
| | | result = month + ""; |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** 获取无符号的当前时间 |
| | | /** |
| | | * 获取无符号的当前时间 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateStrNoSignal(){ |
| | | public static String getCurrentDateStrNoSignal() { |
| | | return getCurrentDate().format(format_ymdhms_no_signal); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取前几个小时的时间 |
| | | * |
| | | * @param hours |
| | | * @return |
| | | */ |
| | | public static String getNextHourDateStrNoSignal(long hours){ |
| | | public static String getNextHourDateStrNoSignal(long hours) { |
| | | return LocalDateTime.now().plusHours(hours).format(format_ymdhms_no_signal); |
| | | } |
| | | |
| | | |
| | | public static Date localDateTimeToDate(LocalDateTime localDateTime) { |
| | | ZoneId zone = ZoneId.systemDefault(); |
| | |
| | | |
| | | /** |
| | | * 小时取整 |
| | | * |
| | | * @param date |
| | | * @param hour |
| | | * @return |
| | | */ |
| | | public static Date integralHour(Date date,Integer hour) { |
| | | if(date==null||hour==null){ |
| | | public static Date integralHour(Date date, Integer hour) { |
| | | if (date == null || hour == null) { |
| | | return null; |
| | | } |
| | | long l = date.getTime()- 1; |
| | | long l = date.getTime() - 1; |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); |
| | | try { |
| | | Date parse = sdf.parse(sdf.format(new Date(l))); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 分钟取整 |
| | | * 以十分钟为单位 ,去除尾端,加上 参数 46->40+minutes*10 |
| | | * 分钟取整 以十分钟为单位 ,去除尾端,加上 参数 46->40+minutes*10 |
| | | * |
| | | * @param date |
| | | * @param minutes |
| | | * @return |
| | | */ |
| | | public static Date integral10Min(Date date,Integer minutes) { |
| | | if(date==null||minutes==null){ |
| | | public static Date integral10Min(Date date, Integer minutes) { |
| | | if (date == null || minutes == null) { |
| | | return null; |
| | | } |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(date); |
| | | int i = cal.get(Calendar.MINUTE);//获取分钟 |
| | | if(i%10==0){ |
| | | int i = cal.get(Calendar.MINUTE);// 获取分钟 |
| | | if (i % 10 == 0) { |
| | | minutes--; |
| | | } |
| | | minutes=minutes*10-i%10; |
| | | long l = date.getTime()- 1; |
| | | minutes = minutes * 10 - i % 10; |
| | | long l = date.getTime() - 1; |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00"); |
| | | try { |
| | | Date parse = sdf.parse(sdf.format(new Date(l))); |
| | |
| | | |
| | | /** |
| | | * 天数取整 |
| | | * |
| | | * @param date |
| | | * @param day |
| | | * @return |
| | | */ |
| | | public static Date integralDay(Date date,Integer day) { |
| | | if(date==null||day==null){ |
| | | public static Date integralDay(Date date, Integer day) { |
| | | if (date == null || day == null) { |
| | | return null; |
| | | } |
| | | long l = date.getTime()- 1; |
| | | long l = date.getTime() - 1; |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); |
| | | try { |
| | | Date parse = sdf.parse(sdf.format(new Date(l))); |
| | |
| | | |
| | | /** |
| | | * 日期相加减 |
| | | * |
| | | * @param time |
| | | * 时间字符串 yyyy-MM-dd HH:mm:ss |
| | | * 时间字符串 yyyy-MM-dd HH:mm:ss |
| | | * @param num |
| | | * 加的数,-num就是减去 |
| | | * @return |
| | | * 减去相应的数量的年的日期 |
| | | * 加的数,-num就是减去 |
| | | * @return 减去相应的数量的年的日期 |
| | | * @throws ParseException |
| | | */ |
| | | public static Date yearAddNum(Date time, Integer num) { |
| | | //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | //Date date = format.parse(time); |
| | | // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | // Date date = format.parse(time); |
| | | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | |
| | | } |
| | | |
| | | public static boolean isValidDate(String str) { |
| | | boolean convertSuccess=true; |
| | | boolean convertSuccess = true; |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); |
| | | try { |
| | | format.setLenient(false); |
| | | format.parse(str); |
| | | } catch (ParseException e) { |
| | | convertSuccess=false; |
| | | convertSuccess = false; |
| | | } |
| | | return convertSuccess; |
| | | } |
| | |
| | | return date; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将日期对象格式化成yyyy-MM-dd格式的字符串 |
| | | * |
| | | * @param date 日期对象 |
| | | * @param date |
| | | * 日期对象 |
| | | * @return String |
| | | */ |
| | | public static String getDateStringYMD(Date date) { |
| | |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getFirstDayOfMonth() { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.add(Calendar.MONTH,0); |
| | | cal.add(Calendar.MONTH, 0); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), actualMinimum, 00, 00, 00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月的第一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 时间 |
| | | * |
| | | * @param year |
| | | * 年份 |
| | | * @param month |
| | | * 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthStart(Integer year,Integer month) { |
| | | public static Date getYearMonthStart(Integer year, Integer month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, year); |
| | |
| | | // 设置日历中月份的最小天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, firstDay); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), actualMinimum, 00, 00, 00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月的最后一天时间 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | | * @return 时间 |
| | | * |
| | | * @param year |
| | | * 年份 |
| | | * @param month |
| | | * 月份 |
| | | * @return 时间 |
| | | */ |
| | | public static Date getYearMonthEnd(Integer year,Integer month) { |
| | | public static Date getYearMonthEnd(Integer year, Integer month) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, year); |
| | |
| | | // 设置日历中月份的最大天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay); |
| | | int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); |
| | | cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), actualMinimum, 00, 00, 00); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | public static Date getDateM(Date start, int min) { |
| | | public static Date getDateM(Date start, int min) { |
| | | Calendar date1 = Calendar.getInstance(); |
| | | date1.setTime(start); |
| | | date1.add(Calendar.MONTH, min); |
| | |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static int getMonth(Date date){ |
| | | public static int getMonth(Date date) { |
| | | Calendar instance = Calendar.getInstance(); |
| | | instance.setTime(date); |
| | | return instance.get(Calendar.MONTH); |
| | | } |
| | | |
| | | public static boolean before(Date start, Date end){ |
| | | public static int getYear(Date date) { |
| | | Calendar instance = Calendar.getInstance(); |
| | | instance.setTime(date); |
| | | return instance.get(Calendar.YEAR); |
| | | } |
| | | |
| | | public static boolean before(Date start, Date end) { |
| | | Calendar date1 = Calendar.getInstance(); |
| | | date1.setTime(start); |
| | | Calendar date2 = Calendar.getInstance(); |
| | |
| | | return date1.before(date2); |
| | | } |
| | | |
| | | /** |
| | | * date2比date1多的天数 |
| | | * |
| | | * @param date1 |
| | | * @param date2 |
| | | * @return |
| | | */ |
| | | public static int differentDays(Date date1, Date date2) { |
| | | Calendar cal1 = Calendar.getInstance(); |
| | | cal1.setTime(date1); |
| | | |
| | | Calendar cal2 = Calendar.getInstance(); |
| | | cal2.setTime(date2); |
| | | int day1 = cal1.get(Calendar.DAY_OF_YEAR); |
| | | int day2 = cal2.get(Calendar.DAY_OF_YEAR); |
| | | |
| | | public static void main(String[]args)throws Exception{ |
| | | // Date date= new Date(); |
| | | // Date after = new Date(); |
| | | // System.out.println(calTimeDifference(date,after)); |
| | | int year1 = cal1.get(Calendar.YEAR); |
| | | int year2 = cal2.get(Calendar.YEAR); |
| | | if (year1 != year2) // 同一年 |
| | | { |
| | | int timeDistance = 0; |
| | | for (int i = year1; i < year2; i++) { |
| | | if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年 |
| | | { |
| | | timeDistance += 366; |
| | | } else // 不是闰年 |
| | | { |
| | | timeDistance += 365; |
| | | } |
| | | } |
| | | |
| | | return timeDistance + (day2 - day1); |
| | | } else // 不同年 |
| | | { |
| | | // System.out.println("判断day2 - day1 : " + (day2-day1)); |
| | | return day2 - day1; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计算剩余天数,不足一天按一天算 |
| | | * @param expireDate |
| | | * @return |
| | | */ |
| | | public static int retrieveRemainingDays(Date expireDate) { |
| | | Date nowDate = new Date(); |
| | | Calendar startDate = Calendar.getInstance(); |
| | | startDate.setTime(nowDate); |
| | | Calendar endDate = Calendar.getInstance(); |
| | | endDate.setTime(expireDate); |
| | | Calendar date = (Calendar) startDate.clone(); |
| | | int daysBetween = 0; |
| | | while (date.before(endDate)) { |
| | | date.add(Calendar.DAY_OF_MONTH, 1); |
| | | daysBetween++; |
| | | } |
| | | return daysBetween; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月第一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getFirstDayOfMonthString() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 0); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime()) + " 00:00:00"; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月最后一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getLastDayOfMonthString() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 1); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 0); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(calendar.getTime()) + " 23:59:59"; |
| | | } |
| | | /** |
| | | * 获取当前月最后一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getLastDayOfMonth() { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | |
| | | calendar.add(Calendar.MONTH, 1); |
| | | |
| | | calendar.set(Calendar.DAY_OF_MONTH, 0); |
| | | // 格式化日期 |
| | | |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间(年月日) |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getDayOfMonthString() { |
| | | Calendar today = Calendar.getInstance(); |
| | | today.set(Calendar.HOUR, 0); |
| | | today.set(Calendar.MINUTE, 0); |
| | | today.set(Calendar.SECOND, 0); |
| | | today.set(Calendar.MILLISECOND, 0); |
| | | // 格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | return sdf.format(today.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 日期转周 |
| | | * |
| | | * @param datetime |
| | | * @return |
| | | */ |
| | | public static String dateToWeek(String datetime) { |
| | | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; |
| | | Calendar cal = Calendar.getInstance(); // 获得一个日历 |
| | | Date datet = null; |
| | | try { |
| | | if (StringUtils.isNotEmpty(datetime)) { |
| | | datet = f.parse(datetime); |
| | | cal.setTime(datet); |
| | | } |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个周中的某天。 |
| | | if (w < 0) |
| | | w = 0; |
| | | return weekDays[w]; |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | // List<String> beforeDays = getBeforeDays(15); |
| | | // System.out.println(beforeDays); |
| | | List<String> yearMonths = getYearMonths(); |
| | | System.out.println(yearMonths); |
| | | List<String> latest12Month = getLatest12Month(LocalDate.now(), 10); |
| | | System.out.println(latest12Month); |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年月字符串 |
| | | * @return 年月字符串 |
| | | */ |
| | | public static String getCurrentDateString_YYYY_MM() { |
| | | String nowtime = getCurrentDate().format(format_yms); |
| | | return nowtime; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年字符串 |
| | | * @return |
| | | */ |
| | | public static String getCurrentDateString_YYYY() { |
| | | String nowtime = getCurrentDate().format(format_y); |
| | | return nowtime; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年第一天 |
| | | * @return 当前年第一天时间 |
| | | */ |
| | | public static Date getFirstDayOfYear() { |
| | | Calendar currCal=Calendar.getInstance(); |
| | | int currentYear = currCal.get(Calendar.YEAR); |
| | | return getYearFirst(currentYear); |
| | | } |
| | | |
| | | /** |
| | | * 获取某年第一天日期 |
| | | * @param year 年份 |
| | | * @return Date |
| | | */ |
| | | public static Date getYearFirst(int year){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.clear(); |
| | | calendar.set(Calendar.YEAR, year); |
| | | Date currYearFirst = calendar.getTime(); |
| | | return currYearFirst; |
| | | } |
| | | |
| | | /** |
| | | * 获取上月第一天时间 |
| | | * @return 上月第一天时间 |
| | | */ |
| | | public static Date getLastMonthFirst(){ |
| | | // 上月起始 |
| | | Calendar lastMonthFirstDateCal = Calendar.getInstance(); |
| | | lastMonthFirstDateCal.add(Calendar.MONTH,-1); |
| | | lastMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1); |
| | | String lastMonthFirstTime = yyyyMMdd_format.format(lastMonthFirstDateCal.getTime()) + " 00:00:00"; |
| | | return stringToDateStandard(lastMonthFirstTime); |
| | | } |
| | | |
| | | /** |
| | | * 获取上月最后一天时间 |
| | | * @return 最后一天时间 |
| | | */ |
| | | public static Date getLastMonthEnd(){ |
| | | // 上月末尾 |
| | | Calendar lastMonthEndDateCal = Calendar.getInstance(); |
| | | lastMonthEndDateCal.add(Calendar.MONTH,-1); |
| | | lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | String lastMonthEndTime = yyyyMMdd_format.format(lastMonthEndDateCal.getTime()) + " 23:59:59"; |
| | | return stringToDateStandard(lastMonthEndTime); |
| | | } |
| | | |
| | | /** |
| | | * 获取近20天日期 |
| | | * @return 近20天日期 |
| | | */ |
| | | public static List<Date> getOldDays() { |
| | | List<Date> dates = new ArrayList<>(); |
| | | Date date = new Date(); |
| | | for (int i = 19; i >= 0; i--) { |
| | | Date date1 = DateUtils.addDay(date, -i); |
| | | dates.add(date1); |
| | | } |
| | | return dates; |
| | | } |
| | | |
| | | /** |
| | | * 获取上月最后一天时间 |
| | | * @return 上月最后一天时间 |
| | | */ |
| | | public static String getOldMonthLastDay(){ |
| | | Calendar calendar=Calendar.getInstance(); |
| | | int month=calendar.get(Calendar.MONTH); |
| | | calendar.set(Calendar.MONTH, month-1); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | return format_yyymmdd.format(calendar.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 获取上月第一天时间 |
| | | * @return 上月第一天时间 |
| | | */ |
| | | public static String getOldMonthFirstDay(){ |
| | | Calendar calendar=Calendar.getInstance(); |
| | | calendar.add(Calendar.MONTH, -1); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | return format_yyymmdd.format(calendar.getTime()); |
| | | } |
| | | |
| | | /** |
| | | * 获取某年的月份最后一天时间 |
| | | * @param year 年份 |
| | | * @return 月份list |
| | | */ |
| | | public static List<String> getYearLastMonths(Integer year){ |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(getLastDayOfMonth(year,1)); |
| | | list.add(getLastDayOfMonth(year,2)); |
| | | list.add(getLastDayOfMonth(year,3)); |
| | | list.add(getLastDayOfMonth(year,4)); |
| | | list.add(getLastDayOfMonth(year,5)); |
| | | list.add(getLastDayOfMonth(year,6)); |
| | | list.add(getLastDayOfMonth(year,7)); |
| | | list.add(getLastDayOfMonth(year,8)); |
| | | list.add(getLastDayOfMonth(year,9)); |
| | | list.add(getLastDayOfMonth(year,10)); |
| | | list.add(getLastDayOfMonth(year,11)); |
| | | list.add(getLastDayOfMonth(year,12)); |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年的月份第一天时间 |
| | | * @param year 年份 |
| | | * @return 月份list |
| | | */ |
| | | public static List<String> getYearFirstMonths(Integer year){ |
| | | List<String> list = new ArrayList<>(); |
| | | list.add(getFirstDayOfMonth(year,1)); |
| | | list.add(getFirstDayOfMonth(year,2)); |
| | | list.add(getFirstDayOfMonth(year,3)); |
| | | list.add(getFirstDayOfMonth(year,4)); |
| | | list.add(getFirstDayOfMonth(year,5)); |
| | | list.add(getFirstDayOfMonth(year,6)); |
| | | list.add(getFirstDayOfMonth(year,7)); |
| | | list.add(getFirstDayOfMonth(year,8)); |
| | | list.add(getFirstDayOfMonth(year,9)); |
| | | list.add(getFirstDayOfMonth(year,10)); |
| | | list.add(getFirstDayOfMonth(year,11)); |
| | | list.add(getFirstDayOfMonth(year,12)); |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年某月最后一天 |
| | | * @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.DAY_OF_MONTH); |
| | | //设置日历中月份的最大天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, lastDay); |
| | | //格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String lastDayOfMonth = sdf.format(cal.getTime()); |
| | | return lastDayOfMonth; |
| | | } |
| | | |
| | | /** |
| | | * 获取某年某月的第一天 |
| | | * @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.getActualMinimum(Calendar.DAY_OF_MONTH); |
| | | //设置日历中月份的最小天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, firstDay); |
| | | //格式化日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String firstDayOfMonth = sdf.format(cal.getTime()); |
| | | |
| | | return firstDayOfMonth; |
| | | } |
| | | |
| | | /** |
| | | * 获取20天前那天时间 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getTwentyDay() { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(new Date()); |
| | | cal.add(Calendar.DATE, -20); |
| | | |
| | | return DateUtil.format(cal.getTime(),"yyyy-MM-dd"); |
| | | } |
| | | |
| | | /** |
| | | * 获取之前的日期 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getBeforeDay(Integer amount) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(new Date()); |
| | | cal.add(Calendar.DATE, -amount); |
| | | return DateUtil.format(cal.getTime(),"yyyy-MM-dd"); |
| | | } |
| | | |
| | | /** |
| | | * 获取最近本月20天数据 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getMonthTwentyDay() { |
| | | Date date = new Date(); |
| | | Long offset = DateUtil.between(new Date(), DateUtil.beginOfMonth(date), DateUnit.DAY); |
| | | if (offset >= 20) { |
| | | return getTwentyDay(); |
| | | } else { |
| | | //距离月初不足20天 |
| | | Calendar calendar = new GregorianCalendar(); |
| | | calendar.setTime(date); |
| | | //获得本月第一天 |
| | | calendar.add(Calendar.MONTH, 0); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | return DateUtil.format(calendar.getTime(),"yyyy-MM-dd"); |
| | | } |
| | | |
| | | } |
| | | |
| | | public static List<String> getBeforeDays(int before) { |
| | | List<String> dates = new ArrayList<>(); |
| | | for (int i = before; i >= 0; i--) { |
| | | dates.add(getBeforeDay(i)); |
| | | } |
| | | return dates; |
| | | } |
| | | |
| | | /** |
| | | * 获取当年所有月份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static List<String> getYearMonths() { |
| | | Calendar instance = Calendar.getInstance(); |
| | | int year = instance.get(Calendar.YEAR); |
| | | List<String> months = new ArrayList<>(); |
| | | for (int i = 1; i <= 12; i++) { |
| | | String result = String.valueOf(year); |
| | | if (i < 10) { |
| | | result = result + "-0" +i; |
| | | } else { |
| | | result = result + "-"+i; |
| | | } |
| | | months.add(result); |
| | | } |
| | | return months; |
| | | } |
| | | |
| | | //获取当前12个月 |
| | | public static List<String> getLatest12Month(LocalDate date,int num){ |
| | | List<String> monthList = new ArrayList<>(); |
| | | for(int i = 0;i <= num-1; i++){ |
| | | LocalDate localDate = date.minusMonths(i); |
| | | String month = localDate.toString().substring(0,7); |
| | | monthList.add(month); |
| | | } |
| | | return monthList; |
| | | } |
| | | |
| | | |
| | | } |