我正在尝试使用以下命令创建一个“固定”时间(24 小时制的午夜,即 00:00:00)以设置为 SQL SELECT 查询的字符串...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar todayDate = new GregorianCalendar();
Log.d(TAG, "todayDate: " + todayDate.getTime().toString());
Log.d(TAG, "formatted todayDate: " + sdf.format(todayDate.getTime()));
todayDate.clear(Calendar.HOUR);
todayDate.clear(Calendar.MINUTE);
todayDate.clear(Calendar.SECOND);
todayDate.set(Calendar.HOUR, 0);
todayDate.set(Calendar.MINUTE, 0);
todayDate.set(Calendar.SECOND, 0);
Log.d(TAG, "formatted modified todayDate: " + sdf.format(todayDate.getTime()));
这很好,除非当前时间是下午。例如,
todayDate: Fri Jan 28 23:34:34 GMT 2011
formatted todayDate: 2011-01-28 23:34:34
formatted modified todayDate: 2011-01-28 12:00:00 <- THE hour is 12 not 00
如果我在当前时间介于午夜和中午之间(即 00:00:00 -> 11:59:59 AM)时执行此操作,则格式化字符串中的小时正确设置为 00。如果我在任何时候这样做中午之后和午夜之前的时间然后我得到 12 小时而不是 00。
任何人都可以解释一下并帮我找到解决方法(或替代的做事方式)吗?