Finding Date Difference

Discussion in 'Java' started by Mick, Dec 2, 2004.

  1. Mick

    Mick Guest

    There must be an 'easy' way to find the difference between 2 dates

    ie.

    thisDate_1 = new java.util.Date(strStartDate);
    thisDate_2 = new java.util.Date(strEndDate);

    thisDateDiff = thisDate_2 - thisDate_1 ????



    --
    Mick
     
    Mick, Dec 2, 2004
    #1
    1. Advertising

  2. Mick wrote:
    > There must be an 'easy' way to find the difference between 2 dates
    >
    > ie.
    >
    > thisDate_1 = new java.util.Date(strStartDate);
    > thisDate_2 = new java.util.Date(strEndDate);
    >
    > thisDateDiff = thisDate_2 - thisDate_1 ????
    >

    long thisDateDiff = thisDate_2.getTime() - thisDate_1.getTime();
    ....as always javadoc is your friend:
    <http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#getTime()>

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Dec 2, 2004
    #2
    1. Advertising

  3. Mick

    Mick Guest

    Thanks...but

    Why does this not compile??

    Calendar thisCal_1 = Calendar.getInstance();
    thisDate_1 = new java.util.Date(strStartDate);
    thisCal_1.setTime(thisDate_1);

    Mick

    "Thomas Fritsch" <> wrote in message
    news:...
    > Mick wrote:
    > > There must be an 'easy' way to find the difference between 2 dates
    > >
    > > ie.
    > >
    > > thisDate_1 = new java.util.Date(strStartDate);
    > > thisDate_2 = new java.util.Date(strEndDate);
    > >
    > > thisDateDiff = thisDate_2 - thisDate_1 ????
    > >

    > long thisDateDiff = thisDate_2.getTime() - thisDate_1.getTime();
    > ...as always javadoc is your friend:
    > <http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#getTime()>
    >
    > --
    > "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
    >
     
    Mick, Dec 2, 2004
    #3
  4. On Thu, 02 Dec 2004 18:31:26 GMT, Mick wrote:

    Please refrain form top-posting Mick, I find it nost confusing..
    <http://www.physci.org/codes/javafaq.jsp#netiquette>

    > Why does this not compile??
    >
    > Calendar thisCal_1 = Calendar.getInstance();
    > thisDate_1 = new java.util.Date(strStartDate);
    > thisCal_1.setTime(thisDate_1);


    That would depend on the compilation errors, which are a lot more
    specific than 'not compile' amd actually reference line numbers.
    <http://www.physci.org/codes/javafaq.jsp#exact>

    Check especially the links to 'compile time' error messages at
    the end of that section.

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 2, 2004
    #4
  5. Mick wrote:

    > Thanks...but
    >
    > Why does this not compile??
    >
    > Calendar thisCal_1 = Calendar.getInstance();
    > thisDate_1 = new java.util.Date(strStartDate);

    thisDate_1 is assigned a Date object ...
    > thisCal_1.setTime(thisDate_1);

    .... but setTime(...) expects a long.
    I could set a link to the javadoc of Date#setTime here, but I don't ;-)
    >
    > Mick


    Thomas
    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Dec 2, 2004
    #5
  6. Thomas Fritsch wrote:
    > Mick wrote:
    >
    >> Thanks...but
    >>
    >> Why does this not compile??
    >>
    >> Calendar thisCal_1 = Calendar.getInstance();
    >> thisDate_1 = new java.util.Date(strStartDate);

    >
    > thisDate_1 is assigned a Date object ...
    >
    >> thisCal_1.setTime(thisDate_1);

    >
    > ... but setTime(...) expects a long.
    > I could set a link to the javadoc of Date#setTime here, but I don't ;-)

    Sorry, my answer was wrong.
    I confused Date#setTime with Calendar#setTime
    >
    >>
    >> Mick

    >
    >
    > Thomas



    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Dec 2, 2004
    #6
  7. Mick

    Mick Guest

    "Andrew Thompson" <> wrote in message
    news:11bf4k4qo9jbh$...
    > On Thu, 02 Dec 2004 18:31:26 GMT, Mick wrote:
    >
    > Please refrain form top-posting Mick, I find it nost confusing..
    > <http://www.physci.org/codes/javafaq.jsp#netiquette>
    >
    > > Why does this not compile??
    > >
    > > Calendar thisCal_1 = Calendar.getInstance();
    > > thisDate_1 = new java.util.Date(strStartDate);
    > > thisCal_1.setTime(thisDate_1);

    >
    > That would depend on the compilation errors, which are a lot more
    > specific than 'not compile' amd actually reference line numbers.
    > <http://www.physci.org/codes/javafaq.jsp#exact>
    >
    > Check especially the links to 'compile time' error messages at
    > the end of that section.



    C:\....._jsp.java:35: expected
    thisCal_1.setTime(thisDate_1);
    ^
    1 error

    The 'arrow' is actually under the opening bracket (

    Mick


    >
    > HTH
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
     
    Mick, Dec 2, 2004
    #7
  8. Mick wrote:
    > Thanks...but
    >
    > Why does this not compile??
    >
    > Calendar thisCal_1 = Calendar.getInstance();
    > thisDate_1 = new java.util.Date(strStartDate);
    > thisCal_1.setTime(thisDate_1);

    Exactly which line does not compile? What did the compiler say?


    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Dec 2, 2004
    #8
  9. > "Andrew Thompson" <> wrote in message
    ...
    >> Please refrain form top-posting Mick, I find it nost confusing..
    >> <http://www.physci.org/codes/javafaq.jsp#netiquette>


    Did you read *all* of that link Mick? There were further tips to
    prompt you to trim content no longer relevant.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 2, 2004
    #9
  10. Andrew Thompson, Dec 2, 2004
    #10
  11. Mick

    Jacob Guest

    Mick wrote:
    > There must be an 'easy' way to find the difference between 2 dates


    No there isn't. You can find the difference between two
    moments in time, but make sure the two are represented
    by Calendars of the same reference (i.e. TimeZone).
    The difference is measured in milliseconds so it still
    takes some effort to convert it to something useful
    depending on your context.

    In an attempt to make it easier to work with *dates* (as
    opposed to *times*) I wrote the Day.java class:

    http://geosoft.no/software/day/Day.java.html

    In particluar, note the Day.daysBetween() method.
     
    Jacob, Dec 3, 2004
    #11
  12. Mick

    Tim Ward Guest

    "Jacob" <> wrote in message
    news:fKYrd.10893$...
    >
    > No there isn't. You can find the difference between two
    > moments in time, but make sure the two are represented
    > by Calendars of the same reference (i.e. TimeZone).
    > The difference is measured in milliseconds so it still
    > takes some effort to convert it to something useful
    > depending on your context.


    Oh yes, this is a fun problem regardless of language. Compare a date in July
    with one in December - depending on where you live there's likely to be an
    odd hour in the difference, because of seasonal clock changes, which can
    really srew things up if you're not expecting it.

    --
    Tim Ward
    Brett Ward Limited - www.brettward.co.uk
     
    Tim Ward, Dec 3, 2004
    #12
  13. Mick

    Mick Guest

    "Andrew Thompson" <> wrote in message
    news:10wk5i6cna18f$.1nmh790eoq0fv$...
    > On Thu, 02 Dec 2004 19:44:08 GMT, Mick wrote:
    >
    > > C:\....._jsp.java:35: expected

    > ...
    >
    > expected ..what exactly? The compiler is usually more specific.


    Yes it is but in this case that is all it had for 'expected'.

    There was also this:

    org.apache.jasper.JasperException: Unable to compile class for JSP

    An error occurred at line: -1 in the jsp file: null

    Generated servlet error:
    [javac] Compiling 1 source file

    Mick

    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
     
    Mick, Dec 3, 2004
    #13
  14. On Fri, 03 Dec 2004 12:47:20 GMT, Mick wrote:

    > "Andrew Thompson" <> wrote in message
    > news:10wk5i6cna18f$.1nmh790eoq0fv$...
    >> On Thu, 02 Dec 2004 19:44:08 GMT, Mick wrote:
    >>
    >>> C:\....._jsp.java:35: expected

    >> ...
    >>
    >> expected ..what exactly? The compiler is usually more specific.

    >
    > Yes it is but in this case that is all it had for 'expected'.
    >
    > There was also this:
    >
    > org.apache.jasper.JasperException: Unable to compile class for JSP
    >
    > An error occurred at line: -1 in the jsp file: null
    >
    > Generated servlet error:
    > [javac] Compiling 1 source file


    Aha! I suspect that is the *cause* if the problem.
    Does it occur before or after the 'expected' error?

    And Mick, if I might bug you for one more thing? Please
    trim 'sigs.'*, as well as any material that is no longer
    needed in a thread.

    * These things do not need to be quoted.
    <trim>
    > Mick
    >
    >>
    >> --
    >> Andrew Thompson
    >> http://www.PhySci.org/codes/ Web & IT Help
    >> http://www.PhySci.org/ Open-source software suite
    >> http://www.1point1C.org/ Science & Technology
    >> http://www.LensEscapes.com/ Images that escape the mundane

    </trim>

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 3, 2004
    #14
  15. Mick

    Mick Guest

    > >>
    > >>> C:\....._jsp.java:35: expected
    > >> expected ..what exactly? The compiler is usually more specific.

    > >
    > > Yes it is but in this case that is all it had for 'expected'.
    > >
    > > There was also this:
    > >
    > > org.apache.jasper.JasperException: Unable to compile class for JSP
    > >
    > > An error occurred at line: -1 in the jsp file: null
    > >
    > > Generated servlet error:
    > > [javac] Compiling 1 source file

    >
    > Aha! I suspect that is the *cause* if the problem.
    > Does it occur before or after the 'expected' error?
    >

    This output was before the 'expected' error.

    Mick
     
    Mick, Dec 3, 2004
    #15
    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. Andreas Klemt
    Replies:
    3
    Views:
    1,781
    Natty Gur
    Aug 10, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,358
    Michael Borgwardt
    May 30, 2004
  3. Replies:
    10
    Views:
    937
    Dale King
    May 15, 2006
  4. Edward
    Replies:
    3
    Views:
    200
    Gunnar Hjalmarsson
    Sep 8, 2004
  5. Nik
    Replies:
    11
    Views:
    288
    Dr J R Stockton
    Sep 11, 2009
Loading...

Share This Page