我正在尝试使用 sqlite 中的日期,我需要一种比较它们的方法。我的问题是,当我尝试将设置日期保存到数据库中时,它给了我新的值;然而,它们并没有太大区别——仅以毫秒为单位。
这是代码:
Date today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
我如何让它给我恒定的价值?
PS我可以将它除以1000并保存,但我使用greenDao框架,它封装了插入逻辑。
谢谢。
尝试:
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
date = now.getTime();
new Date() 分配由 System.currentTimeMillis() 返回的当前时间。这意味着您不会在代码中重置毫秒。
I think you might want to look at Java Date rounding
Basically the answers suggest using Apache commons-lang DataUtils. See http://commons.apache.org/lang/api-3.1/index.html and especially the truncate method:
public static Date truncate(Date date,int field)
Truncate this date, leaving the field specified as the most significant field. For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if
you passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with MONTH, it would return 1 Mar 2002 0:00:00.000.