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

com.feingto.cloud.kit.DateKit Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
package com.feingto.cloud.kit;

import lombok.SneakyThrows;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期格式化工具类
 *
 * @author longfei
 */
public class DateKit {
    public static final String DATE_TIME_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE = "yyyy-MM-dd";
    public static final String TIME = "HH:mm:ss";
    private static ZoneId zone = ZoneId.systemDefault();

    public static String format(String date) {
        return format(date, DATE_TIME);
    }

    public static String format(Date date) {
        return format(date, DATE_TIME);
    }

    public static String format(String date, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.parse(date));
    }

    public static String format(Date date, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.ofInstant(date.toInstant(), zone));
    }

    public static String format(long milliSeconds, String pattern) {
        Date date = new Date();
        date.setTime(milliSeconds);
        return format(date, pattern);
    }

    public static String nowUTC() {
        return new DateTime(DateTimeZone.UTC).toString(DATE);
    }

    public static String now() {
        return format(new Date(), DATE_TIME);
    }

    public static String now(String pattern) {
        return format(new Date(), pattern);
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    public static String getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        String wk = "";
        switch (week) {
            case 1:
                wk = "星期天";
                break;
            case 2:
                wk = "星期一";
                break;
            case 3:
                wk = "星期二";
                break;
            case 4:
                wk = "星期三";
                break;
            case 5:
                wk = "星期四";
                break;
            case 6:
                wk = "星期五";
                break;
            case 7:
                wk = "星期六";
        }
        return wk;
    }

    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    public static long getMilliSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    public static Date parse(String date) {
        return Date.from(LocalDateTime.parse(date, DateTimeFormatter.ofPattern(DATE_TIME))
                .atZone(zone).toInstant());
    }

    @SneakyThrows
    public static Date parse(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }

    /**
     * 单位时间前的日期
     */
    public static String getBeforeOf(String date, int count, int unit) {
        return getBeforeOf(date, DATE_TIME, count, unit);
    }

    /**
     * 单位时间前的日期
     */
    public static String getBeforeOf(String date, String pattern, int count, int unit) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(date, pattern));
        cl.add(unit, -count);
        return format(cl.getTime());
    }

    /**
     * 单位时间后的日期
     */
    public static String getAfterOf(String date, int count, int unit) {
        return getAfterOf(date, DATE_TIME, count, unit);
    }

    /**
     * 单位时间后的日期
     */
    public static String getAfterOf(String date, String pattern, int count, int unit) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(parse(date, pattern));
        cl.add(unit, count);
        return format(cl.getTime());
    }

    /**
     * 单位时间前的日期
     */
    public static Date getBeforeOf(Date date, int count, int unit) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(unit, -count);
        return cl.getTime();
    }

    /**
     * 单位时间后的日期
     */
    public static Date getAfterOf(Date date, int count, int unit) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(unit, count);
        return cl.getTime();
    }

    public static Date getFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    public static Date getFirstDayOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        return calendar.getTime();
    }

    public static Date getFirstDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getTime();
    }

    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int week = calendar.get(Calendar.WEEK_OF_MONTH);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        return calendar.getTime();
    }

    public static Date getFirstDayOfWeek(int year, int month, int week) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.DAY_OF_WEEK, 2);
        return calendar.getTime();
    }

    public static Date getLastDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }

    public static Date getLastDayOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return calendar.getTime();
    }

    public static Date getLastDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return calendar.getTime();
    }

    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int week = calendar.get(Calendar.WEEK_OF_MONTH);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
        return calendar.getTime();
    }

    public static Date getLastDayOfWeek(int year, int month, int week) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
        return calendar.getTime();
    }

    public static Date calcOfYear(Date date, int years) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);
        return calendar.getTime();
    }

    public static Date calcOfMonth(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }

    public static Date calcOfDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    public static Date calcOfWeek(Date date, int weeks) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.WEEK_OF_MONTH, weeks);
        return calendar.getTime();
    }

    public static Date calcOfHour(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hours);
        return calendar.getTime();
    }

    public static Date calcOfMinute(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minutes);
        return calendar.getTime();
    }

    public static Date calcOfSecond(Date date, int seconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, seconds);
        return calendar.getTime();
    }

    public static Date calcOfMilliSecond(Date date, int milliSeconds) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MILLISECOND, milliSeconds);
        return calendar.getTime();
    }

    public static Date compareOfMax(Date date1, Date date2) {
        return date1.getTime() > date2.getTime() ? date1 : date2;
    }

    public static Date compareOfMin(Date date1, Date date2) {
        return date1.getTime() < date2.getTime() ? date1 : date2;
    }

    public static int compareOfDiffYear(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(date1);
        calendar2.setTime(date2);
        return Math.abs(calendar1.get(Calendar.YEAR) - calendar2.get(Calendar.YEAR));
    }

    public static int compareOfDiffMonth(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(date1);
        calendar2.setTime(date2);
        return compareOfDiffYear(date1, date2) * 12 + Math.abs(calendar1.get(Calendar.MONTH) - calendar2.get(Calendar.MONTH));
    }

    public static int compareOfDiffDay(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(date1);
        calendar2.setTime(date2);
        return Math.abs(calendar1.get(Calendar.DATE) - calendar2.get(Calendar.DATE));
    }

    public static long compareOfDiff(Date date1, Date date2) {
        return date1.getTime() - date2.getTime();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy