| | |
| | | import java.text.DecimalFormat; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.DayOfWeek; |
| | | import java.time.LocalDate; |
| | | import java.time.ZoneId; |
| | | import java.time.temporal.ChronoField; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.util.regex.Pattern; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | public class DateUtils { |
| | | |
| | |
| | | /** |
| | | * @return 得到明天 |
| | | */ |
| | | public static Date getNextDay(Date d1){ |
| | | long d2 = d1.getTime() + 86400*1000; |
| | | public static Date getNextDay(Date d1) { |
| | | long d2 = d1.getTime() + 86400 * 1000; |
| | | return new Date(d2); |
| | | } |
| | | |
| | | /** |
| | | * @return 得到昨天 |
| | | */ |
| | | public static Date getPreviousDay(Date d1){ |
| | | long d2 = d1.getTime() - 86400*1000; |
| | | public static Date getPreviousDay(Date d1) { |
| | | long d2 = d1.getTime() - 86400 * 1000; |
| | | return new Date(d2); |
| | | } |
| | | |
| | | /** |
| | | * @return 返回时间差的语言描述 如1天2小时5分6秒 |
| | | */ |
| | |
| | | |
| | | public static String dateProportion(Date start, Date end) { |
| | | float differentSecond = DateUtils.differentSecond(start, end); |
| | | float f = differentSecond / DAYTIMESUNSET *100; |
| | | return String.format("%.2f", f)+"%"; |
| | | float f = differentSecond / DAYTIMESUNSET * 100; |
| | | return String.format("%.2f", f) + "%"; |
| | | } |
| | | |
| | | public static Date strToDate(String dateStr,String format) { |
| | | public static Date strToDate(String dateStr, String format) { |
| | | SimpleDateFormat sf = new SimpleDateFormat(format); |
| | | Date result = null; |
| | | try { |
| | |
| | | public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS"; |
| | | public static final String STR_HHMMSS = "HH:mm:ss"; |
| | | public static final String STR_HHMM = "HH:mm"; |
| | | |
| | | /** |
| | | * <p> |
| | | * Description: 实际投标月份 |
| | |
| | | * Description: 修改时间为指定时间当天的23:59:59.000 |
| | | * </p> |
| | | * |
| | | * @param date |
| | | * 需要修改的时间 |
| | | * @param date 需要修改的时间 |
| | | * @return 修改后的时间 |
| | | */ |
| | | public static Date fillTime(Date date) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * @param date |
| | | * 当前时间 |
| | | * @param i |
| | | * 往前几天 |
| | | * @param date 当前时间 |
| | | * @param i 往前几天 |
| | | * @return |
| | | */ |
| | | public static Date fillBeforeTime(Date date, Integer i) { |
| | |
| | | * Description: 修改时间为指定时间前一天的00:00:00 |
| | | * </p> |
| | | * |
| | | * @param date |
| | | * 需要修改的时间 |
| | | * @param date 需要修改的时间 |
| | | * @return 修改后的时间 |
| | | */ |
| | | public static Date plusTime(Date date, Integer i) { |
| | |
| | | * </p> |
| | | * 如: 2013-11-11 18:56:33 ---> 2013-11-11 00:00:00 |
| | | * |
| | | * @param date |
| | | * 需要修改的时间 |
| | | * @param date 需要修改的时间 |
| | | * @return 修改后的时间 |
| | | */ |
| | | public static Date removeTime(Date date) { |
| | |
| | | * Description: 按默认格式(yyyy-MM-dd HH:mm:ss.SSS)获取时间字符串 |
| | | * </p> |
| | | * |
| | | * @param date |
| | | * 要转换的日期 |
| | | * @param date 要转换的日期 |
| | | * @return 转换后的时间字符串 |
| | | */ |
| | | public static String format(Date date) { |
| | |
| | | return df.format(date); |
| | | } |
| | | |
| | | public static Date parseDate(Date date,String format){ |
| | | public static Date parseDate(Date date, String format) { |
| | | SimpleDateFormat df = new SimpleDateFormat(format); |
| | | try { |
| | | date = df.parse(df.format(date)); |
| | |
| | | return date; |
| | | } |
| | | |
| | | public static Date getDelayedYear(){ |
| | | public static Date getDelayedYear() { |
| | | Calendar curr = Calendar.getInstance(); |
| | | curr.set(Calendar.YEAR,curr.get(Calendar.YEAR)+1); |
| | | Date date=curr.getTime(); |
| | | curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1); |
| | | Date date = curr.getTime(); |
| | | return date; |
| | | } |
| | | |
| | | /** |
| | | * <p> |
| | | * Description: 按指定格式获取时间字符串 |
| | | * </p> |
| | | * |
| | | * @param date |
| | | * 要转换的日期 |
| | | * @param format |
| | | * 格式,例如:yyyy-MM-dd HH:mm:ss.SSS |
| | | * @param date 要转换的日期 |
| | | * @param format 格式,例如:yyyy-MM-dd HH:mm:ss.SSS |
| | | * @return 转换后的时间字符串 |
| | | */ |
| | | public static String format(Date date, String format) { |
| | |
| | | * Description: 加月函数 |
| | | * </p> |
| | | * |
| | | * @param month |
| | | * 月份数 |
| | | * @param month 月份数 |
| | | * @return |
| | | */ |
| | | public static Date addMonth(Integer month, Date time) { |
| | |
| | | // 计算秒数 |
| | | Long days = (t2 - t1) / (1000); |
| | | return days.intValue(); |
| | | }catch (Exception e) { |
| | | } catch (Exception e) { |
| | | return 0; |
| | | } |
| | | |
| | |
| | | double hours = (t2 - t1) / (1000 * 60 * 60); |
| | | return Double.valueOf(new DecimalFormat("#.00").format(hours)); |
| | | } |
| | | |
| | | //根据当前时间和出生日期获取年龄 |
| | | public static int getAge(Date birthDay){ |
| | | public static int getAge(Date birthDay) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 取出系统当前时间的年、月、日部分 |
| | | int yearNow = cal.get(Calendar.YEAR); |
| | |
| | | // 当前年份与出生年份相减,初步计算年龄 |
| | | int age = yearNow - yearBirth; |
| | | // 当前月份与出生日期的月份相比,如果月份小于出生月份,则年龄上减1,表示不满多少周岁 |
| | | if (monthNow <= monthBirth){ |
| | | if (monthNow <= monthBirth) { |
| | | // 如果月份相等,在比较日期,如果当前日,小于出生日,也减1,表示不满多少周岁 |
| | | if (monthNow == monthBirth){ |
| | | if (monthNow == monthBirth) { |
| | | if (dayOfMonthNow < dayOfMonthBirth) { |
| | | age--; |
| | | } |
| | | }else { |
| | | } else { |
| | | age--; |
| | | } |
| | | |
| | |
| | | public static Date addDays(Date startTime, Integer day) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(startTime); |
| | | c.add(Calendar.DATE, day ); |
| | | c.add(Calendar.DATE, day); |
| | | return c.getTime(); |
| | | } |
| | | /*public static Date addHours(Date startTime, Integer hours) { |
| | |
| | | return c.getTime(); |
| | | }*/ |
| | | |
| | | public static Date toDate(String date, String format){ |
| | | public static Date toDate(String date, String format) { |
| | | SimpleDateFormat df = new SimpleDateFormat(format); |
| | | try { |
| | | return df.parse(date); |
| | |
| | | return null; |
| | | } |
| | | |
| | | public static Date toDateFull(String date){ |
| | | public static Date toDateFull(String date) { |
| | | SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME); |
| | | try { |
| | | return df.parse(date); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | public static Date toDateMedium(String date){ |
| | | |
| | | public static Date toDateMedium(String date) { |
| | | SimpleDateFormat df = new SimpleDateFormat(STR_DATE_TIME_SMALL); |
| | | try { |
| | | Date dd = df.parse(date); |
| | |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static Integer getDate(Date date) { |
| | | if (date != null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static Integer getYear() { |
| | | return getYear(new Date()); |
| | | } |
| | | |
| | | public static Integer getYear(Date date) { |
| | | if (date != null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static Integer getMonth() { |
| | | return getMonth(new Date()); |
| | | } |
| | | |
| | | public static Integer getMonth(Date date) { |
| | | if (date != null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | int month = calendar.get(Calendar.MONTH); |
| | | return month+1; |
| | | return month + 1; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static Integer getDay() { |
| | | return getDay(new Date()); |
| | | } |
| | | |
| | | public static Integer getDay(Date date) { |
| | | if (date != null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | |
| | | } |
| | | |
| | | |
| | | public static Date[] getMonthStartTimeAndEndTime(Integer month){ |
| | | public static Date[] getMonthStartTimeAndEndTime(Integer month) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | Date[] dates = new Date[2]; |
| | | Date startTime = null; |
| | | Date endsTime = null; |
| | | if(month != null){ |
| | | if (month != null) { |
| | | calendar.set(Calendar.MONTH, month); |
| | | //获得到本月的第一天 |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); |
| | |
| | | dates[1] = endsTime; |
| | | return dates; |
| | | } |
| | | public static Date[] getMonthStartTimeAndEndTime(Date date){ |
| | | |
| | | public static Date[] getMonthStartTimeAndEndTime(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | Integer month = calendar.get(Calendar.MONTH); |
| | | Date[] dates = new Date[2]; |
| | | Date startTime = null; |
| | | Date endsTime = null; |
| | | if(month != null){ |
| | | if (month != null) { |
| | | calendar.set(Calendar.MONTH, month); |
| | | //获得到本月的第一天 |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); |
| | |
| | | /** |
| | | * 获取days天的时间区间,endTime 为当天的后一天0点, |
| | | * startTime 为endTime前days天 |
| | | * |
| | | * @param days |
| | | * @return |
| | | */ |
| | |
| | | /** |
| | | * 根据某一年获取最后一天的时间 |
| | | * 2013 ---> 2013-12-31 23:59:59.000 |
| | | * |
| | | * @param year |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 获取start/end的所有日期字符串 格式yyyy-MM-dd |
| | | * |
| | | * @param start |
| | | * @param end |
| | | * @return |
| | | */ |
| | | public static List<String> getDatesStringList(Date start, Date end,String strDate) { |
| | | public static List<String> getDatesStringList(Date start, Date end, String strDate) { |
| | | List<String> list = new ArrayList<>(); |
| | | int i = getDays(start, end); |
| | | for(int j = 0; j <= i; j++) { |
| | | if(j == 0) { |
| | | for (int j = 0; j <= i; j++) { |
| | | if (j == 0) { |
| | | list.add(format(start, strDate)); |
| | | } else { |
| | | list.add(format(plusTime(start, j), strDate)); |
| | |
| | | |
| | | /** |
| | | * 获取start/end的所有日期字符串 格式yyyy-MM-dd |
| | | * |
| | | * @param start |
| | | * @param end |
| | | * @return |
| | |
| | | public static List<String> getDatesStringList(Date start, Date end) { |
| | | List<String> list = new ArrayList<>(); |
| | | int i = getDays(start, end); |
| | | for(int j = 0; j <= i; j++) { |
| | | if(j == 0) { |
| | | for (int j = 0; j <= i; j++) { |
| | | if (j == 0) { |
| | | list.add(format(start, STR_DATE)); |
| | | } else { |
| | | list.add(format(plusTime(start, j), STR_DATE)); |
| | |
| | | |
| | | /** |
| | | * 获取start/end的所有日期字符串 格式yyyyMMdd |
| | | * |
| | | * @param start |
| | | * @param end |
| | | * @return |
| | |
| | | public static List<String> getDatesStringList2(Date start, Date end) { |
| | | List<String> list = new ArrayList<>(); |
| | | int i = getDays(start, end); |
| | | for(int j = 0; j <= i; j++) { |
| | | if(j == 0) { |
| | | for (int j = 0; j <= i; j++) { |
| | | if (j == 0) { |
| | | list.add(format(start, STRDATE)); |
| | | } else { |
| | | list.add(format(plusTime(start, j), STRDATE)); |
| | |
| | | return list; |
| | | } |
| | | |
| | | public static List<String> getMonthBetween(Date start, Date end){ |
| | | public static List<String> getMonthBetween(Date start, Date end) { |
| | | List<String> list = new ArrayList<>(); |
| | | Calendar min = Calendar.getInstance(); |
| | | Calendar max = Calendar.getInstance(); |
| | |
| | | max.setTime(end); |
| | | max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); |
| | | Calendar curr = min; |
| | | while (curr.before(max)){ |
| | | list.add(format(curr.getTime(),STR_YEAR_MONTH)); |
| | | curr.add(Calendar.MONTH,1); |
| | | while (curr.before(max)) { |
| | | list.add(format(curr.getTime(), STR_YEAR_MONTH)); |
| | | curr.add(Calendar.MONTH, 1); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取dateStr的日期格式yyyy-MM-dd |
| | | * |
| | | * @param dateStr |
| | | * @return |
| | | */ |
| | |
| | | |
| | | /** |
| | | * 获取dateStr的日期格式yyyyMMdd |
| | | * |
| | | * @param dateStr |
| | | * @return |
| | | */ |
| | |
| | | return startTime == null ? removeTime(new Date()) : startTime; |
| | | } |
| | | |
| | | public static Date getFormatDate(String dateStr,String format) { |
| | | public static Date getFormatDate(String dateStr, String format) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(format); |
| | | Date startTime = null; |
| | | try { |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param time |
| | | * @param n |
| | | * @return 在一个时间上加秒 |
| | | * @return 在一个时间上加秒 |
| | | */ |
| | | public static Date addSecond(Date time,Integer n) { |
| | | public static Date addSecond(Date time, Integer n) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(time); |
| | | c.add(Calendar.SECOND, n); |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param time |
| | | * @param n |
| | | * @return 在一个时间上加分钟 |
| | | * @return 在一个时间上加分钟 |
| | | */ |
| | | public static Date addMinute(Date time,Integer n) { |
| | | public static Date addMinute(Date time, Integer n) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(time); |
| | | c.add(Calendar.MINUTE, n); |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param time |
| | | * @param n |
| | | * @return 在一个时间上加小时 |
| | | * @return 在一个时间上加小时 |
| | | */ |
| | | public static Date addHour(Date time,Integer n) { |
| | | public static Date addHour(Date time, Integer n) { |
| | | Calendar c = Calendar.getInstance(); |
| | | c.setTime(time); |
| | | c.add(Calendar.HOUR_OF_DAY, n); |
| | |
| | | |
| | | /** |
| | | * 获取start/end的所有日期字符串 格式yyyy-MM-dd |
| | | * |
| | | * @param start |
| | | * @param end |
| | | * @return |
| | |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取start/end的所有日期字符串 格式yyyy |
| | | * |
| | | * @param start |
| | | * @param end |
| | | * @return |
| | |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前时间 定位到小时 |
| | | * 格式为 yyyy-MM-dd hh:mm |
| | | * 例:2018-02-27 11:00 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getNowHourDate() { |
| | |
| | | |
| | | /** |
| | | * 获取每日8:11或11:11的时间点 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getNowHourDateTo() { |
| | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(now); |
| | | int hour = calendar.get(Calendar.HOUR_OF_DAY); |
| | | if(hour != 8 && hour != 11) { |
| | | if (hour != 8 && hour != 11) { |
| | | return null; |
| | | } |
| | | int minute = calendar.get(Calendar.MINUTE); |
| | | if(minute < 11) { |
| | | if (minute < 11) { |
| | | return null; |
| | | } |
| | | String dateStr = format(now, STR_DATE); |
| | |
| | | |
| | | /** |
| | | * 获取每日8:00、13:00、20:00的时间点 |
| | | * |
| | | * @return |
| | | */ |
| | | public static List<Date> getHourDateList(Date time) { |
| | | List<Date> result = new ArrayList<>(); |
| | | result.add(addHour(time,8)); |
| | | result.add(addHour(time,13)); |
| | | result.add(addHour(time,20)); |
| | | result.add(addHour(time, 8)); |
| | | result.add(addHour(time, 13)); |
| | | result.add(addHour(time, 20)); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 获取上月的最后一天 |
| | | * |
| | | * @return |
| | | */ |
| | | public static Date getPreviousMonthLastDay() { |
| | |
| | | |
| | | /** |
| | | * 获取end时间月的第一天 |
| | | * |
| | | * @param end 月份最后一天 |
| | | * @return |
| | | */ |
| | |
| | | int hour = 0; |
| | | if (time <= 0) { |
| | | return "0小时"; |
| | | } |
| | | else { |
| | | } else { |
| | | BigDecimal bigDecimal = new BigDecimal(time); |
| | | BigDecimal bigDecimal1 = new BigDecimal(3600); |
| | | BigDecimal hourBigDecimal = new BigDecimal(0); |
| | | hourBigDecimal = new BigDecimal(String.valueOf(bigDecimal)). |
| | | divide(new BigDecimal(String.valueOf(bigDecimal1)),2, BigDecimal.ROUND_HALF_UP); |
| | | divide(new BigDecimal(String.valueOf(bigDecimal1)), 2, BigDecimal.ROUND_HALF_UP); |
| | | timeStr = hourBigDecimal + "小时"; |
| | | } |
| | | return timeStr; |
| | |
| | | retStr = "" + i; |
| | | return retStr; |
| | | } |
| | | |
| | | /** |
| | | * 验证时间 |
| | | * |
| | |
| | | Calendar cal = Calendar.getInstance(); |
| | | cal.setTime(theDate); |
| | | String[] times = planTime.split(":"); |
| | | cal.set(Calendar.HOUR_OF_DAY,Integer.parseInt(times[0])); |
| | | cal.set(Calendar.MINUTE,Integer.parseInt(times[1])); |
| | | cal.set(Calendar.SECOND,Integer.parseInt(times[2])); |
| | | cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0])); |
| | | cal.set(Calendar.MINUTE, Integer.parseInt(times[1])); |
| | | cal.set(Calendar.SECOND, Integer.parseInt(times[2])); |
| | | return cal.getTime(); |
| | | } |
| | | |
| | | public static long getTimeWithOutDay(Date d) { |
| | | return d.getTime()/1000%86400; |
| | | return d.getTime() / 1000 % 86400; |
| | | } |
| | | |
| | | /** |
| | | * @Description: long类型转换成日期 |
| | | * |
| | | * @param lo 毫秒数 |
| | | * @return String yyyy-MM-dd HH:mm:ss |
| | | * @Description: long类型转换成日期 |
| | | */ |
| | | public static String longToDate(long lo){ |
| | | public static String longToDate(long lo) { |
| | | Date date = new Date(lo); |
| | | SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | return sd.format(date); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param startDate |
| | | * @param endDate |
| | | * @return |
| | | */ |
| | | public static List<Date> getWeekDays(LocalDate startDate, LocalDate endDate) { |
| | | return null; |
| | | List<Date> result = new ArrayList<>(); |
| | | List<LocalDate> dateList = Stream.iterate(startDate, localDate -> localDate.plusDays(1)) |
| | | .limit(ChronoUnit.DAYS.between(startDate, endDate) + 1) |
| | | .filter(localDate -> DayOfWeek.SATURDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))) || DayOfWeek.SUNDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK)))) |
| | | .collect(Collectors.toList()); |
| | | dateList.forEach(localDate -> result.add(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()))); |
| | | return result; |
| | | } |
| | | } |