I have a query in mysql which compares 2 dates like this
convert_tz(updatedDate,'+05:30','-05:00') < ?
the convert function returns the value of column createddate in US Time. when I run this query in mysql query browser like
convert_tz(updatedDate,'+05:30','-05:00') < '2013-04-14 09:30:00'
it gives me correct values for example
product count
------- ------
A 123
B 7
Now, I am setting this in java using PreparedStatement like this
pst.setTimestamp(1, new java.sql.Timestamp(end.getTimeInMillis()));
rs=pst.executeQuery();
System.out.println("=====new Open Tickets Query executed=====");
System.out.println(pst);
the last line prints the whole query and the value set is
convert_tz(updatedDate,'+05:30','-05:00') < '2013-04-14 09:30:00'
but it gives me different values like this
product count
------- ------
A 155
B 19
So, I suspected that it is TimeZone problem I changed my code to
end.setTimeZone(TimeZone.getTimeZone("America/New York"));
pst.setTimestamp(1, new java.sql.Timestamp(end.getTimeInMillis()));
rs=pst.executeQuery();
System.out.println("=====new Open Tickets Query executed=====");
System.out.println(pst);
but it still gives same wrong result.
More info: How I am setting Calendar end variable
I have a web application which gives me date string "2013-04-14 09:30:00"
DateFormat df1=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar end=Calendar.getInstance();
end.setTime(df1.parse(endString));
end.set(Calendar.HOUR, 9);
end.set(Calendar.MINUTE, 30);
end.set(Calendar.SECOND, 0);
Also, for experiment I tried with java.util.Date object it gives me correct result following is the code
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
end.setTime(sdf.parse("2012-10-01 00:00:00"));
pst.setTimestamp(1, new java.sql.Timestamp(end.getTime()));
UPDATE :- If I use a deprecated method the answer is correct
pst.setTimestamp(1, new java.sql.Timestamp(octDate.get(Calendar.YEAR)-1900,octDate.get(Calendar.MONTH),octDate.get(Calendar.DATE),octDate.get(Calendar.HOUR),octDate.get(Calendar.MINUTE),octDate.get(Calendar.SECOND),0));
pst.setTimestamp(2, new java.sql.Timestamp(end.get(Calendar.YEAR)-1900,end.get(Calendar.MONTH),end.get(Calendar.DATE),end.get(Calendar.HOUR),end.get(Calendar.MINUTE),end.get(Calendar.SECOND),0));
UPDATE 2:- After the suggestion of first answer I did this
1) executed SELECT NOW()
in mysql and it returned '2013-04-22 11:56:08'
2) executed
System.out.println(new Date(System.currentTimeMillis()));
output : Mon Apr 22 11:56:25 IST 2013
means both systems have same timezone