package com.fdkankan.ucenter.util; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import com.fdkankan.ucenter.entity.IncrementType; import org.joda.time.DateTime; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateUserUtil { public static final String DEF_FMT = "yyyy-MM-dd HH:mm:ss"; public static final String DAY_FMT = "yyyy-MM-dd"; public static final String M_FMT = "yyyy-MM-dd HH:mm"; public static String getLastTime(String time1,String time2){ return getDate(time1).getTime() >getDate(time2).getTime() ? time1 : time2; } public static Date getDate(String time){ try { return new SimpleDateFormat(DEF_FMT).parse(time); }catch (Exception e){ e.printStackTrace(); } return new Date(); } public static String getDate(Date time){ return new SimpleDateFormat(DEF_FMT).format(time); } public static String getZeroDate(){ return new SimpleDateFormat(DAY_FMT).format(new Date()) +" 00:00:00"; } public static String getLastDate(){ return new SimpleDateFormat(DAY_FMT).format(new Date()) +" 23:59:59"; } public static String getDayTime(String time) { try { Date date = new SimpleDateFormat(DEF_FMT).parse(time); return new SimpleDateFormat(DAY_FMT).format(date); }catch (Exception e){ e.printStackTrace(); } return time; } public static DateTime getDateTime(Date userTime,IncrementType incrementType,Integer count) { DateTime dateTime = new DateTime(); if(userTime.getTime() >new Date().getTime()){ dateTime = new DateTime(userTime); } if(count == null){ count = 1; } switch (incrementType.getValidTimeType()){ case 0 : return dateTime.plusYears(incrementType.getValidTime() * count); case 1 : return dateTime.plusMonths(incrementType.getValidTime() * count); case 2 : return dateTime.plusDays(incrementType.getValidTime() * count); default: return dateTime; } } public static Long getLastNowTime() { Date date1 = new Date(); Date date2 = DateUserUtil.getDate(getLastDate()); return ( date2.getTime() - date1.getTime() ) /1000; } public static String getMDate(String time) { try { Date date = new SimpleDateFormat(DEF_FMT).parse(time); return new SimpleDateFormat(M_FMT).format(date); }catch (Exception e){ e.printStackTrace(); } return time; } public static Long between(Date endDate, Date newDate) { long between = DateUtil.between(endDate, newDate, DateUnit.DAY); if(between == 0 && endDate.getTime() > newDate.getTime()){ return 1L; } if(endDate.getTime() < newDate.getTime()){ return 0L; } return between; } public static Date getMonthDate(Date endDate, Integer month) { LocalDateTime localDateTime = endDate.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime().plusMonths(month); return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } /*日期加+1年*/ public static Date dateAddOneYear(Date date,Integer i) { Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(Calendar.YEAR,i); //把日期往后增加一天,整数 往后推,负数往前移动 date=calendar.getTime(); //这个时间就是日期往后推一天的结果 return date; } }