java - Set time to 00:00:00 -
i have problem resetting hours in java. given date want set hours 00:00:00.
this code :
/** * resets milliseconds, seconds, minutes , hours provided date * * @param date * @return */ public static date trim(date date) { calendar calendar = calendar.getinstance(); calendar.settime(date); calendar.set(calendar.millisecond, 0); calendar.set(calendar.second, 0); calendar.set(calendar.minute, 0); calendar.set(calendar.hour, 0); return calendar.gettime(); }
the problem time 12:00:00
, 00:00:00
, when query database entity saved on 07.02.2013 00:00:00
, actual entity time, stored, 12:00:00
query fails.
i know 12:00:00 == 00:00:00
!
i using appengine. appengine bug, problem or other issue? or depend on else?
use constant instead of calendar.hour
, use calendar.hour_of_day
.
calendar.set(calendar.hour_of_day, 0);
calendar.hour
uses 0-11 (for use am/pm), , calendar.hour_of_day
uses 0-23.
to quote javadocs:
public static final int hour
field number , set indicating hour of morning or afternoon. hour used 12-hour clock (0 - 11). noon , midnight represented 0, not 12. e.g., @ 10:04:15.250 pm hour 10.
and
public static final int hour_of_day
field number , set indicating hour of day. hour_of_day used 24-hour clock. e.g., @ 10:04:15.250 pm hour_of_day 22.
testing ("now" c. 14:55 on july 23, 2013 pacific daylight time):
public class main { static simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); public static void main(string[] args) { calendar = calendar.getinstance(); now.set(calendar.hour, 0); now.set(calendar.minute, 0); now.set(calendar.second, 0); system.out.println(sdf.format(now.gettime())); now.set(calendar.hour_of_day, 0); system.out.println(sdf.format(now.gettime())); } }
output:
$ javac main.java $ java main 2013-07-23 12:00:00 2013-07-23 00:00:00
Comments
Post a Comment