hibernate查询日期Date传参

340 阅读1分钟

1、传String,将String转成"yyyy-MM-dd hh:mm:ss"格式。

2、sql里用to_char 函数来比较大小。

	nowDateGet = nowDateGet.replace("T"," ");
			nowDate = StringUtil.dateFormat("yyyy-MM-dd hh:mm:ss", nowDateGet);
hql.append(" and to_char(clickTime,'yyyy-MM-dd hh24:mi:ss') >= ? and to_char(clickTime,'yyyy-MM-dd hh24:mi:ss') <= ? ");
public static String dateFormat(String format, String time) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			java.util.Date date = sdf.parse(time);
			return sdf.format(date);
		} catch (Exception e) {
			return "";
		}
	}