Simple date compare

Discussion in 'Java' started by Ulf, Mar 9, 2008.

  1. Ulf

    Ulf Guest

    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.
     
    Ulf, Mar 9, 2008
    #1
    1. Advertising

  2. Ulf

    Roedy Green Guest

    On Sun, 9 Mar 2008 00:53:15 -0800 (PST), Ulf <>
    wrote, quoted or indirectly quoted someone who said :

    >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
     
    Roedy Green, Mar 9, 2008
    #2
    1. Advertising

  3. Roedy Green wrote:
    > On Sun, 9 Mar 2008 00:53:15 -0800 (PST), Ulf <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> 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.
    > --
    >
    > Roedy Green Canadian Mind Products
    > The Java Glossary
    > http://mindprod.com
     
    Andrea Francia, Mar 9, 2008
    #3
  4. Ulf

    Eric Sosman Guest

    Ulf wrote:
    > 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.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 9, 2008
    #4
  5. Ulf

    Lew Guest

    Andrea Francia wrote:
    > Roedy Green wrote:
    >> On Sun, 9 Mar 2008 00:53:15 -0800 (PST), Ulf <>
    >> wrote, quoted or indirectly quoted someone who said :
    >>
    >>> 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.


    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.

    --
    Lew
     
    Lew, Mar 9, 2008
    #5
  6. Ulf

    Ulf Guest

    On 9 Mar, 14:51, Lew <> wrote:
    > Andrea Francia wrote:
    > > Roedy Green wrote:
    > >> On Sun, 9 Mar 2008 00:53:15 -0800 (PST), Ulf <>
    > >> wrote, quoted or indirectly quoted someone who said :

    >
    > >>> Can anyone please help me with a simple solution?

    >
    > >> 1. use BigDate.  seehttp://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.

    >
    > 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#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.
    >
    > --
    > 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
     
    Ulf, Mar 9, 2008
    #6
  7. Ulf

    Lew Guest

    Ulf wrote:
    > 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.

    --
    Lew
     
    Lew, Mar 9, 2008
    #7
  8. Ulf

    Roedy Green Guest

    On Sun, 09 Mar 2008 10:45:09 GMT, Andrea Francia
    <> 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
     
    Roedy Green, Mar 10, 2008
    #8
  9. Ulf

    Roedy Green Guest

    On Sun, 09 Mar 2008 09:51:40 -0400, Lew <> wrote,
    quoted or indirectly quoted someone who said :

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

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 10, 2008
    #9
  10. Ulf

    Roedy Green Guest

    On Sun, 09 Mar 2008 09:40:08 -0400, Eric Sosman
    <> 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
     
    Roedy Green, Mar 10, 2008
    #10
  11. Roedy Green wrote:
    > 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
     
    Andrea Francia, Mar 10, 2008
    #11
  12. Ulf

    Guest

    , Mar 10, 2008
    #12
  13. Ulf

    Jacob Guest

    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James P.
    Replies:
    7
    Views:
    12,626
    phamtasmic
    Jul 13, 2004
  2. CZ
    Replies:
    1
    Views:
    707
    Jack Li
    Sep 23, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,301
    Michael Borgwardt
    May 30, 2004
  4. Michael Haas

    compare date field to current date

    Michael Haas, Feb 15, 2004, in forum: ASP General
    Replies:
    4
    Views:
    291
    Mike Haas
    Feb 15, 2004
  5. Scott Knapp

    Compare Input Date to Server Date

    Scott Knapp, Oct 2, 2003, in forum: Javascript
    Replies:
    2
    Views:
    188
    Dr John Stockton
    Oct 2, 2003
Loading...

Share This Page