dateUtil

       import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: rd13
 * @date: 2019/11/22.
 * 使用jdk8各种对日期的操作
 */
public class DateUtil {
    /*当前日期增加几天*/
    public static Date pulsDays(Date date, int days) {
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime newLocalDateTime = localDateTime.plusDays(days);
        return localDateTimeToDate(newLocalDateTime);
    }

    /*当前日期增加几年*/
    public Date pulsYears(Date date, int years) {
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime newLocalDateTime = localDateTime.plusYears(years);
        return localDateTimeToDate(newLocalDateTime);
    }

    /**
     * localDateTime转化为Date
     *
     * @param localDateTime yyyy-mm-ddT:hh:mm:ss:aaa 标准utc格式
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        return Date.from(zdt.toInstant());
    }


    /*把Date转化为LocalDate*/
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        return localDate;
    }

    /*把Date转化为LocalDateTime*/
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDate = instant.atZone(zoneId).toLocalDateTime();
        return localDate;
    }

    /*设置为每个月最后一天*/
    public Date getEndMonthTime(Date date) throws ParseException {
        //获取当前时间戳对象用来过渡
        Instant ins = date.toInstant();
        //设置时区
        ZoneId zd = ZoneId.systemDefault();
        //把转化成localdate(jdk8不包含具体时间的对象 yyy-mm-dd)
        LocalDate localDate = ins.atZone(zd).toLocalDate();
        String localString = localDate.with(TemporalAdjusters.lastDayOfMonth()) + ":23" + ":59" + ":59";
        return new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").parse(localString);
    }

    /*设置为某一天的结束时间*/
    public static Date getEndDayeTime(Date date) {
        //获取当前时间戳对象用来过渡
        Instant ins = date.toInstant();
        //设置时区
        ZoneId zd = ZoneId.systemDefault();
        //把转化成localdate(jdk8不包含具体时间的对象 yyy-mm-dd)
        LocalDate localDate = ins.atZone(zd).toLocalDate();

        //设置当天的结束时间
        LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

        //LocalDateTime转化为date
        return localDateTimeToDate(endTime);
    }

    /*设置为某一天的开始时间*/
    public static Date getStartDayeTime(Date date) {
        //获取当前时间戳对象用来过渡
        Instant ins = date.toInstant();
        //设置时区
        ZoneId zd = ZoneId.systemDefault();
        //把转化成localdate(jdk8不包含具体时间的对象 yyy-mm-dd)
        LocalDate localDate = ins.atZone(zd).toLocalDate();

        //设置当天的结束时间
        LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MIN);

        //LocalDateTime转化为date
        return localDateTimeToDate(endTime);
    }

    /**
     * 获取当前时间前后几小时的秒数
     * <p>
     * num >0 表示之后
     * <p>
     * num<0 表示之前
     *
     * @return
     */
    public static Long afterHourTime(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + num);
        return calendar.getTime().getTime() / 1000;
    }

    /**
     * 计算两个时间查了多少天
     */
    public static long getTwoTimeDiff(Date startTime, Date endTime) {
        try {
            if (startTime == null || endTime == null) {
                return 0;
            }
            LocalDate startData = startTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate endData = endTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            return endData.toEpochDay() - startData.toEpochDay();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}

    


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容