【Java】DateTimeUtils(时间格式化工具类)

Java 时间格式化工具类

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public enum DateTimeUtils {

    /** 日期格式 <code>[yyyy-MM-dd]</code> */
    DATE("yyyy-MM-dd"),
    
    /** 日期格式 <code>[yyyyMMdd]</code> */
    DATE_COMPACT("yyyyMMdd"),
    
    /** 日期格式 <code>[yyyy_MM_dd]</code> */
    DATE_UNDERLINE("yyyy_MM_dd"),
    
    /** 时间格式 <code>[HH:mm:ss]</code> */
    TIME("HH:mm:ss"),
    
    /** 时间格式 <code>[HHmmss]</code> */
    TIME_COMPACT("HHmmss"),
    
    /** 时间格式 <code>[HH_mm_ss]</code> */
    TIME_UNDERLINE("HH_mm_ss"),
    
    /** 时间格式 <code>[HH:mm:ss.SSS]</code> */
    TIME_MILLI("HH:mm:ss.SSS"),
    
    /** 时间格式 <code>[HHmmssSSS]</code> */
    TIME_MILLI_COMPACT("HHmmssSSS"),
    
    /** 时间格式 <code>[HH_mm_ss_SSS]</code> */
    TIME_MILLI_UNDERLINE("HH_mm_ss_SSS"),
    
    /** 日期时间格式 <code>[yyyy-MM-dd HH:mm:ss]</code> */
    DATE_TIME("yyyy-MM-dd HH:mm:ss"),
    
    /** 日期时间格式 <code>[yyyyMMddHHmmss]</code> */
    DATE_TIME_COMPACT("yyyyMMddHHmmss"),
    
    /** 日期时间格式 <code>[yyyy_MM_dd_HH_mm_ss]</code> */
    DATE_TIME_UNDERLINE("yyyy_MM_dd_HH_mm_ss"),
    
    /** 日期时间格式 <code>[yyyy-MM-dd HH:mm:ss.SSS]</code> */
    DATE_TIME_MILLI("yyyy-MM-dd HH:mm:ss.SSS"),
    
    /** 日期时间格式 <code>[yyyyMMddHHmmssSSS]</code> */
    DATE_TIME_MILLI_COMPACT("yyyyMMddHHmmssSSS"),
    
    /** 日期时间格式 <code>[yyyy_MM_dd_HH_mm_ss_SSS]</code> */
    DATE_TIME_MILLI_UNDERLINE("yyyy_MM_dd_HH_mm_ss_SSS");
    

    // --------------------------------------------------------------------------------------------

    
    private final String pattern;
    private final DateTimeFormatter formatter;
    
    private DateTimeUtils(String pattern) {
        this.pattern = pattern;
        this.formatter = DateTimeFormatter.ofPattern(this.pattern)
                                          .withZone(ZoneId.systemDefault());
    }
    

    // --------------------------------------------------------------------------------------------

    /**
     * Formats a date-time object using this formatter. 
     * 
     * @return  the formatted string, not null
     */
    public String formatNow() {
        return formatter.format(Instant.now());
    }
    
    /**
     * Formats a date-time object using this formatter. 
     * 
     * @param epochMilli         the temporal object to format, not null
     * @return  the formatted string, not null
     */
    public String formatTimestamp(long epochMilli) {
        return formatter.format(Instant.ofEpochMilli(epochMilli));
    }
    
    /**
     * Formats a date-time object using this formatter. 
     * 
     * @param date       the temporal object to format, not null
     * @return  the formatted string, not null
     */
    public String formatDate(Date date) {
        return formatter.format(date.toInstant());
    }
    
    /**
     * Formats a date-time object using this formatter. 
     * 
     * @param instant        the temporal object to format, not null
     * @return  the formatted string, not null
     */
    public String formatInstant(Instant instant) {
        return formatter.format(instant);
    }
    
    /**
     * Formats a date-time object using this formatter. 
     * 
     * @param localDate      the temporal object to format, not null
     * @return  the formatted string, not null
     */
    public String formatLocalDate(LocalDate localDate) {
        return formatter.format(localDate);
    }
    
    /**
     * Formats a date-time object using this formatter. 
     * 
     * @param localTime      the temporal object to format, not null
     * @return  the formatted string, not null
     */
    public String formatLocalTime(LocalTime localTime) {
        return formatter.format(localTime);
    }
    
    /**
     * Formats a date-time object using this formatter. 
     * 
     * @param localDateTime      the temporal object to format, not null
     * @return  the formatted string, not null
     */
    public String formatLocalDateTime(LocalDateTime localDateTime) {
        return formatter.format(localDateTime);
    }
    

    // --------------------------------------------------------------------------------------------

    
    /**
     * Fully parses the text producing an object of the specified type.
     * 
     * @param date       the text to parse, not null
     * @return  the parsed time-stamp
     */
    public long asTimestamp(String date) {
        return formatter.parse(date, Instant::from).toEpochMilli();
    }
    
    /**
     * Fully parses the text producing an object of the specified type.
     * 
     * @param date       the text to parse, not null
     * @return  the parsed date-time, not null
     */
    public Date parseDate(String date) {
        return Date.from(formatter.parse(date, Instant::from));
    }
    
    /**
     * Fully parses the text producing an object of the specified type.
     * 
     * @param instant        the text to parse, not null
     * @return  the parsed date-time, not null
     */
    public Instant parseInstant(String instant) {
        return formatter.parse(instant, Instant::from);
    }
    
    /**
     * Fully parses the text producing an object of the specified type.
     * 
     * @param localDate      the text to parse, not null
     * @return  the parsed date-time, not null
     */
    public LocalDate parseLocalDate(String localDate) {
        return formatter.parse(localDate, LocalDate::from);
    }
    
    /**
     * Fully parses the text producing an object of the specified type.
     * 
     * @param localTime      the text to parse, not null
     * @return  the parsed date-time, not null
     */
    public LocalTime parseLocalTime(String localTime) {
        return formatter.parse(localTime, LocalTime::from);
    }
    
    /**
     * Fully parses the text producing an object of the specified type.
     * 
     * @param localDateTime      the text to parse, not null
     * @return  the parsed date-time, not null
     */
    public LocalDateTime parseLocalDateTime(String localDateTime) {
        return formatter.parse(localDateTime, LocalDateTime::from);
    }
    

    // --------------------------------------------------------------------------------------------

    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of years added.
     * 
     * @param date      date of the String type
     * @param years     the years to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusYears(String date, long years) {
        return formatter.format(parseLocalDateTime(date).plusYears(years));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of months added.
     * 
     * @param date      date of the String type
     * @param months    the months to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusMonths(String date, long months) {
        return formatter.format(parseLocalDateTime(date).plusMonths(months));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of weeks added.
     * 
     * @param date      date of the String type
     * @param weeks     the weeks to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusWeeks(String date, long weeks) {
        return formatter.format(parseLocalDateTime(date).plusWeeks(weeks));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of days added.
     * 
     * @param date      date of the String type
     * @param days      the days to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusDays(String date, long days) {
        return formatter.format(parseLocalDateTime(date).plusDays(days));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of hours added.
     * 
     * @param date      date of the String type
     * @param hours     the hours to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusHours(String date, long hours) {
        return formatter.format(parseLocalDateTime(date).plusHours(hours));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of minutes added.
     * 
     * @param date      date of the String type
     * @param minutes   the minutes to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusMinutes(String date, long minutes) {
        return formatter.format(parseLocalDateTime(date).plusMinutes(minutes));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of seconds added.
     * 
     * @param date      date of the String type
     * @param seconds   the seconds to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusSeconds(String date, long seconds) {
        return formatter.format(parseLocalDateTime(date).plusSeconds(seconds));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of milliseconds added.
     * 
     * @param date          date of the String type
     * @param milliseconds  the milliseconds to add, may be negative
     * @return  based on this date-time with the years added, not null
     */
    public String plusMilliseconds(String date, long milliseconds) {
        return formatter.format(parseLocalDateTime(date).plusNanos(milliseconds * 1000000L));
    }
    

    // --------------------------------------------------------------------------------------------

    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of years subtracted.
     * 
     * @param date      date of the String type
     * @param years     the years to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusYears(String date, long years) {
        return formatter.format(parseLocalDateTime(date).minusYears(years));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of months subtracted.
     * 
     * @param date      date of the String type
     * @param months    the months to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusMonths(String date, long months) {
        return formatter.format(parseLocalDateTime(date).minusMonths(months));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of weeks subtracted.
     * 
     * @param date      date of the String type
     * @param weeks     the weeks to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusWeeks(String date, long weeks) {
        return formatter.format(parseLocalDateTime(date).minusWeeks(weeks));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of days subtracted.
     * 
     * @param date      date of the String type
     * @param days      the days to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusDays(String date, long days) {
        return formatter.format(parseLocalDateTime(date).minusDays(days));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of hours subtracted.
     * 
     * @param date      date of the String type
     * @param hours     the hours to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusHours(String date, long hours) {
        return formatter.format(parseLocalDateTime(date).minusHours(hours));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of minutes subtracted.
     * 
     * @param date      date of the String type
     * @param minutes   the minutes to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusMinutes(String date, long minutes) {
        return formatter.format(parseLocalDateTime(date).minusMinutes(minutes));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of seconds subtracted.
     * 
     * @param date      date of the String type
     * @param seconds   the seconds to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusSeconds(String date, long seconds) {
        return formatter.format(parseLocalDateTime(date).minusSeconds(seconds));
    }
    
    /**
     * Returns a copy of this <tt>date</tt> with the specified number of milliseconds subtracted.
     * 
     * @param date          date of the String type
     * @param milliseconds  the milliseconds to subtract, may be negative
     * @return  based on this date-time with the years subtracted, not null
     */
    public String minusMilliseconds(String date, long milliseconds) {
        return formatter.format(parseLocalDateTime(date).minusNanos(milliseconds * 1000000L));
    }
    

    // --------------------------------------------------------------------------------------------

    
    /**
     * Change the format of the date display
     * 
     * @param date      date of the String type
     * @param pattern   the format of the date display
     * @return
     */
    public String transform(String date, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(parseLocalDateTime(date));
    }
    
}

猜你喜欢

转载自blog.csdn.net/zte1055889498/article/details/88531934