Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Simple date compare

Thread view: 
Ulf - 09 Mar 2008 09:53 GMT
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.
Roedy Green - 09 Mar 2008 10:56 GMT
>Can anyone please help me with a simple solution?

1. use BigDate.  see http://mindprod.com/products1.html#COMMON11

2. get the timestamp long, and divide by the number of milliseconds in
a day to get the day number in GMT.  

3. as 2, but adjust by timezone offset in millis first to get local
date.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Andrea Francia - 09 Mar 2008 11:45 GMT
>> Can anyone please help me with a simple solution?
>
> 1. use BigDate.  see http://mindprod.com/products1.html#COMMON11
>
> 2. get the timestamp long, and divide by the number of milliseconds in
> a day to get the day number in GMT.  
This is not safe, it will not take in account the leap second.

> 3. as 2, but adjust by timezone offset in millis first to get local
> date.
[quoted text clipped - 3 lines]
> The Java Glossary
> http://mindprod.com
Lew - 09 Mar 2008 14:51 GMT
>>> Can anyone please help me with a simple solution?
>>
[quoted text clipped - 6 lines]
>> 3. as 2, but adjust by timezone offset in millis first to get local
>> date.

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.uti
l.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.

Signature

Lew

Ulf - 09 Mar 2008 15:54 GMT
> >>> Can anyone please help me with a simple solution?
>
[quoted text clipped - 30 lines]
> --
> Lew

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
Lew - 09 Mar 2008 17:57 GMT
> 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.

Signature

Lew

Roedy Green - 10 Mar 2008 03:30 GMT
>Calendar lets one zero out the time fields, leaving only the date fields filled.
>
[quoted text clipped - 10 lines]
>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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 10 Mar 2008 03:28 GMT
On Sun, 09 Mar 2008 10:45:09 GMT, Andrea Francia
<andrea.francia@gmx.it.invalid> wrote, quoted or indirectly quoted
someone who said :

>> 2. get the timestamp long, and divide by the number of milliseconds in
>> a day to get the day number in GMT.  
>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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Andrea Francia - 10 Mar 2008 10:03 GMT
> 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
sla29970@gmail.com - 10 Mar 2008 19:09 GMT
On Mar 10, 2:03 am, Andrea Francia <andrea.fran...@gmx.it.invalid>
wrote:
> >leap secondsin Java are handled by adjustments to the clock.
> > Computationally they don't exist.

This is true for a POSIX-compliant system clock.  Other system clocks
may count leap seconds.

> Could you provide a reference for this affirmation, I'm interested.
> Thanks

I'll offer these
http://www.ucolick.org/~sla/leapsecs/timescales.html#POSIX
http://www.ucolick.org/~sla/leapsecs/onlinebib.html#POSIX
Eric Sosman - 09 Mar 2008 14:40 GMT
> I want to check if todays date <= a date retrieved from a SQL
> database.
[quoted text clipped - 10 lines]
>
> 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.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Roedy Green - 10 Mar 2008 03:32 GMT
On Sun, 09 Mar 2008 09:40:08 -0400, Eric Sosman
<esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted
someone who said :

>     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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Jacob - 11 Mar 2008 14:23 GMT
http://geosoft.no/software/day/Day.java.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.