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

com.yixan.tools.common.util.DateUtil Maven / Gradle / Ivy

There is a newer version: 3.7.1
Show newest version
package com.yixan.tools.common.util;

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

/**
 * 日期工具
 *
 * @author zhaohuihua
 * @version V1.0 2015年10月23日
 */
public class DateUtil {

    /** 一秒有多少毫秒 **/
    public static final long RATE_SECOND = 1000;
    /** 一分钟有多少毫秒 **/
    public static final long RATE_MINUTE = 60 * RATE_SECOND;
    /** 一小时有多少毫秒 **/
    public static final long RATE_HOUR = 60 * RATE_MINUTE;
    /** 一天有多少毫秒 **/
    public static final long RATE_DAY = 24 * RATE_HOUR;

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

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

    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

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

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

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

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

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

    public static Date setMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, month);
        return calendar.getTime();
    }

    public static Date setDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    public static Date setHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, hour);
        return calendar.getTime();
    }

    public static Date setMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    public static Date setSecond(Date date, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }

    public static Date setMillisecond(Date date, int millisecond) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MILLISECOND, millisecond);
        return calendar.getTime();
    }

    /**
     * 将日期格式化为字符串
     *
     * @param date 待处理的日期
     * @param pattern 日期格式
     * @return 日期字符串
     */
    public static String format(Date date, String pattern) {
        if (pattern == null) {
            throw new NullPointerException();
        }
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 转换为标准的字符串, 如 2012-08-08 20:00:00.000
     *
     * @param date 待处理的日期
     * @return 日期字符串
     */
    public static String toNormativeString(Date date) {
        return toNormativeString(date, false);
    }

    /**
     * 转换为标准的字符串, 如 2012-08-08 20:00:00.000
     *
     * @param date 待处理的日期
     * @param compact 紧凑格式, 纯数字没有分隔符
     * @return 日期字符串
     */
    public static String toNormativeString(Date date, boolean compact) {
        if (date == null) {
            return null;
        }
        String fmt = compact ? "yyyyMMddHHmmssSSS" : "yyyy-MM-dd HH:mm:ss.SSS";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(date);
    }

    /**
     * 转换为日期字符串, 如 2012-08-08
     *
     * @param date 待处理的日期
     * @return 日期字符串
     */
    public static String toDateString(Date date) {
        return toDateString(date, false);
    }

    /**
     * 转换为日期字符串, 如 2012-08-08
     *
     * @param date 待处理的日期
     * @param compact 紧凑格式, 纯数字没有分隔符
     * @return 日期字符串
     */
    public static String toDateString(Date date, boolean compact) {
        if (date == null) {
            return null;
        }
        String fmt = compact ? "yyyyMMdd" : "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(date);
    }

    /**
     * 转换为时间字符串, 如 20:00:00.000
     *
     * @param date 待处理的日期
     * @return 时间字符串
     */
    public static String toTimeString(Date date) {
        return toTimeString(date, false);
    }

    /**
     * 转换为时间字符串, 如 20:00:00.000
     *
     * @param date 待处理的日期
     * @param compact 紧凑格式, 纯数字没有分隔符
     * @return 时间字符串
     */
    public static String toTimeString(Date date, boolean compact) {
        if (date == null) {
            return null;
        }
        String fmt = compact ? "HHmmss" : "HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(date);
    }

    /**
     * 转换为日期加时间字符串, 如 2012-08-08 20:00:00
     *
     * @param date 待处理的日期
     * @return 日期字符串
     */
    public static String toDateTimeString(Date date) {
        return toDateTimeString(date, false);
    }

    /**
     * 转换为日期加时间字符串, 如 2012-08-08 20:00:00
     *
     * @param date 待处理的日期
     * @param compact 紧凑格式, 纯数字没有分隔符
     * @return 日期字符串
     */
    public static String toDateTimeString(Date date, boolean compact) {
        if (date == null) {
            return null;
        }
        String fmt = compact ? "yyyyMMddHHmmss" : "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(date);
    }

    /**
     * 转换为第1时间
* Calendar.YEAR=当年第1时间, Calendar.MONTH=当月第1时间, Calendar.DAY_OF_MONTH=当日第1时间, ...
* 如 toFirstTime(2016-08-08 20:30:40.500, Calendar.YEAR) --- 2016-01-01 00:00:00.000
* 如 toFirstTime(2016-08-08 20:30:40.500, Calendar.MONTH) --- 2016-08-01 00:00:00.000
* 如 toFirstTime(2016-08-08 20:30:40.500, Calendar.DAY_OF_MONTH) --- 2016-08-08 00:00:00.000
* 如 toFirstTime(2016-08-08 20:30:40.500, Calendar.HOUR_OF_DAY) --- 2016-08-08 20:00:00.000
* 如 toFirstTime(2016-08-08 20:30:40.500, Calendar.MINUTE) --- 2016-08-08 20:30:00.000
* 如 toFirstTime(2016-08-08 20:30:40.500, Calendar.SECOND) --- 2016-08-08 20:30:40.000
* * @param date 待处理的日期 * @param field 类型 * @return 第1时间 */ public static Date toFirstTime(Date date, int field) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); switch (field) { case Calendar.YEAR: calendar.set(Calendar.MONTH, Calendar.JANUARY); case Calendar.MONTH: calendar.set(Calendar.DAY_OF_MONTH, 1); case Calendar.DAY_OF_MONTH: calendar.set(Calendar.HOUR_OF_DAY, 0); case Calendar.HOUR_OF_DAY: calendar.set(Calendar.MINUTE, 0); case Calendar.MINUTE: calendar.set(Calendar.SECOND, 0); case Calendar.SECOND: calendar.set(Calendar.MILLISECOND, 0); } return calendar.getTime(); } /** * 转换为最后时间
* Calendar.YEAR=当年最后时间, Calendar.MONTH=当月最后时间, Calendar.DAY_OF_MONTH=当日最后时间, ...
* 如 toLastTime(2016-08-08 20:30:40.500, Calendar.YEAR) --- 2016-12-31 23:59:59.999
* 如 toLastTime(2016-08-08 20:30:40.500, Calendar.MONTH) --- 2016-08-31 23:59:59.999
* 如 toLastTime(2016-08-08 20:30:40.500, Calendar.DAY_OF_MONTH) --- 2016-08-08 23:59:59.999
* 如 toLastTime(2016-08-08 20:30:40.500, Calendar.HOUR_OF_DAY) --- 2016-08-08 20:59:59.999
* 如 toLastTime(2016-08-08 20:30:40.500, Calendar.MINUTE) --- 2016-08-08 20:30:59.999
* 如 toLastTime(2016-08-08 20:30:40.500, Calendar.SECOND) --- 2016-08-08 20:30:40.999
* * @param date 待处理的日期 * @param field 类型 * @return 最后时间 */ public static Date toLastTime(Date date, int field) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); switch (field) { case Calendar.YEAR: calendar.set(Calendar.MONTH, Calendar.DECEMBER); case Calendar.MONTH: // 下月1日的前一天 calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); case Calendar.DAY_OF_MONTH: calendar.set(Calendar.HOUR_OF_DAY, 23); case Calendar.HOUR_OF_DAY: calendar.set(Calendar.MINUTE, 59); case Calendar.MINUTE: calendar.set(Calendar.SECOND, 59); case Calendar.SECOND: calendar.set(Calendar.MILLISECOND, 999); } return calendar.getTime(); } /** * 转换为结束时间, 即设置时分秒为00:00:00 * * @param date 待处理的日期 * @return 结束时间 */ public static Date toStartTime(Date date) { if (date == null) { return null; } return toFirstTime(date, Calendar.DAY_OF_MONTH); } /** * 转换为结束时间, 即设置时分秒为23:59:59 * * @param date 待处理的日期 * @return 结束时间 */ public static Date toEndTime(Date date) { if (date == null) { return null; } return toLastTime(date, Calendar.DAY_OF_MONTH); } /** * 获取当天的毫秒数 * * @param date * @return */ public static long getMillisOfDay(Date date) { Date start = toStartTime(date); return date.getTime() - start.getTime(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy