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

Java8 日期、 时间Date操作工具类

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

Java8 日期、 时间Date操作工具类

Java8 日期、 时间Date操作工具类

直接附上操作工具类源码:

package com.janwes;

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


public class DateUtil {

    
    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    
    public static ZonedDateTime dateToZonedDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault());
    }

    
    public static Date toDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    
    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    
    public static Date toDate(ZonedDateTime zonedDateTime) {
        return Date.from(zonedDateTime.toInstant());
    }

    
    public static String dateFormat(Date date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    
    public static Date strDateParseDate(String strDate, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            return simpleDateFormat.parse(strDate);
        } catch (ParseException e) {
            throw new IllegalArgumentException(String.format("parse strDate %s failure, maybe %s and %s is don't match", strDate, strDate, pattern));
        }
    }

    
    public static Date plusDays(Date date, long offsetDay) {
        final LocalDate localDate = DateUtil.dateToLocalDate(date);
        final LocalDate plusLocalDate = localDate.plusDays(offsetDay);
        return DateUtil.toDate(plusLocalDate);
    }

    
    public static LocalDate plusDays(LocalDate localDate, long offsetDay) {
        return localDate.plusDays(offsetDay);
    }

    
    public static LocalDateTime plusDays(LocalDateTime localDateTime, long offsetDay) {
        return localDateTime.plusDays(offsetDay);
    }

    
    public static LocalDate minusDays(LocalDate localDate, long offsetDay) {
        return localDate.minusDays(offsetDay);
    }

    
    public static LocalDateTime minusDays(LocalDateTime localDateTime, long offsetDay) {
        return localDateTime.minusDays(offsetDay);
    }

    
    public static Date minusDays(Date date, long offsetDay) {
        final LocalDate localDate = DateUtil.dateToLocalDate(date);
        final LocalDate minusLocalDate = localDate.minusDays(offsetDay);
        return DateUtil.toDate(minusLocalDate);
    }

    
    public static Date plusMonths(Date date, long offsetMonths) {
        final LocalDate localDate = DateUtil.dateToLocalDate(date);
        final LocalDate plusLocalDate = localDate.plusMonths(offsetMonths);
        return DateUtil.toDate(plusLocalDate);
    }

    
    public static LocalDate plusMonths(LocalDate localDate, long offsetMonths) {
        return localDate.plusMonths(offsetMonths);
    }

    
    public static LocalDateTime plusMonths(LocalDateTime localDateTime, long offsetMonths) {
        return localDateTime.plusMonths(offsetMonths);
    }

    
    public static Date minusMonths(Date date, long offsetMonths) {
        final LocalDate localDate = DateUtil.dateToLocalDate(date);
        final LocalDate plusLocalDate = localDate.minusMonths(offsetMonths);
        return DateUtil.toDate(plusLocalDate);
    }

    
    public static LocalDate minusMonths(LocalDate localDate, long offsetMonths) {
        return localDate.minusMonths(offsetMonths);
    }

    
    public static LocalDateTime minusMonths(LocalDateTime localDateTime, long offsetMonths) {
        return localDateTime.minusMonths(offsetMonths);
    }

    
    public static long diffHours(Date startDate, Date endDate) {
        // Date转成LocalDate
        LocalDateTime startLocalDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime endLocalDateTime= endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        // 计算相差时数
        return ChronoUnit.HOURS.between(startLocalDateTime, endLocalDateTime);
    }

    
    public static long diffHours(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
        // 计算相差时数
        return ChronoUnit.HOURS.between(startLocalDateTime, endLocalDateTime);
    }

    
    public static long diffDays(Date startDate, Date endDate) {
        // Date转成LocalDate
        LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // Math.abs(startLocalDate.toEpochDay() - endLocalDate.toEpochDay()); // 转换成绝对值(不出现负数)
        //return startLocalDate.toEpochDay() - endLocalDate.toEpochDay();
        // 计算相差天数
        return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
    }

    
    public static long diffDays(LocalDate startLocalDate, LocalDate endLocalDate) {
        // 计算相差天数
        return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
    }

    
    public static long diffDays(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
        // 两种方式计算相差天数,精确至时分秒计算,与LocalDate有实质性的区别,LocalDate忽略时分秒的差异
        Duration duration = Duration.between(startLocalDateTime, endLocalDateTime);
        return duration.toDays();
        // 计算相差天数
        //return ChronoUnit.DAYS.between(startLocalDateTime, endLocalDateTime);
    }

    
    public static long diffMonths(Date startDate, Date endDate) {
        // Date转成LocalDate
        LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 计算相差月数
        return ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
    }

    
    public static long diffMonths(LocalDate startLocalDate, LocalDate endLocalDate) {
        // 计算相差月数
        return ChronoUnit.MONTHS.between(startLocalDate, endLocalDate);
    }

    
    public static long diffMonths(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
        // 计算相差月数
        return ChronoUnit.MONTHS.between(startLocalDateTime, endLocalDateTime);
    }
}

敬请期待,后续持续更新中…

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

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

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