报错如下:
错误代码:‘
@Override public List<Pxml> selectList(String startTime, String endTime) { Example example = new Example(Pxml.class); example.createCriteria().andBetween("uploadTime", startTime, endTime).andEqualTo("dealStatus", 0); List<Pxml> list = pxmlDao.selectByExample(example); return list; }
原因分析:Oracle数据库的UPLOAD_TIME为DATE类型,故需要将string类型转成Date类型。
修改代码如下:
public List<Pxml> selectList(String startTime, String endTime) { Example example = new Example(Pxml.class); example.createCriteria().andBetween("uploadTime", com.ljxx.common.util.DateUtil.parseDate(startTime), com.ljxx.common.util.DateUtil.parseDate(endTime)).andEqualTo("dealStatus", 0); List<Pxml> list = pxmlDao.selectByExample(example); return list; }
DateUtil工具类如下:
@Slf4j public class DateUtil { public static final String LONG_MODEL = "yyyy-MM-dd HH:mm:ss"; public static final String LONG_MODEL_MS = "yyyy-MM-dd HH:mm:ss.S"; public static final String LONG3_MODEL = "yyMMddHHmmss"; public static final String LONG2_MODEL = "yyyy-MM-dd HH:mm"; public static final String SHORT_MODEL = "yyyy-MM-dd"; public static final String MONTH_MODEL = "yyyy-MM"; public static final String TIME_MODEL = "hh:mm:ss"; public static final String YEAR_MODEL = "yyyy"; public static String toStringNoInterval(Date date, int length) { SimpleDateFormat formatter = null; if (length == 8) formatter = new SimpleDateFormat("yyyyMMdd"); else if (length == 6) formatter = new SimpleDateFormat("yyyyMM"); else if (length == 14) formatter = new SimpleDateFormat(LONG3_MODEL); else if (length == 17) formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); else if (length == 4) formatter = new SimpleDateFormat(YEAR_MODEL); else return date.toString(); return formatter.format(date); } /** * 时间格式化 * * @param d * @return */ public static String dateFormat(Date d, String model) { if (d == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(model); return sdf.format(d); } /** * 时间格式化 * * @param time * @return */ public static String dateFormat(String time, String model) { SimpleDateFormat sdf = new SimpleDateFormat(model); return sdf.format(cn.hutool.core.date.DateUtil.parse(time)); } /** * 时间格式化 * @param d * @return */ public static String dateFormatMs(Date d) { if (d == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(LONG_MODEL_MS); return sdf.format(d); } /** * 字符串转java.util.date * @param date * @return */ public static Date parseDate(String date) { String patterns[] = { "yyyy-MM-dd", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss.S", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S" }; try { return org.apache.commons.lang3.time.DateUtils.parseDate(date,patterns); } catch (ParseException e) { log.error("日期格式化失败{}" + date, e); } return null; } /** * 字符串转java.sql.date * @param date * @return */ public static java.sql.Date parseSqlDate(String date) { Date utilDate = parseDate(date); if(null != utilDate){ return new java.sql.Date(utilDate.getTime()); } return null; } //获取一天的开始时间 public static String beginTime(String startDate){ return startDate + " 00:00:00"; } //获取一天的结束时间 public static String endTime(String endDate){ return endDate + " 23:59:59"; } }
还有可能的原因是:表中日期类型如PRODUCE_DATE字段的类型与实体类的类型不一致.