时间转换工具类

package org.fas.util;

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;

/**
 * 时间转换工具类
 * @author Leif Liu
 */
public class TimeConversionUtil {
    private TimeConversionUtil() {}

    /**
     * 01. java.util.Date >> java.time.LocalDateTime
     *
     * @param date java.util.Date
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 02. java.util.Date >> java.time.LocalDate
     * @param date java.util.Date
     * @return java.time.LocalDate
     */
    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 03. java.util.Date >> java.time.LocalTime
     * @param date java.util.Date
     * @return java.time.LocalTime
     */
    public static LocalTime dateToLocalTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }

    /**
     * 04. java.time.LocalDateTime >> java.util.Date
     * @param localDateTime java.time.LocalDateTime
     * @return java.util.Date
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * java.time.LocalDate >> java.util.Date
     * @param localDate java.time.LocalDate
     * @return java.util.Date
     */
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    // 06. java.time.LocalTime >> java.util.Date

    /**
     * 06. java.time.LocalTime >> java.util.Date
     * @param localTime java.time.LocalTime
     * @return java.util.Date
     */
    public static Date localTimeToDate(LocalTime localTime) {
        return Date.from(LocalDateTime.of(LocalDate.now(), localTime).atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 07. java.time.LocalDateTime >> java.lang.String
     * @param localDateTime java.time.LocalDateTime
     * @param format yyyyMMddHHmmssSSS
     * @return java.lang.String
     */
    public static String localDateTimeToString(LocalDateTime localDateTime, String format) {
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 08. java.lang.String >> java.time.LocalDateTime
     * @param time java.lang.String
     * @param format yyyyMMddHHmmssSSS
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime stringToLocalDateTime(String time, String format) {
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format));
    }

    /**
     * 09. java.time.LocalDateTime >> java.lang.Long
     * @param localDateTime java.time.LocalDateTime
     * @return java.lang.Long
     */
    public static long localDateTimeToLong(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 10. java.lang.Long >> java.time.LocalDateTime
     * @param timestamp java.lang.Long
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime longToLocalDateTime(long timestamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
    }
}

你可能感兴趣的:(时间转换工具类)