1.hql中时间格式转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = sdf.format(new Date());
sb.append(" and s.askTime < '").append(d).append("' ");
2.几天前的hql查询
//创建Date对象
Date endDate = new Date();
//创建基于当前时间的日历对象
Calendar cl = Calendar.getInstance();
cl.setTime(endDate);
//距离今天,一个月内数据 if(flag.equals("month")){ cl.add(Calendar.MONTH, -1);} //距离今天,一周内的数据if(flag.equals("week")){ cl.add(Calendar.DATE, -7); }
Date startDate = cl.getTime();
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
//格式化开始日期和结束日期
String start = dd.format(startDate);String end = dd.format(endDate);
//如果(flag!=month) && (flag!=week), 则查询的就是当天的数据
String hql = "from tableName where columnName >= '" + start +"' and columnName <= '"+end+"';
转载于:https://www.cnblogs.com/yanjunwu/p/3498035.html
