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

com.github.xiaoyuge5201.date.DateUtils Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package com.github.xiaoyuge5201.date;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author xiaoyuge
 */
public class DateUtils {
    /**
     * 格式化时间按照某种格式
     *
     * @param date    日期
     * @param pattern 格式化类型
     * @return 格式化后的结果
     */
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            date = new Date(System.currentTimeMillis());
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        return DateFormatUtils.format(date, pattern);
    }

    /**
     * 格式化日期返回YYYY-MM-DD
     *
     * @param date 时间
     * @return 格式化的时间
     */
    public static String defaultFormatYMD(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    /**
     * 格式化当前时间
     *
     * @return YYYY-MM-DD
     */
    public static Date parseDateFormat() {
        SimpleDateFormat fo = new SimpleDateFormat();
        Date date = new Date(System.currentTimeMillis());
        fo.applyPattern("yyyy-MM-dd");
        try {
            date = fo.parse(DateFormatUtils.format(date, "yyyy-MM-dd"));
        } catch (Exception localException) {
        }
        return date;
    }

    /**
     * 查看某一天是星期几
     *
     * @param date 日期
     * @return 星期几
     */
    public static String getWeekDayToWeekName(Date date) {
        String[] dayNames = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar = Calendar.getInstance();
        if (date == null) {
            date = new Date();
        }
        calendar.setTime(date);
        int dayOfWeek = calendar.get(7);
        return dayNames[(dayOfWeek - 1)];
    }

    /**
     * 判断某个时间在两个时间之间
     *
     * @param srcDate   目标时间
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 判断结果
     */
    public static boolean between(Date srcDate, Date startDate, Date endDate) {
        if (startDate.after(srcDate)) {
            return false;
        }
        if (endDate.before(srcDate)) {
            return false;
        }
        return true;
    }

    /**
     * 某个时间到当天时间的天数差
     *
     * @param overTime 日期
     * @return 天数差
     */
    public static String marginTimeFormat(Date overTime) {
        String marginTimeFormat = "";
        Calendar overDate = Calendar.getInstance();
        overDate.setTime(overTime);
        Calendar nowDate = Calendar.getInstance();
        long seconds = (overDate.getTime().getTime() - nowDate.getTime().getTime()) / 1000L;
        if (seconds <= 0L) {
            return "过期";
        }
        long dayMargin = seconds / 86400L;

        seconds -= dayMargin * 86400L;
        long hourMargin = seconds / 3600L;

        seconds -= hourMargin * 3600L;
        long minuteMargin = seconds / 60L;

        seconds -= minuteMargin * 60L;
        if (dayMargin > 0L) {
            marginTimeFormat = dayMargin + "天";
            return marginTimeFormat;
        }
        if (hourMargin > 0L) {
            marginTimeFormat = hourMargin + "小时";
            return marginTimeFormat;
        }
        if (minuteMargin > 0L) {
            marginTimeFormat = minuteMargin + "分钟";
            return marginTimeFormat;
        }
        marginTimeFormat = "1分钟";
        return marginTimeFormat;
    }

    /**
     * 获取n个小时之后的时间
     *
     * @param date 日期
     * @param h    小时数
     * @return n个小时之后的时间
     */
    public static Date getDateByHours(Date date, int h) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, h);
        calendar.set(Calendar.MINUTE, 0);
        return calendar.getTime();
    }

    /**
     * 获得当天零时零分零秒
     *
     * @param date 时间
     * @return 日期的0点0分
     */
    public static Date initDateByDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 判断两个时间是否是同一个月
     *
     * @param date1 时间1
     * @param date2 时间2
     * @return 判断结果true/false
     */
    public static boolean isSameMonth(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        int year1 = cal.get(Calendar.YEAR);
        int month1 = cal.get(Calendar.MONTH);
        cal.setTime(date2);
        int year2 = cal.get(Calendar.YEAR);
        int month2 = cal.get(Calendar.MONTH);
        return (year1 == year2) && (month1 == month2);
    }

    /**
     * 获取两个时间相差的天数
     *
     * @param date1 时间1
     * @param date2 时间2
     * @return 相差的天数
     */
    public static int getDaysOfDifference(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        int d1 = cal.get(5);
        cal.setTime(date2);
        int d2 = cal.get(5);
        if (d2 > d1) {
            return d2 - d1 + 1;
        }
        return d1 - d2 + 1;
    }

    /**
     * 获取当前是几号
     *
     * @return 当前是几号
     */
    public static int getDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取某个月的总天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.LONG, 1);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.DATE, -1);
        return cal.get(Calendar.DATE);
    }

    /**
     * 获取当前月份的1号
     *
     * @return 当月1号的日期
     */
    public static String getFirstDayOfCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(5, 1);
        Date beginTime = cal.getTime();
        return datef.format(beginTime) + " 00:00:00";
    }

    /**
     * 获取下个月的一号
     *
     * @return 下个月一号的日期
     */
    public static String getNextMonthFirst() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(2, 1);
        lastDate.set(5, 1);
        return sdf.format(lastDate.getTime()) + " 00:00:00";
    }

    /**
     * 获取某些天后的时间
     *
     * @param step 天数
     * @return step天之后的日期
     */
    public static Date getNextDateByStep(int step) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, step);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
        return calendar.getTime();
    }

    /**
     * 获取n个月后的时间
     *
     * @param step n个月
     * @return 获取n个月后的时间
     */
    public static String getYearAndMonthByStep(int step) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, step);
        return format.format(cal.getTime());
    }

    /**
     * 判断两个时间的先后顺序
     *时间格式为yyyy-MM-dd hh:mm:ss
     * @param date1 时间1
     * @param date2 时间2
     * @return 比较结果 为1: date1 在date2之前,  结果为-1 表示date1在date2之后, 为0表示两个是同一天
     */
    public static int compareDateByYMDHMS(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    /**
     * 判断两个时间的先后顺序
     *时间格式为yyyy-MM-dd
     * @param date1 时间1
     * @param date2 时间2
     * @return 比较结果 为1: date1 在date2之前,  结果为-1 表示date1在date2之后, 为0表示两个是同一天
     */
    public static int compareDateByYMD(String date1, String date2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy