1

I have a HQL query that fetch data from DB given user time stamp.the query works fine but not fetch exact data i expect.

Eg: i passed 02/16/2015,02/23/2015, values from my jsp pages as start date and end date.i pass those values to HQL.but my code gives me result between 01/16/2015 and 02/23/2015.reduce 1 month.so i would like to know how can i achieve that what i expected.below is my code.Thank you.

 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);

        }
    });
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74

1 Answers1

0

The correct dateformat is MM/dd/yyyy

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

mm represent minutes while MM represents months. Official format description:

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
Mythul
  • 1,807
  • 7
  • 34
  • 53
  • Thank you for your answer but still dont work.the problem is i can not fetch current month data. – gihan-maduranga Feb 23 '15 at 09:47
  • Are you sure the date didn't fix it ? Either way it is a bug there. Lets say I have the date "02/11/2015", using new SimpleDateFormat("mm/dd/yyyy") results in the date January 11 2015, hour 00:02:00 (2 minutes) while using new SimpleDateFormat("MM/dd/yyyy") gives the date February 11 2015 hour 00:00:00 . There is the month difference. – Mythul Feb 23 '15 at 10:19
  • Maybe this question could help you also: http://stackoverflow.com/questions/21265134/hql-to-query-records-between-two-dates – Mythul Feb 23 '15 at 10:23
  • after i changed my date format mm into MM HQL doesn't fetch any data even.but there is no error.but when i give mm its work.but one month behind.i think the error is on my date format implementation.also when search field it is necessary to have current month value as a sdate or edate if not either it isn't work also. – gihan-maduranga Feb 23 '15 at 10:34
  • 1
    the previously date goes as 2015-01-20 12:29:31.084 even i send 02/20/2015 but after changed mm into MM its sent as Mon Feb 16 00:00:00 IST 2015.the problem is HQL doesnt work.even there is no error shown.thanks. – gihan-maduranga Feb 23 '15 at 11:09