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

com.gitee.summer9102.develop.common.util.DateFormatUtil Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.gitee.summer9102.develop.common.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class DateFormatUtil {

    private static DateTimeFormatter dateTimeFormatter;
    public static final String dateTimeStr = "yyyy-MM-dd HH:mm:ss";
    // 中国时区+8
    public static final ZoneOffset CHINA = ZoneOffset.ofHours(8);

    static {
        dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeStr);
    }

    public static DateTimeFormatter getDateTimeFormatter() {
        if (dateTimeFormatter == null) {
            dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeStr);
        }
        return dateTimeFormatter;
    }

    public static String format(LocalDate date) {
        return date.toString();
    }

    public static String format(LocalDateTime dateTime) {
        return dateTime.format(getDateTimeFormatter());
    }

    public static String format(Instant instant) {
        return format(toLocalDateTime(instant));
    }

    public static String format(long epochSecond) {
        return format(toLocalDateTime(epochSecond));
    }

    public static LocalDate toLocalDate(String date) {
        return LocalDate.parse(date);
    }

    public static LocalDate toLocalDate(LocalDateTime dateTime) {
        return dateTime.toLocalDate();
    }

    public static LocalDate toLocalDate(Instant instant) {
        return toLocalDate(toLocalDateTime(instant));
    }

    public static LocalDate toLocalDate(long epochSecond) {
        return toLocalDate(toLocalDateTime(epochSecond));
    }

    public static LocalDateTime toLocalDateTime(String dateTime) {
        return LocalDateTime.parse(dateTime, getDateTimeFormatter());
    }

    public static LocalDateTime toLocalDateTime(LocalDate date) {
        return date.atStartOfDay();
    }

    public static LocalDateTime toLocalDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, CHINA);
    }

    public static LocalDateTime toLocalDateTime(long epochSecond) {
        return LocalDateTime.ofEpochSecond(epochSecond, 0, CHINA);
    }

    public static Instant toInstant(LocalDate localDate) {
        return toInstant(toLocalDateTime(localDate));
    }

    public static Instant toInstant(LocalDateTime localDateTime) {
        return localDateTime.toInstant(CHINA);
    }

    public static Instant toInstant(long epochSecond) {
        return Instant.ofEpochSecond(epochSecond);
    }

    public static long toEpochSecond(LocalDate localDate) {
        return toEpochSecond(toLocalDateTime(localDate));
    }

    public static long toEpochSecond(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(CHINA);
    }

    public static long toEpochSecond(Instant instant) {
        return instant.getEpochSecond();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy