public class DayUtil { //log private static final Logger logger = LoggerFactory.getLogger(GeneralIntroductionQueryParam.class); //日期格式-日 public static final DateFormat DATE_FORMAT_DAY = new SimpleDateFormat("yyyy-MM-dd"); //日期格式-日-短格式 public static final DateFormat DATE_FORMAT_SHORT = new SimpleDateFormat("yyyyMMdd"); //日期格式-日-长格式 public static final DateFormat DATE_FORMAT_LONG= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 日期字符串检查 * * @param oneDay 单日日期字符串 * @return 转换成日期类型 */ public static Date checkDayDate(String oneDay, String errorMsg) { if (errorMsg == null || errorMsg.length() == 0) throw new ValidationException("输入字期字符串为空!"); Date result = null; try { result = DATE_FORMAT_DAY.parse(oneDay.trim()); } catch (ParseException e) { logger.error("checkDayDate error:", ExceptionUtils.getStackTrace(e)); logger.error("发生参数转换错误,参数为:" + oneDay); throw new ValidationException("参数值:" + oneDay + " " + errorMsg); } catch (Exception e) { logger.error("发生参数转换错误,参数为:" + oneDay); logger.error("checkDayDate error:", ExceptionUtils.getStackTrace(e)); } return result; } /** * 日是期类型格式转换 * * @param srcFormat 源格式 * @param targetFormat 目标格式 * @param dateStr 要转换的日期字符串 * @return 转换后字符串 */ public static String convert(DateFormat srcFormat, DateFormat targetFormat, String dateStr) { try { Date date = srcFormat.parse(dateStr); return targetFormat.format(date); } catch (Exception e) { logger.error("convert error:", ExceptionUtils.getStackTrace(e)); } return null; } /* public static void main(String[] args){ Date date = checkDayDate("2017-08-17", "错误信息"); System.out.println(date);// parse()方法字符串转化成标准日期类型:Thu Aug 17 00:00:00 CST 2017 }*/