DateUtil


package com.zkml.common.util;

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

/**
 * 日期工具类
 *
 * @author :zhaojun on 2016-08-29 16:59
 */
public final class DateUtil {

    /**
     * 格式化格式 yyyy-MM-dd
     */
    public final static String DAY_FORMAT = "yyyy-MM-dd";

    /**
     * 格式化格式 yyyy-MM-dd HH:mm:ss
     */
    public final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 字符串转换成date类型
     *
     * @param str str
     * @return Date
     * @throws ParseException
     */
    public static Date stringToDate(String str, String dateFormat) {
        if (MyUtil.isBlank(str))
            return null;
        str = str.trim();
        SimpleDateFormat sdf = null;
        if (MyUtil.isNotBlank(dateFormat)) {
            sdf = new SimpleDateFormat(dateFormat);
        } else {
            if (str.length() > 19)
                str = str.substring(0, 19);
            //默认时间类型为 yyyy-MM-dd HH:mm:ss,根据字符长度来截取
            long index = str.length();
            if (index == 4) {
                //年
                sdf = new SimpleDateFormat("yyyy");
            } else if (index == 7) {
                //年月
                sdf = new SimpleDateFormat("yyyy-MM");
            } else if (index == 10) {
                //年月日
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            } else if (index == 13) {
                //年月日 时
                sdf = new SimpleDateFormat("yyyy-MM-dd HH");
            } else if (index == 16) {
                //年月日 时分
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            } else if (index == 19) {
                //年月日 时分秒
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
        }
        Date date = null;
        try {
            //严格遵守格式转换
            sdf.setLenient(false);
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 时间转字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String dateToString(Date date, String dateFormat) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = null;
        if (MyUtil.isNotBlank(dateFormat)) {
            sdf = new SimpleDateFormat(dateFormat);
        } else {
            sdf = new SimpleDateFormat(DATE_FORMAT);
        }
        //严格遵守格式转换
        sdf.setLenient(false);
        return sdf.format(date);

    }

    /**
     * 根据时间获取前一天的0点
     *
     * @param date
     * @param day  0当前时间 -1昨天 1明天
     * @return
     */
    public static Date getDayBeforeDateStart(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        date = calendar.getTime();
        return date;
    }

    /**
     * 根据时间获取前一天的23:59:59
     *
     * @param date
     * @param day  0当前时间 -1昨天 1明天
     * @return
     */
    public static Date getDayBeforeDateEnd(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        date = calendar.getTime();
        return date;
    }

    /**
     * 在给定日期上追加分钟
     *
     * @param length 追加分钟数值
     * @param date   需要追加值的时间
     * @return 追加后的时间格式格式 yyyy-MM-dd HH:mm:ss
     */
    public static Date addMinuteTime(Date date, int length) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            return sdf.parse(childAddMinuteTime(date, length));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 在给定日期上追加秒数
     *
     * @param length 追加秒数值
     * @param date   需要追加值的时间
     * @return 追加后的时间格式格式 yyyy-MM-dd HH:mm:ss
     */
    public static Date addSecondTime(Date date, int length) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            return sdf.parse(childAddSecondTime(date, length));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Java Calender类获得指定日期加几天
     *
     * @param day 被追加的日期字符串 yyyy-MM-dd 或更精确
     * @param d   day 追加的天数
     * @return 追加后的时间字符串
     */
    public static String addDay(String day, int d) {
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        try {
            date = new SimpleDateFormat(DAY_FORMAT).parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int length = c.get(Calendar.DATE);
        c.set(Calendar.DATE, length + d);
        return new SimpleDateFormat(DAY_FORMAT).format(c.getTime());
    }

    /**
     * 比较两个时间的大小
     *
     * @param date1 日期1 yyyy-MM-dd HH:mm:ss
     * @param date2 日期2 yyyy-MM-dd HH:mm:ss
     * @return true:date1在date2前  false:date1在date2后 (前后判断以二维时间轴为准)
     */
    public static boolean compareDate(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            Date dt1 = sdf.parse(date1);
            Date dt2 = sdf.parse(date2);
            return dt1.getTime() >= dt2.getTime();
//            if (dt1.getTime() > dt2.getTime()) {
            //System.out.println("dt1 在dt2前");
//                flag = true;
//            } else if (dt1.getTime() < dt2.getTime()) {
            //System.out.println("dt1在dt2后");
//                flag = false;
//            }else{
//                System.out.println("两个时间相等");
//                flag = true;
//            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    /**
     * 在给定日期上追加分钟
     *
     * @param length 追加分钟数值
     * @param date   需要追加值的时间
     * @return 追加后的时间字符串 yyyy-MM-dd HH:mm:ss
     */
    private static String childAddMinuteTime(Date date, int length) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        return sdf.format(new Date(new Date().getTime() + length * 60 * 1000));
    }

    /**
     * 在给定日期上追加秒数
     *
     * @param length 追加秒数值
     * @param date   需要追加值的时间
     * @return 追加后的时间字符串 yyyy-MM-dd HH:mm:ss
     */
    private static String childAddSecondTime(Date date, int length) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        return sdf.format(new Date(date.getTime() + length * 1000));
    }

    /**
     * 去除Mybatis使用String接收date后缀多的.0
     *
     * @param date 时间的字符串
     * @return 去除后缀.0的字符串
     */
    public static String cutPointZeroSuffix(String date) {
        if (MyUtil.isBlank(date)) {
            return null;
        }
        // 数字中不包含"."  或者 不是 年月日 时分秒
        if (!date.contains(".") || date.length() < 19) {
            return date;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Date result = null;
        try {
            result = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(result);
    }

    /**
     * 是否周末
     *
     * @param dateStr
     * @return
     */
    public static boolean isWeekend(String dateStr) {
        Date date = stringToDate(dateStr, DAY_FORMAT);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }

    /**
     * 获取当前时间戳毫秒
     *
     * @return long毫秒数
     */
    public static long getCurrentMilTimeStamp() {
        long mils = Calendar.getInstance().getTimeInMillis();
        return mils;
    }

    /**
     * 根据传入时间字符串获取当前时间戳毫秒,eg:2018-06-08 15:12:12
     *
     * @param dateStr dateStr
     * @return long毫秒数
     */
    public static long getCurrentMilTimeStamp(String dateStr) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat(DATE_FORMAT).parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long mils = calendar.getTimeInMillis();
        return mils;
    }
}


原文地址:https://www.cnblogs.com/fangh816/p/13295588.html