栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java开发中日期加减操作工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java开发中日期加减操作工具类

Java开发中日期加减操作工具类

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;


public final class DateUtils {
    
    public static Long getDurationSecconds(String startTime, String endTime, String pattern) {
        final LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).getSeconds();
    }

    
    public static Long getDurationSecconds(Date startTime, Date endTime, String pattern) {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        final LocalDateTime start = LocalDateTime.parse(sdf.format(startTime), DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(sdf.format(endTime), DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).getSeconds();
    }

    
    public static Long getDurationMinutes(String startTime, String endTime, String pattern) {
        final LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).toMinutes();
    }

    
    public static Long getDurationMinutes(Date startTime, Date endTime, String pattern) {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        final LocalDateTime start = LocalDateTime.parse(sdf.format(startTime), DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(sdf.format(endTime), DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).toMinutes();
    }

    
    public static Long getDurationHours(String startTime, String endTime, String pattern) {
        final LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).toHours();
    }

    
    public static Long getDurationHours(Date startTime, Date endTime, String pattern) {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        final LocalDateTime start = LocalDateTime.parse(sdf.format(startTime), DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(sdf.format(endTime), DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).toHours();
    }


    
    public static Long getDurationDays(String startTime, String endTime, String pattern) {
        final LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).toDays();
    }

    
    public static Long getDurationDays(Date startTime, Date endTime, String pattern) {
        final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        final LocalDateTime start = LocalDateTime.parse(sdf.format(startTime), DateTimeFormatter.ofPattern(pattern));
        final LocalDateTime end = LocalDateTime.parse(sdf.format(endTime), DateTimeFormatter.ofPattern(pattern));
        return Duration.between(start, end).toDays();
    }

    
    public static Long currentDateSeccondsAddTarget(Long target, DateEnum dateEnum) throws IllegalAccessException {
        final LocalDateTime now = LocalDateTime.now();
        final LocalDateTime targetDate = getLocalDateTime(target, dateEnum, now);
        return Duration.between(now, targetDate).getSeconds();
    }


    
    public static String customizeDateAddTargetDate(String customerDate, String pattern, Long target, DateEnum dateEnum) throws IllegalAccessException {
        final LocalDateTime customize = LocalDate.parse(customerDate, DateTimeFormatter.ofPattern(pattern)).atTime(LocalTime.now());
        final LocalDateTime targetDate = getLocalDateTime(target, dateEnum, customize);
        return targetDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    
    public static String currentDateSeccondsAddTarget(Long target, DateEnum dateEnum, String pattern) throws IllegalAccessException {
        return getLocalDateTime(target, dateEnum, LocalDateTime.now()).format(DateTimeFormatter.ofPattern(pattern));
    }


    private static LocalDateTime getLocalDateTime(Long target, DateEnum dateEnum, LocalDateTime now) throws IllegalAccessException {
        LocalDateTime targetDate;
        if (dateEnum.name().equals(DateEnum.SECONDS.name())) {
            targetDate = now.plusSeconds(target);
        } else if (dateEnum.name().equals(DateEnum.MINUTES.name())) {
            targetDate = now.plusMinutes(target);
        } else if (dateEnum.name().equals(DateEnum.HOUR.name())) {
            targetDate = now.plusHours(target);
        } else if (dateEnum.name().equals(DateEnum.DAY.name())) {
            targetDate = now.plusDays(target);
        } else if (dateEnum.name().equals(DateEnum.MONTH.name())) {
            targetDate = now.plusMonths(target);
        } else if (dateEnum.name().equals(DateEnum.WEEK.name())) {
            targetDate = now.plusWeeks(target);
        } else if (dateEnum.name().equals(DateEnum.YEAR.name())) {
            targetDate = now.plusYears(target);
        } else {
            throw new IllegalAccessException("DateEnum object is Illegal");
        }
        return targetDate;
    }

    public static void main(String[] args) throws IllegalAccessException {
        //当前时间减去5天
        final String target = currentDateSeccondsAddTarget(Long.valueOf("-5"), DateEnum.DAY, "yyyy-MM-dd");
        System.out.println(target);
        //当前时间加上100天
        final Long aLong = currentDateSeccondsAddTarget(100L, DateEnum.DAY);
        System.out.println(aLong);
        //指定时间、指定格式加上目标天数
        final String targetDate = customizeDateAddTargetDate("2022-06-06", "yyyy-MM-dd", -3L, DateEnum.DAY);
        System.out.println(targetDate);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号