java时间工具类

public class DateUtil {

    /**
     * 一天的毫秒数
     */
    public static final long ONE_DAY_MILLI = 24 * 60 * 60 * 1000L;

    /**
     * 一个月(31天)的毫秒数
     */
    public static final long ONE_MONTH_MILLI = 31 * 24 * 60 * 60 * 1000L;

    // 无符号日期长度
    private static final int DATE_LENGTH = 8;

    // 无符号时间长度
    private static final int TIME_LENGTH = 6;

    private DateUtil() {

    }

    /**
     * 时间的各种格式器
     */
    public static final String DATE_STYLE = "yyyy-MM-dd";
    public static final String DATE_STYLE_EMPTY = "yyyyMMdd";
    public static final String DATE_YM = "yyyy-MM";
    public static final String DATE_TIME_STYLE = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_STYLE3 = "yyyyMMddHHmmss";
    public static final String DATE_TIME_STYLE4 = "yyyyMMddHHmmssSSS";
    public static final String DATE_TIME_STYLE5 = "HH:mm:ss";
    public static final String DATE_TIME_STYLE6 = "HHmmss";
    public static final String DATE_YEAR = "yyyy";
    
    
    public static final String DATE_STYLE2 = "yyyy.MM.dd";  
    public static final String DATE_STYLE_SMS = "yyyy年MM月dd日";
    public static final String DATE_STYLE_TIME_S = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_STYLE_TIME_S2 = "yyyy.MM.dd HH:mm:ss"; 

    public static final String TIME_STYLE = "HH:mm:ss";
    public static final String TIME_STYLE2 = "HHmmss";
    public static final String TIME_HH_MM_STYLE = "HH:mm";
    
      
    public static synchronized String formatDate(Date date) {
        if(date == null) {
            return null;
        }
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE);
        String dateStr = SDF1.format(date);
        return dateStr;
    }

    public static synchronized String formatYM(Date date) {
        SimpleDateFormat SDF5 = new SimpleDateFormat(DATE_YM);
        String ymStr;
        ymStr = SDF5.format(date);
        return ymStr;
    }


    public static synchronized String formatMonth(Date date) {
        SimpleDateFormat SDF5 = new SimpleDateFormat(DATE_YM);
        String dateStr;
        dateStr = SDF5.format(date);
        return dateStr;
    }
    
  

    /**
     * 根据毫秒数获取日期字符串
     * 
     * @param milliseconds
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static synchronized String formatDateTime(long milliseconds) {
        SimpleDateFormat SDF2 = new SimpleDateFormat(DATE_TIME_STYLE);
        String dateStr;
        dateStr = SDF2.format(new Date(milliseconds));
        return dateStr;
    }

    public static synchronized String formatDateTime1(long milliseconds) {
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE);
        String dateStr;
        dateStr = SDF1.format(new Date(milliseconds));
        return dateStr;
    }

  

    /**
     * 
     * 格式化日期为 yyyyMMdd
     * 
     * @param date
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static synchronized String formatDateEmpty(Date date) {
        SimpleDateFormat SDF_EMPTY = new SimpleDateFormat(DATE_STYLE_EMPTY);
        String dateStr = SDF_EMPTY.format(date);
        return dateStr;
    }

    /**
     * 
     * 格式化时间为hhMMss
     * 
     * @param date
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static synchronized String formatTimeEmpty(Date date) {
        SimpleDateFormat SDF_EMPTY = new SimpleDateFormat(DATE_TIME_STYLE6);
        String dateStr = SDF_EMPTY.format(date);
        return dateStr;
    }


    public static synchronized Date parseToTime(String dateStr) {
        SimpleDateFormat SDF6 = new SimpleDateFormat(DATE_TIME_STYLE5);
        try {
            Date date = SDF6.parse(dateStr);
            return date;
        } catch (ParseException e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_TIME_STYLE5 + "格式失败");
        }
    }

    public static synchronized Date parseToTimeEmpty(String timeStr) {
        SimpleDateFormat SDF7 = new SimpleDateFormat(DATE_TIME_STYLE6);
        try {
            Date date = SDF7.parse(timeStr);
            return date;
        } catch (ParseException e) {
            throw new LswsException("参数:" + timeStr + "日期转到成" + DATE_TIME_STYLE6 + "格式失败");
        }
    }

   

    

    public static String cutDate(String dateTime) {
        StringBuilder sbr = new StringBuilder();
        if (StringUtils.isNotBlank(dateTime) && 10 <= dateTime.length()) {
            sbr.append(dateTime.substring(0, 4));
            sbr.append(dateTime.substring(5, 7));
            sbr.append(dateTime.substring(8, 10));
        }
        return sbr.toString();
    }

    public static String cutTime(String dateTime) {
        StringBuilder sbr = new StringBuilder();
        if (StringUtils.isNotBlank(dateTime) && 19 <= dateTime.length()) {
            sbr.append(dateTime.substring(11, 13));
            sbr.append(dateTime.substring(14, 16));
            sbr.append(dateTime.substring(17, 19));
        }
        return sbr.toString();
    }

    public static String cutNoNum(String dateTime) {
        StringBuilder sbr = new StringBuilder();
        Pattern p = Pattern.compile("[^0-9]");
        if (StringUtils.isNotBlank(dateTime)) {
            Matcher m = p.matcher(dateTime);
            sbr.append(m.replaceAll("").trim());
        }
        return sbr.toString();
    }

    public static String formatDate(String date) {
        String newDate = date;
        if (StringUtils.isNotBlank(date)) {
            newDate = date.replaceAll("-", "");
        }
        return newDate;
    }

    public static String formatTime(String time) {
        String newTime = time;
        if (StringUtils.isNotBlank(time)) {
            newTime = time.replaceAll(":", "");
        }
        return newTime;
    }

 

    public static synchronized String formatDateToRsf(Date date) {
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE);
        if (date == null) {
            return null;
        }
        String dateStr;
        dateStr = SDF1.format(date);
        return dateStr;
    }
    
    /**
     * 
     * Date转为  yyyy-MM-dd HH:mm:ss
     * 
     * @param date
     * @return  String
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static synchronized String formatDateToString(Date date) {
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_TIME_STYLE);
        if (date == null) {
            return null;
        }
        String dateStr;
        dateStr = SDF1.format(date);
        return dateStr;
    }

    /**
     * 比较时间先后 如果 前面的时间比后面的大 返回true
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDate(Date date1, Date date2) {
        long dateNum1 = date1.getTime();
        long dateNum2 = date2.getTime();
        if (dateNum1 > dateNum2) {
            return true;
        }
        return false;
    }

    /**
     * 验证报文传过来 是不是yyyy-MM-dd格式
     */
    public static boolean checkDate(String date) {
        boolean flag = false;
        if (StringUtils.isNotBlank(date)) {
            String re = "^\\d{4}\\D+\\d{2}\\D+\\d{2}$";
            Pattern pattern = Pattern.compile(re);
            Matcher matcher = pattern.matcher(date);
            if (matcher.matches()) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 验证报文传过来时间 是不是HH:mm:ss格式
     * 
     * @param time
     * @return
     */
    public static boolean checkTime(String time) {
        boolean flag = false;
        if (StringUtils.isNotBlank(time)) {
            String re = "^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$";
            Pattern pattern = Pattern.compile(re);
            Matcher matcher = pattern.matcher(time);
            if (matcher.matches()) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 功能描述: 转换为有符号日期<br>
     *
     * @param date 8位日期
     * @return
     */
    public static String convertDate(String date) {
        if (StringUtils.isBlank(date)) {
            return StringUtils.EMPTY;
        }

        if (DATE_LENGTH != date.length()) {
            return date;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(date.substring(0, 4));
        sb.append("-");
        sb.append(date.substring(4, 6));
        sb.append("-");
        sb.append(date.substring(6, 8));
        
        return sb.toString();
    }

    /**
     * 功能描述: 转换为有符号时间<br>
     *
     * @param date 6位时间
     * @return
     */
    public static String convertTime(String time) {
        if (StringUtils.isBlank(time)) {
            return StringUtils.EMPTY;
        }

        if (TIME_LENGTH != time.length()) {
            return time;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(time.substring(0, 2));
        sb.append(":");
        sb.append(time.substring(2, 4));
        sb.append(":");
        sb.append(time.substring(4, 6));
        
        return sb.toString();
    }

    /**
     * 取n之内的
     * 
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }
    
  

    public static synchronized String formatSmsDate(Date date) {
        SimpleDateFormat SDF_SMS = new SimpleDateFormat(DATE_STYLE_SMS);
        String dateStr = SDF_SMS.format(date);
        return dateStr;
    }

    public static synchronized String formatTimeStyle2(Date date) {
        SimpleDateFormat SDF1 = new SimpleDateFormat(TIME_STYLE2);
        String dateStr = SDF1.format(date);
        return dateStr;
    }

   

    public static synchronized String formatDate2(Date date) {
            SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE2);
            String dateStr = SDF1.format(date);
            return dateStr;
    }

    public static synchronized String formatDateStyleEmpty(Date date) {
        if(ParamsUtil.isNullOrEmpty(date)){
            return Constants.EMPTY;
        }
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE_EMPTY);
        String dateStr = SDF1.format(date);
        return dateStr;
    }

    public static String formatPrintDate(Date date) {
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE);
        String dateStr = SDF1.format(date);
        return dateStr;
    }

    public static String formatPrintDateTime(Date date) {
        if(ParamsUtil.isNullOrEmpty(date)){
            return Constants.EMPTY;
        }
        SimpleDateFormat SDF2 = new SimpleDateFormat(DATE_TIME_STYLE);
        String dateStr = SDF2.format(date);
        return dateStr;
    }

    public static synchronized String formatDateTime(Date date) {
        if(date == null) {
            return Constants.EMPTY;
        }
        String dateStr;
        SimpleDateFormat SDF2 = new SimpleDateFormat(DATE_TIME_STYLE);
        dateStr = SDF2.format(date);
        return dateStr;
    }

    public static synchronized String formatDateTime2(Date date) {
        if(date == null) {
            return Constants.EMPTY;
        }
        String dateStr;
        SimpleDateFormat SDF3 = new SimpleDateFormat(DATE_TIME_STYLE3);
        dateStr = SDF3.format(date);
        return dateStr;
    }

    public static synchronized String formatTime(Date date) {
        SimpleDateFormat SDF_TIME = new SimpleDateFormat(TIME_STYLE);
        String dateStr = SDF_TIME.format(date);
        return dateStr;
    }

    public static synchronized Date parseTime(String dateStr) {
        SimpleDateFormat SDF_TIME = new SimpleDateFormat(TIME_STYLE);
        try {
            Date date = SDF_TIME.parse(dateStr);
            return date;
        } catch (ParseException e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_STYLE + "格式失败",e);
        }
    }
    
    public static synchronized Date parseTime(String timeStr,String timeStyle) {
        SimpleDateFormat SDF_TIME = new SimpleDateFormat(timeStyle);
        try {
            Date date = SDF_TIME.parse(timeStr);
            return date;
        } catch (ParseException e) {
            throw new LswsException("参数:" + timeStr + "日期转到成" + timeStyle + "格式失败",e);
        }
    }

    public static synchronized Date parseToDate(String dateStr) {
        try {
            SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE);
            Date date = SDF1.parse(dateStr);
            return date;
        } catch (Exception e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_STYLE + "格式失败",e);
        }
    }

    public static synchronized Date parseToDate(String dateStr,String format) {
        try {
            SimpleDateFormat SDF1 = new SimpleDateFormat(format);
            Date date = SDF1.parse(dateStr);
            return date;
        } catch (Exception e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_STYLE + "格式失败",e);
        }
    }

    public static synchronized Date parseToDateEmpty(String dateStr) {
        try {
            if(StringUtils.isBlank(dateStr)) {
                return null;
            }
            SimpleDateFormat SDF_EMPTY = new SimpleDateFormat(DATE_STYLE_EMPTY);
            Date date = SDF_EMPTY.parse(dateStr);
            return date;
        } catch (Exception e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_STYLE_EMPTY + "格式失败",e);
        }
    }

    public static synchronized Date parseToDateTime(String dateStr) {
        try {
            if(StringUtils.isBlank(dateStr)) {
                return null;
            }
            SimpleDateFormat SDF2 = new SimpleDateFormat(DATE_TIME_STYLE);
            Date date = SDF2.parse(dateStr);
            return date;
        } catch (Exception e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_TIME_STYLE + "格式失败",e);
        }
    }

    public static synchronized Timestamp parseToTimeStamp(String dateStr) {
            String dt = dateStr.replaceAll("-", "").replaceAll(" ", "").replace(":", "");
            int yeardt = Integer.parseInt(dt.substring(0, 4));
            int monthdt = Integer.parseInt(dt.substring(4, 6));
            int daydt = Integer.parseInt(dt.substring(6, 8));
            int hourOfDay = Integer.parseInt(dt.substring(8, 10));
            int minute = Integer.parseInt(dt.substring(10, 12));
            int second = Integer.parseInt(dt.substring(12, 14));
            GregorianCalendar calendardt = new GregorianCalendar(yeardt, monthdt - 1, daydt, hourOfDay, minute, second);
            long time = calendardt.getTime().getTime();
            return new Timestamp(time);
    }

    public static synchronized Date parseToDateTime2(String dateStr) {
        try {
            SimpleDateFormat SDF3 = new SimpleDateFormat(DATE_TIME_STYLE3);
            Date date = SDF3.parse(dateStr);
            return date;
        } catch (Exception e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + DATE_TIME_STYLE3 + "格式失败",e);
        }
    }

    // 生成17位编码
    public static synchronized String formatDateToLongChar(Date date) {
        SimpleDateFormat SDF4 = new SimpleDateFormat(DATE_TIME_STYLE4);
        String dateStr = SDF4.format(date);
        return dateStr;
    }

    /**
     * 转换时间的格式yyyy-MM-dd HH:mm:ss到yyyyMMddHHmmss
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String convertToDateStr(String date) {
        StringBuilder sbr = new StringBuilder();
        sbr.append(date.substring(0, 4));
        sbr.append(date.substring(5, 7));
        sbr.append(date.substring(8, 10));
        sbr.append(date.substring(11, 13));
        sbr.append(date.substring(14, 16));
        sbr.append(date.substring(17, 19));
        return sbr.toString();

    }

    /**
     * 判断当前时间是否在早8点到晚8点之间
     */
    public static boolean compareHourIn8to20() {
        Calendar rightNow = Calendar.getInstance();
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        if (hour > 7 && hour < 20) {
            return true;
        }
        return false;

    }

    /**
     * 判断当前时间加上时间间隔是否在早8点到晚8点之间
     *
     * @param hourP
     * @param minuteP
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static boolean compareHourIn8to20(int hourP, int minuteP) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.set(Calendar.HOUR_OF_DAY, hourP);
        rightNow.set(Calendar.MINUTE, minuteP);

        int hour = rightNow.get(Calendar.HOUR_OF_DAY);
        if (hour > 7 && hour < 20) {
            return true;
        }
        return false;
    }

    /**
     * 判断查询条件(预约作业时间)是否在 当前时间加上最大时date内, ture:在时间范围内 false:在最大上限之外
     *
     * @param srvTimeStart
     * @param srvTimeEnd
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static boolean compareDate(String srvTimeStart, String srvTimeEnd) {

        if (ParamsUtil.isNullOrEmpty(srvTimeStart) || ParamsUtil.isNullOrEmpty(srvTimeEnd)) {
            return false;
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DATE, 3);
        long rightNowTime = rightNow.getTime().getTime();
        String dt = srvTimeStart.replaceAll("-", "");
        String df = srvTimeEnd.replaceAll("-", "");
        int yeardt = Integer.parseInt(dt.substring(0, 4));
        int monthdt = Integer.parseInt(dt.substring(4, 6));
        int daydt = Integer.parseInt(dt.substring(6, 8));

        int yeardf = Integer.parseInt(df.substring(0, 4));
        int monthdf = Integer.parseInt(df.substring(4, 6));
        int daydf = Integer.parseInt(df.substring(6, 8));

        GregorianCalendar calendardt = new GregorianCalendar(yeardt, monthdt - 1, daydt);
        GregorianCalendar calendardf = new GregorianCalendar(yeardf, monthdf - 1, daydf);
        long dateToTime = calendardt.getTime().getTime();
        long dateFromTime = calendardf.getTime().getTime();

        if (dateFromTime <= rightNowTime && dateToTime <= rightNowTime) {
            return true;
        }
        return false;
    }

    /**
     *
     * 功能描述: <br>
     *  输入日期与当前时间比较 如果早于当前时间 返回 true; 如果晚于当前时间 返回false
     *
     * @param dateTime
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static boolean compareDateWhitCurrentTime(Date dateTime) {
        if (ParamsUtil.isNullOrEmpty(dateTime)) {
            return false;
        }

        long rightNowTime = parseToDate(formatDate(currentDate())).getTime();
        long dateToTime = dateTime.getTime();

        if (dateToTime < rightNowTime) {
            return true;
        }
        return false;
    }

    /**
     *
     * 功能描述: <br>
     * 〈根据输入的日期计算出新时间〉
     *
     * @param days 时间日期
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String computeDate(int days) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, days);
        long date = cal.getTimeInMillis();
        SimpleDateFormat SDF1 = new SimpleDateFormat(DATE_STYLE);
        return SDF1.format(new Date(date));
    }

    /**
     * 取当前时间
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Timestamp currentTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 取当前时间
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static java.sql.Date currentSqlDate() {
        return new java.sql.Date(System.currentTimeMillis());
    }

    /**
     * 取当前时间
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Date currentDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 解析时间
     *
     * @param dateStr
     * @param formatStyle
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Date parseDate(String dateStr, String formatStyle) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }

        if (StringUtils.isBlank(formatStyle)) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new LswsException("参数:" + dateStr + "格式失败",e);
        }
    }

    /**
     * 解析
     *
     * @param dateStr
     * @param formatStyle
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Timestamp parseTimestamp(String dateStr, String formatStyle) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }

        if (StringUtils.isBlank(formatStyle)) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        try {
            return new Timestamp(sdf.parse(dateStr).getTime());
        } catch (ParseException e) {
            throw new LswsException("参数:" + dateStr + "日期转到成" + "格式失败",e);
        }
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate 较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse(sdf.format(smdate));
        Date date2 = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }


    /**
     * 在给定时间上加时间
     *
     * @param date
     * @param addType
     * @param count
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Date addDate(Date date, int addType, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(addType, count);
        return calendar.getTime();
    }

    /**
     *
     * 功能描述: <br>
     * 计算当前日期 上一个月第一天日期 yyyy-mm-dd
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Date getLastMonthFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        // 获取前一个月 月份
        int month = calendar.get(Calendar.MONTH) - 1;
        // 设置新月份
        calendar.set(Calendar.MONTH, month);
        // 设置该月份第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     *
     * 功能描述: <br>
     * 计算当前日期 上一个月第一天日期 yyyy-mm-dd
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Date getLastMonthEndDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        // 获取前一个月 月份
        int month = calendar.get(Calendar.MONTH);
        // 设置新月份
        calendar.set(Calendar.MONTH, month);
        // 设置该月份最后一天
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return calendar.getTime();
    }

    /**
     * 解析时间
     *
     * @param date
     * @param formatStyle
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String formatDate(Date date, String formatStyle) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        return sdf.format(date);
    }

    /**
     * 解析时间
     *
     * @param formatStyle
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String currDateStr(String formatStyle) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        return sdf.format(DateUtil.currentDate());
    }

    /**
     *
     * 功能描述: <br>
     * 获取范围内所有日期
     *
     * @param dBegin
     * @param dEnd
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static List<String> findDates(String dBegin, String dEnd) {
        List<String> lDate = new ArrayList<String>();
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(parseToDate(dBegin));
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(parseToDate(dEnd));
        // 测试此日期是否在指定日期之后
        while (parseToDate(dEnd).after(calBegin.getTime())) {
            lDate.add(formatDate2(calBegin.getTime()));
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
        }
        lDate.add(formatDate2(parseToDate(dEnd)));
        return lDate;
    }

    /**
     * 加小时
     *
     * @param date
     * @param amount
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static String addHore(int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(DateUtil.currentDate());
        c.add(Calendar.HOUR, amount);
        return DateUtil.formatDate(c.getTime(), DateUtil.TIME_HH_MM_STYLE);
    }

    /**
     * 加天
     *
     * @param date
     * @param amount
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static  String addDay(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, amount);
        return DateUtil.formatDate(c.getTime());
    }

    /**
     * 冗余解析<br/>
     * 先当解析 yyyy-MM-dd 再当解析yyyyMMdd
     *
     * @param date
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Date parseDate(String date) {
        if(StringUtils.isBlank(date)) {
            return null;
        }

        String[] formats = new String[] {DATE_STYLE, DATE_STYLE_EMPTY};

        Date value = null;
        boolean pass = true;
        for(int i = 0; pass && i < formats.length; i ++) {
            SimpleDateFormat    sdf = new SimpleDateFormat(formats[i]);
            try {
                value = sdf.parse(date);
                pass = false;
            } catch (Exception e) {
                throw new LswsException("转换失败{}",e);
            }
        }

        return value;
    }

}

 

posted @ 2018-05-05 15:01  nihao小树苗  阅读(217)  评论(0编辑  收藏  举报