java8 DateUtil工具


基於Jdk8日期工具

基本能覆蓋日常開發所需日期處理

package com.xx.dubbo.dubboservice.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.Locale;

/**
 * 日期工具
 * @description:
 * @author: gongchang
 * @time: 2020/5/6 9:43
 */
public class DateUtil {

    //數據庫格式的日期
    public static final String SQL_MONTH = "yyyy-MM";
    public static final String SQL_DATE = "yyyy-MM-dd";
    public static final String SQL_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String SQL_TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SS";

    //斜杠格式的日期
    public static final String DATE = "yyyy/MM/dd";
    public static final String TIMESTAMP = "yyyy/MM/dd HH:mm:ss.SS";
    public static final String TIMESTAMP_SHORT = "yyyy/MM/dd HH:mm";
    public static final String TIME = "HH:mm:ss";
    public static final String TIME_SHORT = "HH:mm";

    //不常用日期格式
    public static final String CHINESEDATE = "yyyy年MM月dd日";
    public static final String DATE_TIME = "yyyyMMddHHmmss";
    public static final String DATE_TIME_DETAIL = "yyyyMMddHHmmssSS";
    public static final String DATE_DAY = "yyyyMMdd";
    public static final String DATE_HOUR = "yyyyMMddHH";


    /**
     * 防止被實例化
     */
    private DateUtil(){

    }

    /**
     * Date轉LocalDateTime
     * 使用系統時區
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * LocalDateTime轉Date
     * 使用系統時區
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime){
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 日期轉字符串
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String dateTimeToStr(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    /**
     * 將字符串日期解析為java.time.LocalDateTime
     * @param dateTimeStr
     * @param pattern
     * @return
     */
    public static LocalDateTime strToLocalDateTime(String dateTimeStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(dateTimeStr, formatter);
    }

    /**
     * 開始日期,補齊" 00:00:00"
     *
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getStartDateTimeWithHMS(LocalDateTime localDateTime) {
        return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 結束日期,補齊" 23:59:59"
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getEndDateWithHMS(LocalDateTime localDateTime){
        return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);
    }



    public static LocalDateTime getAfterYears(LocalDateTime localDateTime, int count){
        return localDateTime.plusYears(count);
    }

    public static LocalDateTime getAfterMonths(LocalDateTime localDateTime, int count){
        return localDateTime.plusMonths(count);
    }

    public static LocalDateTime getAfterDays(LocalDateTime localDateTime, int count){
        return localDateTime.plusDays(count);
    }

    public static LocalDateTime getAfterMinutes(LocalDateTime localDateTime, int count){
        return localDateTime.plusMinutes(count);
    }

    public static LocalDateTime getAfterSeconds(LocalDateTime localDateTime, int count){
        return localDateTime.plusSeconds(count);
    }



    /**
     * 獲得當前年的第一天
     * @param
     * @return
     */
    public static LocalDateTime getYearFirstDay(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfYear());
    }

    /**
     * 獲得當前年的最后一天
     * @param
     * @return
     */
    public static LocalDateTime getYearLastDay(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfYear());
    }


    /**
     * 獲得當前月的第一天
     * @param
     * @return
     */
    public static LocalDateTime getMonthFirstDay(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 獲得當前月的最后一天
     * @param
     * @return
     */
    public static LocalDateTime getMonthLastDay(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth());
    }


    /**
     * 獲得當前星期的第一天
     * @param localDateTime
     * @param locale 默認Locale.CHINA 周日為一周的第一天
     * @return
     */
    public static LocalDateTime getWeekFirstDay(LocalDateTime localDateTime, Locale locale) {
        return localDateTime.with(WeekFields.of(locale==null?Locale.CHINA:locale).dayOfWeek(),1);
    }

    /**
     * 獲得當前星期的最后一天
     * @param localDateTime
     * @param locale 默認默認Locale.CHINA 周日為一周的第一天
     * @return
     */
    public static LocalDateTime getWeekLastDay(LocalDateTime localDateTime, Locale locale) {
        return localDateTime.with(WeekFields.of(locale==null?Locale.CHINA:locale).dayOfWeek(),7);
    }



    /**
     * 計算兩個日期之間相差年數
     * @param smallDateTime 較小的時間
     * @param bigDateTime  較大的時間
     * @return 相差年數
     */
    public static int getYearDiff(LocalDateTime smallDateTime, LocalDateTime bigDateTime) {
        return (int)smallDateTime.until(bigDateTime, ChronoUnit.YEARS);
    }

    /**
     * 計算兩個日期之間相差月數
     * @param smallDateTime 較小的時間
     * @param bigDateTime  較大的時間
     * @return 相差月數
     */
    public static int getMonthDiff(LocalDateTime smallDateTime, LocalDateTime bigDateTime) {
        return (int)smallDateTime.until(bigDateTime, ChronoUnit.MONTHS);
    }

    /**
     * 計算兩個日期之間相差的天數
     * @param smallDateTime 較小的時間
     * @param bigDateTime  較大的時間
     * @return 相差天數
     */
    public static int getDayDiff(LocalDateTime smallDateTime, LocalDateTime bigDateTime){
        return (int)smallDateTime.until(bigDateTime, ChronoUnit.DAYS);
    }

    /**
     * 計算兩個日期之間相差小時數
     * @param smallDateTime 較小的時間
     * @param bigDateTime  較大的時間
     * @return 相差小時數
     */
    public static int getHourDiff(LocalDateTime smallDateTime, LocalDateTime bigDateTime){
        return (int)smallDateTime.until(bigDateTime, ChronoUnit.HOURS);
    }

    /**
     * 計算兩個日期之間相差分鍾數
     * @param smallDateTime
     * @param bigDateTime
     * @return 相差分鍾數
     */
    public static int getMinutesDiff(LocalDateTime smallDateTime, LocalDateTime bigDateTime){
        return (int)smallDateTime.until(bigDateTime, ChronoUnit.MINUTES);
    }

    /**
     * 計算兩個日期之間相差秒數
     * @param smallDateTime
     * @param bigDateTime
     * @return 相差秒數
     */
    public static int getSecondsDiff(LocalDateTime smallDateTime, LocalDateTime bigDateTime){
        return (int)smallDateTime.until(bigDateTime, ChronoUnit.SECONDS);
    }


    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(getAfterMonths(now, 1));
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM