日期操作工具类

/**
 * 时间操作工具类
 *
 * @author codeyi
 * @create 2017-08-08
 **/
public class DateUtils {

    /**
     * 是否是今天
     *
     * @param date
     * @return
     */
    public static boolean isToday(final Date date) {
        return isTheDay(date, DateUtils.now());
    }

    /**
     * 是否是指定日期
     *
     * @param date
     * @param day
     * @return
     */
    public static boolean isTheDay(final Date date, final Date day) {
        return date.getTime() >= DateUtils.dayBegin(day).getTime()
                && date.getTime() <= DateUtils.dayEnd(day).getTime();
    }

    /**
     * 获取指定时间的那天 00:00:00.000 的时间
     *
     * @param date
     * @return
     */
    public static Date dayBegin(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取指定时间的那天 23:59:59.999 的时间
     *
     * @param date
     * @return
     */
    public static Date dayEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    /**
     * 获取指定时间所在月初 0000-01-01 00:00:00.000 的时间
     *
     * @param date
     * @return
     */
    public static Date monthBegin(final Date date) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
//将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
//将分钟至0
        c.set(Calendar.MINUTE, 0);
//将秒至0
        c.set(Calendar.SECOND, 0);
//将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取指定时间所在月末 0000-01-31 23:59:59.999 的时间
     *
     * @param date
     * @return
     */
    public static Date monthEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
//将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
//将分钟至0
        c.set(Calendar.MINUTE, 0);
//将秒至0
        c.set(Calendar.SECOND, 0);
//将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        //将当前月加1;
        c.add(Calendar.MONTH, 1);
        //在当前月的下一月基础上减去1毫秒
        c.add(Calendar.MILLISECOND, -1);
        return c.getTime();
    }

    /**
     * 获取指定时间的年初 0000-01-01 00:00:00:000 的时间
     *
     * @param date
     * @return
     */
    public static Date yearBegin(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 01);
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        c.set(Calendar.MILLISECOND, 000);
        return c.getTime();
    }

    /**
     * 获取指定时间的年末 0000-12-31 23:59:59.999 的时间
     *
     * @param date
     * @return
     */
    public static Date yearEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR,1);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 01);
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        c.set(Calendar.MILLISECOND, 000);
        //在当前月的下一月基础上减去1毫秒
        c.add(Calendar.MILLISECOND, -1);
        return c.getTime();
    }

    /**
     * Timestamp转Date
     *
     * @param timestamp
     * @return
     */
    public static java.sql.Date convertToTimestamp(Timestamp timestamp) {
        if (timestamp == null)
            return null;
        return new java.sql.Date(timestamp.getTime());
    }

    /**
     * Date转imestamp
     *
     * @param date
     * @return
     */
    public static Timestamp converBySqlDate(java.sql.Date date) {
        if (date == null)
            return null;
        return new Timestamp(date.getTime());
    }

    /**
     * util.Date转imestamp
     *
     * @param date
     * @return
     */
    public static Timestamp converByUtilDate(java.util.Date date) {
        if (date == null) {
            return null;
        }
        return new Timestamp(date.getTime());
    }

    /**
     * 将String字符串转换为java.sql.Timestamp格式日期,用于数据库保存
     *
     * @param strDate    表示日期的字符串
     * @param dateFormat 传入字符串的日期表示格式(如:"yyyy-MM-dd HH:mm:ss")
     * @return java.sql.Timestamp类型日期对象(如果转换失败则返回null)
     */
    public static java.sql.Timestamp strToSqlDate(String strDate, String dateFormat) {
        SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
        java.util.Date date = null;
        try {
            date = sf.parse(strDate);
        } catch (ParseException e) {
//            e.printStackTrace();
        }
        java.sql.Timestamp dateSQL = new java.sql.Timestamp(date.getTime());
        return dateSQL;
    }


    /**
     * 获取前一天
     *
     * @param date
     * @return
     */
    public static java.util.Date getBeforeDay(java.util.Date date, int beforeCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 0 - beforeCount);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取前一天
     *
     * @param timestamp
     * @return
     */
    public static Timestamp getBeforeDay(Timestamp timestamp, int beforeCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        calendar.add(Calendar.DAY_OF_MONTH, 0 - beforeCount);
        java.util.Date date = calendar.getTime();
        return converByUtilDate(date);
    }

    /**
     * 当前时间对应日期
     *
     * @param date
     * @return
     */
    public static java.util.Date getNowDay(java.util.Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取后一天
     *
     * @param date
     * @return
     */
    public static java.util.Date getNextDay(java.util.Date date, int nextCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, nextCount);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取后一天
     *
     * @param timestamp
     * @return
     */
    public static Timestamp getNextDay(Timestamp timestamp, int nextCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        calendar.add(Calendar.DAY_OF_MONTH, nextCount);
        java.util.Date date = calendar.getTime();
        return converByUtilDate(date);
    }

    /**
     * 格式化日期返回对文本
     *
     * @param timestamp 日期
     * @param formatStr 格式化类型
     * @return
     */
    public static String formatData(Timestamp timestamp, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(timestamp.getTime());
    }

    /**
     * 格式化日期返回对文本
     *
     * @param date      日期
     * @param formatStr 格式化类型
     * @return
     */
    public static String formatData(java.util.Date date, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date.getTime());
    }

    /**
     * 判断选择的日期是否是本周
     *
     * @param time
     * @return
     */
    public static boolean isThisWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new java.sql.Date(time));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if (paramWeek == currentWeek) {
            return true;
        }
        return false;
    }

    /**
     * 判断选择的日期是否是今天
     *
     * @param time
     * @return
     */
    public static boolean isToday(long time) {
        return isThisTime(time, "yyyy-MM-dd");
    }

    /**
     * 判断选择的日期是否是本月
     *
     * @param time
     * @return
     */
    public static boolean isThisMonth(long time) {
        return isThisTime(time, "yyyy-MM");
    }

    /**
     * 获取当前日期的起始时间
     * @param date
     * @return
     */
    public static java.util.Date getDayStarDate(java.sql.Date date) throws ParseException {
        String day = formatData(date, "yyyy-MM-dd")+ " 00:00:00";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        return sdf.parse(day);
    }

    /**
     * 获取当前日期的截止时间
     * @param date
     * @return
     */
    public static java.util.Date getDayEndDate(java.sql.Date date) throws ParseException {
        String day = formatData(date, "yyyy-MM-dd")+ " 23:59:59";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        return sdf.parse(day);
    }
    /**
     * 获取当前日期的起始时间
     * @param date
     * @return
     */
    public static java.util.Date getDayStarDate(java.util.Date date) throws ParseException {
        String day = formatData(date, "yyyy-MM-dd")+ " 00:00:00";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        return sdf.parse(day);
    }

    /**
     * 获取当前日期的截止时间
     * @param date
     * @return
     */
    public static java.util.Date getDayEndDate(java.util.Date date) throws ParseException {
        String day = formatData(date, "yyyy-MM-dd")+ " 23:59:59";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        return sdf.parse(day);
    }

    private static boolean isThisTime(long time, String pattern) {
        java.sql.Date date = new java.sql.Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        //参数时间
        String param = sdf.format(date);
        //当前时间
        String now = sdf.format(new java.util.Date());
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    public static Date now() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        System.out.println("是否是今天:" + isToday(now) + "");
        System.out.println("获取指定时间的那天 00:00:00.000 的时间:" + sdf.format(dayBegin(now)) + "");
        System.out.println("获取指定时间的那天 23:59:59.999 的时间:" + sdf.format(dayEnd(now)) + "");
        System.out.println("获取指定时间所在月初 0000-01-01 00:00:00.000 的时间:" + sdf.format(monthBegin(now)) + "");
        System.out.println("获取指定时间所在月末 0000-01-31 23:59:59.999 的时间:" + sdf.format(monthEnd(now)) + "");
        System.out.println("获取指定时间的年初 0000-01-01 00:00:00:000 的时间:" + sdf.format(yearBegin(now)) + "");
        System.out.println("获取指定时间的年末 0000-12-31 23:59:59.999 的时间:" + sdf.format(yearEnd(now)) + "");
    }
}

你可能感兴趣的:(Java,工具类,Date,时间,日期,Utils)