如何根据HQL中的时间戳范围从DB获取数据

时间:2015-02-23 07:28:18

标签: java hibernate

我有一个HQL查询,可以从给定用户时间戳的DB中获取数据。查询工作正常但不能获取我期望的确切数据。

例如:我通过了02/16 / 2015,02 / 23/2015,我的jsp页面的值作为开始日期和结束日期。我将这些值传递给HQL。但是我的代码在2015年1月16日之间给出了结果和02/23 / 2015.reduce 1个月。所以我想知道我怎样才能实现我的预期。下面是我的代码。谢谢。

 public static List<TestResult> getTestResulDataSet(String sDate,String eDate)  { 
    final Map<String,List<TestResult> > dataSet = new HashedMap();

    DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
     Date date1=null, date2=null;
    String Rsdate=sDate;
    String Redate=eDate;        
        try {
            date1 = (Date) df.parse(Rsdate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            date2 = (Date) df.parse(Redate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    final Date date3=date1; 
    final Date date4=date2; 

    Transactions.exec(new Procedure() {
        @Override
        public void execute() throws Throwable {
            List<TestResult> rs = new ArrayList();

            rs = Transactions.getSession().createCriteria(TestResult.class).add(Restrictions.ge("timestamp",date3)).add(Restrictions.le("timestamp",date4)).list();

            dataSet.put("TestResult", rs);

        }
    });

1 个答案:

答案 0 :(得分:0)

正确的日期格式为MM/dd/yyyy

正确:DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

mm代表分钟,而MM代表数月。 官方格式说明:

Letter  Date or Time Component  Presentation    Examples
G   Era designator  Text    AD
y   Year    Year    1996; 96
Y   Week year   Year    2009; 09
M   Month in year   Month   July; Jul; 07
w   Week in year    Number  27
W   Week in month   Number  2
D   Day in year Number  189
d   Day in month    Number  10
F   Day of week in month    Number  2
E   Day name in week    Text    Tuesday; Tue
u   Day number of week (1 = Monday, ..., 7 = Sunday)    Number  1
a   Am/pm marker    Text    PM
H   Hour in day (0-23)  Number  0
k   Hour in day (1-24)  Number  24
K   Hour in am/pm (0-11)    Number  0
h   Hour in am/pm (1-12)    Number  12
m   Minute in hour  Number  30
s   Second in minute    Number  55
S   Millisecond Number  978
z   Time zone   General time zone   Pacific Standard Time; PST; GMT-08:00
Z   Time zone   RFC 822 time zone   -0800
X   Time zone