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

io.github.xuyao5.dkl.eskits.util.DateUtilsPlus Maven / Gradle / Ivy

There is a newer version: 0.14
Show newest version
package io.github.xuyao5.dkl.eskits.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;

import static org.apache.commons.lang3.time.DateUtils.parseDate;

/**
 * @author Thomas.XU(xuyao)
 * @version 5/07/20 17:10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DateUtilsPlus {

    public static final FastDateFormat STD_DATETIME_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    public static final FastDateFormat STD_DATE_FORMAT = FastDateFormat.getInstance("yyyyMMdd");

    public static Date now() {
        return new Date();
    }

    public static String format2Date(@NonNull FastDateFormat format) {
        return format2Date(now(), format);
    }

    public static String format2Date(@NonNull Date date, @NonNull FastDateFormat format) {
        return DateFormatUtils.format(date, format.getPattern(), Locale.ROOT);
    }

    @SneakyThrows
    public static Date parse2Date(@NonNull FastDateFormat format) {
        return parse2Date(format2Date(format), format);
    }

    @SneakyThrows
    public static Date parse2Date(@NonNull String date, @NonNull FastDateFormat format) {
        return parseDate(date, Locale.ROOT, format.getPattern());
    }

    public static Date parse2Date(long date) {
        return new Date(date);
    }

    public static long daysBetween(String dateTag) {
        return ChronoUnit.DAYS.between(LocalDate.parse(dateTag, DateTimeFormatter.BASIC_ISO_DATE), LocalDate.now());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy