java8日期类处理-LocalDateUtil

本文详细介绍了Java8中LocalDateUtil类的使用方法,包括创建日期、日期比较、日期格式化等核心功能,帮助开发者更好地进行日期处理。
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author fanghui
 * @create 2020-4-8 18:38
 **/
public class LocalDateUtil {

    /**
     * @param localDate
     * @return
     * @Author: fanghui
     * @Date: 2020-04-08 05:21:59
     * @Description: LocalDate转Date
     **/
    public static Date localDate2Date(LocalDate localDate) {
        if (null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());

    }

    /**
     * @param date
     * @return
     * @Author: fanghui
     * @Date: 2020-04-08 05:21:51
     * @Description: Date转LocalDate
     **/
    public static LocalDate date2LocalDate(Date date) {
        if (null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

    }

    /**
     * @param date
     * @return
     * @Author: fanghui
     * @Date: 2021-04-08 05:21:51
     * @Description: 截止当前时间之前的时间段(之前/之后)
     **/
    public static boolean date2LocalDate(Date date) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        now.isAfter(LocalDateTime.parse(u.getFromTime(), pattern))
        now.isBefore(LocalDateTime.parse(u.getToTime(), pattern)))

        可以这样使用:
        
        //fromTime<now<toTime置为推送中
        List<UpgradePushListVO> pushExist = list.stream()
                .filter(u -> now.isAfter(LocalDateTime.parse(u.getFromTime(), pattern)) &&
                    now.isBefore(LocalDateTime.parse(u.getToTime(), pattern)))
                .collect(toList());
        log.info("当前时间大于开始时间,小于结束时间,推送中,pushIds:【{}】",pushExist.stream().map(UpgradePushListVO::getId).collect(toList()));
       
      或者使用下面的getTimeDifference方法返回的也是boolean 类型
      Duration.between(LocalDateTime.now(), examinationResponse.getEndTime()).isNegative()
    }

    /**
     * @param endTime
     * @param startTime
     * @return
     * @Author: fanghui
     * @Date: 2020/12/18
     * @Description: 获取两个时间相差时分秒
     **/
    private String getTimeDifference(LocalDateTime endTime, LocalDateTime startTime) {
        Duration duration = Duration.between(startTime, endTime);
        long hour = duration.getSeconds() / ChronoUnit.HOURS.getDuration().getSeconds();
        long minute = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) / ChronoUnit.MINUTES.getDuration().getSeconds();
        long second = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) - minute * ChronoUnit.MINUTES.getDuration().getSeconds();
        return hour + "时" + minute + "分" + second + "秒";
    }

    /**
     * @param date
     * @return
     * @Author: fanghui
     * @Date: 2020-04-08 05:22:09
     * @Description: Date转换为LocalDateTime
     **/
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * @param localDateTime
     * @return
     * @Author: fanghui
     * @Date: 2020-04-08 05:22:54
     * @Description: LocalDateTime转换为Date
     **/
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static LocalDateTime strTranslocalDateTime(String str) {
        if (MyUtil.isBlank(str)) {
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(str, df);
        return ldt;
    }

    public static String localDateTimeTransStr(LocalDateTime localDateTime) {
        if (MyUtil.isBlank(localDateTime)) {
            return null;
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String localTime = df.format(localDateTime);
        return localTime;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值