All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mogudiandian.util.date.LocalDateUtils Maven / Gradle / Ivy

package com.mogudiandian.util.date;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * JDK8 LocalDate的工具类
 *
 * @author Joshua Sun
 * @since 1.0.1
 */
public final class LocalDateUtils {

    private LocalDateUtils() {}

    /**
     * Date -> LocalDate
     * @param date 老日期
     * @return 新日期
     */
    public static LocalDate convertToLocalDate(Date date) {
        return date.toInstant()
                   .atZone(ZoneId.systemDefault())
                   .toLocalDate();
    }

    /**
     * Date -> LocalDateTime
     * @param date 老日期
     * @return 新日期
     */
    public static LocalDateTime convertToLocalDateTime(Date date) {
        return date.toInstant()
                   .atZone(ZoneId.systemDefault())
                   .toLocalDateTime();
    }

    /**
     * LocalDate -> Date
     * @param date 新日期
     * @return 老日期
     */
    public static Date convert(LocalDate date) {
        return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * LocalDateTime -> Date
     * @param date 新日期
     * @return 老日期
     */
    public static Date convert(LocalDateTime date) {
        return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy