0

I'm trying to work with dates in sqlite and I need a way of comparing them. My issue is that when I try to save a setup date into the database it gives me new values; They however don't differ much - just in milliseconds.

Here's the code:

Date today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);

How do I make it to give me constant values ?

P.S. I could've divided it by 1000 and save but I use greenDao framework, which encapsulates the insertion logic.

Thanks.

midnight
  • 3,420
  • 3
  • 36
  • 58

2 Answers2

1

Try:

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() allocates the current time returned by System.currentTimeMillis(). Which means you are not resetting milliseconds in your code.

dvlpr
  • 139
  • 8
0

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.

Community
  • 1
  • 1
alci
  • 335
  • 1
  • 3
  • 19