关于时间的工具类

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

/**
 * @author XuBaofeng.
 * @date 2018/7/27.
 */
public class DateUtil {

    private static Calendar calendar;

    // ========== 年的操作 ========== //

    /**
     * 根据时间戳获取前一年的时间
     *
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneYear(Long time) throws Exception {
        return getBeforeOneYear(new Timestamp(time));
    }

    /**
     * 根据时间获取前一年的时间
     *
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneYear(Timestamp time) throws Exception {
        Integer span = -1;
        return getTimeByDateAndYearSpan(time, span);
    }

    /**
     * 根据时间戳获取前一年的时间
     *
     * @return
     * @throws Exception
     */
    public static Long getAfterOneYear(Long time) throws Exception {
        return getAfterOneYear(new Timestamp(time));
    }

    /**
     * 根据时间获取前一年的时间
     *
     * @return
     * @throws Exception
     */
    public static Long getAfterOneYear(Timestamp time) throws Exception {
        Integer span = 1;
        return getTimeByDateAndYearSpan(time, span);
    }

    /**
     * 根据时间戳和年跨度获取时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndYearSpan(Long time, Integer span) throws Exception {
        return getTimeByDateAndYearSpan(new Timestamp(time), span);
    }

    /**
     * 根据时间和年跨度获取时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndYearSpan(Timestamp time, Integer span) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.YEAR, span);
        return calendar.getTimeInMillis();
    }


    /**
     * 根据时间戳获取本年第一天
     *
     * @return
     * @throws Exception
     */
    public static Long getYearStart(Long time) throws Exception {
        return getYearStart(new Timestamp(time));
    }

    /**
     * 根据时间获取本年第一天
     *
     * @return
     * @throws Exception
     */
    public static Long getYearStart(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 设置为本年第一天
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        // ===== 将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // ===== 将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        // ===== 将秒至0
        calendar.set(Calendar.SECOND, 0);
        // ===== 将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }


    /**
     * 根据时间戳获取本年最后一天
     *
     * @return
     * @throws Exception
     */
    public static Long getYearEnd(Long time) throws Exception {
        return getYearEnd(new Timestamp(time));
    }

    /**
     * 根据时间获取本年第最后一天
     *
     * @return
     * @throws Exception
     */
    public static Long getYearEnd(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ====== 设置为本年最后一天
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        // ====== 将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        // ====== 将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        // ====== 将秒至59
        calendar.set(Calendar.SECOND, 59);
        // ====== 将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        // ====== 获取本月最后一天的时间戳
        return calendar.getTimeInMillis();
    }

    // ========== 月的操作 ========== //

    /**
     * 根据时间戳获取当天日期的上一个月
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneMonth(Long time) throws Exception {
        return getBeforeOneMonth(new Timestamp(time));
    }

    /**
     * 根据时间获取当天日期的上一个月
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneMonth(Timestamp time) throws Exception {
        Integer span = -1;
        return getTimeByDateAndMonthSpan(time, span);
    }

    /**
     * 根据时间戳获取当天日期的下一个月
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneMonth(Long time) throws Exception {
        return getAfterOneMonth(new Timestamp(time));
    }

    /**
     * 根据时间获取当天日期的下一个月
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneMonth(Timestamp time) throws Exception {
        Integer span = 1;
        return getTimeByDateAndMonthSpan(time, span);
    }

    /**
     * 根据时间戳和月跨度获取时间戳
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndMonthSpan(Long time, Integer span) throws Exception {
        return getTimeByDateAndMonthSpan(new Timestamp(time), span);
    }

    /**
     * 根据时间和月跨度获取时间戳
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndMonthSpan(Timestamp time, Integer span) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MONTH, span);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定日期所在月份开始的时间戳
     *
     * @param time
     * @return
     */
    public static Long getMonthStart(Long time) throws Exception {
        return getMonthStart(new Timestamp(time));
    }

    /**
     * 获取指定日期所在月份开始的时间戳
     *
     * @param time
     * @return
     */
    public static Long getMonthStart(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        // ===== 将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // ===== 将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        // ===== 将秒至0
        calendar.set(Calendar.SECOND, 0);
        // ===== 将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        // ===== 获取本月第一天的时间戳
        return calendar.getTimeInMillis();
    }

    /**
     * 获取指定日期所在月份结束的时间戳
     *
     * @param time 指定日期
     * @return
     */
    public static Long getMonthEnd(Long time) throws Exception {
        return getMonthEnd(new Timestamp(time));
    }

    /**
     * 获取指定日期所在月份结束的时间戳
     *
     * @param time 指定日期
     * @return
     */
    public static Long getMonthEnd(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 设置为当月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        // ===== 将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        // ===== 将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        // ===== 将秒至59
        calendar.set(Calendar.SECOND, 59);
        // ===== 将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        // ===== 获取本月最后一天的时间戳
        return calendar.getTimeInMillis();
    }

    // ========== 周的操作 ========== //

    /**
     * 根据时间戳获取前一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneWeek(Long time) throws Exception {
        return getBeforeOneWeek(new Timestamp(time));
    }

    /**
     * 根据时间获取前一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneWeek(Timestamp time) throws Exception {
        Integer span = -1;
        return getTimeByDateAndWeekSpan(time, span);
    }

    /**
     * 根据时间戳获取后一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneWeek(Long time) throws Exception {
        return getAfterOneWeek(new Timestamp(time));
    }

    /**
     * 根据时间获取后一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneWeek(Timestamp time) throws Exception {
        Integer span = 1;
        return getTimeByDateAndWeekSpan(time, span);
    }

    /**
     * 根据时间戳和天数获取同一时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndWeekSpan(Long time, Integer span) throws Exception {
        return getTimeByDateAndWeekSpan(new Timestamp(time), span);
    }

    /**
     * 根据时间和天数获取同一时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndWeekSpan(Timestamp time, Integer span) throws Exception {
        return getTimeByDateAndDaySpan(time, span * 7);
    }

    /**
     * 根据时间戳获取一天之中最早时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getWeekStart(Long time) throws Exception {
        return getWeekStart(new Timestamp(time));
    }

    /**
     * 根据时间获取一天之中最早时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getWeekStart(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 设置为这周的第一天
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        // ===== 将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // ===== 将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        // ===== 将秒至0
        calendar.set(Calendar.SECOND, 0);
        // ===== 将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 根据时间获取一天之中最晚时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getWeekEnd(Long time) throws Exception {
        return getWeekEnd(new Timestamp(time));
    }

    /**
     * 根据时间获取一天之中最晚时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getWeekEnd(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 设置为这周的最后一天
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
        // ===== 将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        // ===== 将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        // ===== 将秒至59
        calendar.set(Calendar.SECOND, 59);
        // ===== 将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    // ========== 天的操作 ========== //

    /**
     * 根据时间戳获取前一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneDay(Long time) throws Exception {
        return getBeforeOneDay(new Timestamp(time));
    }

    /**
     * 根据时间获取前一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneDay(Timestamp time) throws Exception {
        Integer span = -1;
        return getTimeByDateAndDaySpan(time, span);
    }

    /**
     * 根据时间戳获取后一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneDay(Long time) throws Exception {
        return getAfterOneDay(new Timestamp(time));
    }

    /**
     * 根据时间获取后一天的同一时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneDay(Timestamp time) throws Exception {
        Integer span = 1;
        return getTimeByDateAndDaySpan(time, span);
    }

    /**
     * 根据时间戳和天数获取同一时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndDaySpan(Long time, Integer span) throws Exception {
        return getTimeByDateAndDaySpan(new Timestamp(time), span);
    }

    /**
     * 根据时间和天数获取同一时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndDaySpan(Timestamp time, Integer span) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.DATE, span);
        return calendar.getTimeInMillis();
    }

    /**
     * 根据时间戳获取一天之中最早时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getDayStart(Long time) throws Exception {
        return getDayStart(new Timestamp(time));
    }

    /**
     * 根据时间获取一天之中最早时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getDayStart(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // ===== 将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        // ===== 将秒至0
        calendar.set(Calendar.SECOND, 0);
        // ===== 将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 根据时间获取一天之中最晚时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getDayEnd(Long time) throws Exception {
        return getDayEnd(new Timestamp(time));
    }

    /**
     * 根据时间获取一天之中最晚时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getDayEnd(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        // ===== 将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        // ===== 将秒至59
        calendar.set(Calendar.SECOND, 59);
        // ===== 将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    // ========== 小时的操作 ========== //

    /**
     * 根据时间戳获取上个小时的时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneHour(Long time) throws Exception {
        return getBeforeOneHour(new Timestamp(time));
    }

    /**
     * 根据时间获取上个小时的时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getBeforeOneHour(Timestamp time) throws Exception {
        Integer span = -1;
        return getTimeByDateAndHourSpan(time, span);
    }

    /**
     * 根据时间戳获取下个小时的时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneHour(Long time) throws Exception {
        return getAfterOneHour(new Timestamp(time));
    }

    /**
     * 根据时间获取下个小时的时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getAfterOneHour(Timestamp time) throws Exception {
        Integer span = 1;
        return getTimeByDateAndHourSpan(time, span);
    }

    /**
     * 根据时间戳和小时跨度获取时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndHourSpan(Long time, Integer span) throws Exception {
        return getTimeByDateAndHourSpan(new Timestamp(time), span);
    }

    /**
     * 根据时间和小时跨度获取时间
     *
     * @param time
     * @param span
     * @return
     * @throws Exception
     */
    public static Long getTimeByDateAndHourSpan(Timestamp time, Integer span) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.HOUR, span);
        return calendar.getTimeInMillis();
    }


    /**
     * 根据时间戳获取一小时之中最早时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getHourStart(Long time) throws Exception {
        return getHourStart(new Timestamp(time));
    }

    /**
     * 根据时间获取一小时之中最早时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getHourStart(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        // ===== 将秒至0
        calendar.set(Calendar.SECOND, 0);
        // ===== 将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 根据时间获取一小时之中最晚时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getHourEnd(Long time) throws Exception {
        return getHourEnd(new Timestamp(time));
    }

    /**
     * 根据时间获取一小时之中最晚时间
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static Long getHourEnd(Timestamp time) throws Exception {
        calendar = Calendar.getInstance();
        calendar.setTime(time);
        // ===== 将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        // ===== 将秒至59
        calendar.set(Calendar.SECOND, 59);
        // ===== 将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 根据时间格式字符串获取时间戳
     *
     * @param time
     * @param type
     * @return
     * @throws Exception
     */
    public static Long getTimeByString(String time, String type) throws Exception {
        Date parse = new SimpleDateFormat(type).parse(time);
        return parse.getTime();
    }

    /**
     * 根据时间戳和格式获取时间字符串
     *
     * @param time
     * @param type
     * @return
     * @throws Exception
     */
    public static String getTimeByLong(Long time, String type) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        return sdf.format(new Date(time));
    }

}

  

原文地址:https://www.cnblogs.com/yanwu0527/p/9390729.html