Simple date compare

U

Ulf

I want to check if todays date <= a date retrieved from a SQL
database.

like this: if (today <= toDate)

I only want to care about the date, and not the time.

From the database I get the date with time set to all 0, but from the
today = new Date() I get the time set, and if todays date = toDate the
test will fail because the time partion is larger.

Can anyone please help me with a simple solution?

Using Calendar would be possible, but adds a lot of code.
 
E

Eric Sosman

Ulf said:
I want to check if todays date <= a date retrieved from a SQL
database.

like this: if (today <= toDate)

I only want to care about the date, and not the time.

From the database I get the date with time set to all 0, but from the
today = new Date() I get the time set, and if todays date = toDate the
test will fail because the time partion is larger.

Can anyone please help me with a simple solution?

Using Calendar would be possible, but adds a lot of code.

Use Calendar. I think you'll find that "a lot" is
less than you fear.
 
L

Lew

Andrea said:
This is not safe, it will not take in account the leap second.

The OP's only objection to the Calendar class is
Using Calendar would be possible, but adds a lot of code.

In other words, laziness, even if it were true, which it isn't.

Calendar lets one zero out the time fields, leaving only the date fields filled.

Calendar cal = Calendar.getInstance();
cal.set( Calendar.HOUR, 0 );
cal.set( Calendar.MINUTE, 0 );
cal.set( Calendar.SECOND, 0 );
cal.set( Calendar.MILLISECOND, 0 );

It lets you directly compare one instance to another, using
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#compareTo(java.util.Calendar)>

That's going to be a lot less code and a lot more direct than messing with
milliseconds and timezone calculations. This time laziness is steering you in
the direction of *more* work, not less.
 
U

Ulf

The OP's only objection to the Calendar class is


In other words, laziness, even if it were true, which it isn't.

Calendar lets one zero out the time fields, leaving only the date fields filled.

   Calendar cal = Calendar.getInstance();
   cal.set( Calendar.HOUR, 0 );
   cal.set( Calendar.MINUTE, 0 );
   cal.set( Calendar.SECOND, 0 );
   cal.set( Calendar.MILLISECOND, 0 );

It lets you directly compare one instance to another, using
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#compare...)>

That's going to be a lot less code and a lot more direct than messing with
milliseconds and timezone calculations.  This time laziness is steering you in
the direction of *more* work, not less.

Thanks for your answers.

The think is that... really I'm lazy... but since I'm converting an
old non Java application that uses a lot of dates without the time
part, I was kind of worried that I missed the simple solution.

I will just include a simple function in my utilities class that uses
Calendar to return a date with time = 0.

/Ulf
 
L

Lew

Ulf said:
The think is that... really I'm lazy...

"Laziness" in engineering is actually a virtue. Like other skills, the trick
is knowing when to use it, and which of several alternatives is truly the lazier.
but since I'm converting an
old non Java application that uses a lot of dates without the time
part, I was kind of worried that I missed the simple solution.

In this case, Calendar is the simple solution.
 
R

Roedy Green

This is not safe, it will not take in account the leap second.

leap seconds in Java are handled by adjustments to the clock.
Computationally they don't exist.
 
R

Roedy Green

Calendar lets one zero out the time fields, leaving only the date fields filled.

Calendar cal = Calendar.getInstance();
cal.set( Calendar.HOUR, 0 );
cal.set( Calendar.MINUTE, 0 );
cal.set( Calendar.SECOND, 0 );
cal.set( Calendar.MILLISECOND, 0 );

It lets you directly compare one instance to another, using
<http://java.sun.com/javase/6/docs/api/java/util/Calendar.html#compareTo(java.util.Calendar)>

That's going to be a lot less code and a lot more direct than messing with
milliseconds and timezone calculations. This time laziness is steering you in
the direction of *more* work, not less.

But if you HAVE to think about timezones to define what you mean by
"today".

What you are doing is looking ahead to the day when the planet uses
UTC without timezones.
 
R

Roedy Green

Use Calendar. I think you'll find that "a lot" is
less than you fear.

On the other paw, make sure you cross check you answers with manual
calculation. There are a million ways to get results, but not the
results you intended from Calendar.

see http://mindprod.com/jgloss/calendar.html
for some of the pitfalls.

Also make sure your code works for different timezones or when client
and server are in different timezones.
 
A

Andrea Francia

Roedy said:
leap seconds in Java are handled by adjustments to the clock.
Computationally they don't exist.
Could you provide a reference for this affirmation, I'm interested.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top