/** * 按照默认formatStr的格式,转化dateTimeStr为Date类型 dateTimeStr必须是formatStr的形式 * * @param dateTimeStr * @param formatStr * @return */ public static Date getDate(String dateTimeStr, String formatStr) { try { if (dateTimeStr == null || "".equals(dateTimeStr)) { return null; } DateFormat sdf = DateUtil.getDateFormat(formatStr); return sdf.parse(dateTimeStr); } catch (ParseException e) { throw new MyException(e); } }
/** * 将YYYYMMDD转换成Date日期 * * @param date * @return * @throws BusinessException */ public static Date transferDate(String date) { if (StringUtils.isEmpty(date) || date.length() != 8) { throw new RuntimeException("日期格式错误"); } String con = "-"; String yyyy = date.substring(0, 4); String mm = date.substring(4, 6); String dd = date.substring(6, 8); int month = Integer.parseInt(mm); int day = Integer.parseInt(dd); if (month < 1 || month > 12 || day < 1 || day > 31) { throw new RuntimeException("日期格式错误"); } String str = yyyy + con + mm + con + dd; return DateUtil.getDate(str, DateUtil.YYYY_MM_DD_EN); }
/** * 获取指定日期mins分钟后的一个(formatStr)的字符串 * * @param date * @param mins * @param formatStr * @return */ public static String getDateOfMin(String date, int mins, String formatStr) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(DateUtil.getDate(date, formatStr)); now.add(Calendar.SECOND, mins * 60); return dateToDateString(now.getTime(), formatStr); }
/** * 获取指定日期day天后的一个(formatStr)的字符串 * * @param date * @param mins * @param formatStr * @return */ public static String getDateOfDay(String date, int day, String formatStr) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(DateUtil.getDate(date, formatStr)); now.add(Calendar.DATE, day); return dateToDateString(now.getTime(), formatStr); }
/** * 获取指定日期mon月后的一个(formatStr)的字符串 * * @param date * @param mins * @param formatStr * @return */ public static String getDateOfMon(String date, int mon, String formatStr) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(DateUtil.getDate(date, formatStr)); now.add(Calendar.MONTH, mon); return dateToDateString(now.getTime(), formatStr); }