claculating the number of years from two java.Util dates

Discussion in 'Java' started by manzur, Jun 13, 2006.

  1. manzur

    manzur Guest

    I have two java.util dates with me i want calculate the number of years
    between them .

    Example:

    Date1:19/02/2006
    Date2:19/02/2007

    If i give these two dates i should get the number ofyears as 1.

    thanks in advance
    manzur, Jun 13, 2006
    #1
    1. Advertising

  2. manzur

    MiSt Guest

    manzur napisał(a):
    > I have two java.util dates with me i want calculate the number of years
    > between them .
    >
    > Example:
    >
    > Date1:19/02/2006
    > Date2:19/02/2007
    >
    > If i give these two dates i should get the number ofyears as 1.
    >


    It depends what do you want.
    What if ?:

    Date1=19/12/2006,
    Date2=19/02/2007

    It should give 1 year or 0 year?

    If you want only calendar differnce (in my example 1 year) create two
    Calendar objects and get years fields values and check diference

    If you want duration (in my example 0 year) get time in milis from date
    get differnce and divide by number of milis in year or simply use
    DurationFormatUtils from apache commons lang

    --
    MiSt
    MiSt, Jun 13, 2006
    #2
    1. Advertising

  3. manzur

    Seamus Guest

    manzur wrote:
    > I have two java.util dates with me i want calculate the number of years
    > between them .
    >
    > Example:
    >
    > Date1:19/02/2006
    > Date2:19/02/2007
    >
    > If i give these two dates i should get the number ofyears as 1.
    >
    > thanks in advance


    Given two Data objects, d1 and d2

    long elapsed = d2.getTime() - d1.getTime();
    // elapsed is in milliseconds so we have to convert to year
    int years = elapsed * 1000 * 60 * 60 * 24 * 365; // leap years make it
    more complex TEISTTR
    Seamus, Jun 13, 2006
    #3
  4. manzur

    P.Hill Guest

    Seamus wrote:
    > long elapsed = d2.getTime() - d1.getTime();
    > // elapsed is in milliseconds so we have to convert to year
    > int years = elapsed * 1000 * 60 * 60 * 24 * 365;


    // leap years make it more complex TEISTTR

    Oh and since we don't use a calendar with leaps (nor funny days with 23
    and 24 hours) then it really isn't important. Oh wait we do, so your
    solution is pretty poor suggestion, particularly for those real world
    calculations like right about the same time last year to right about the
    same time this year.

    Suggestion to OP, use the Calendar object and convert properly to
    either calendar year and if you want more than (any day in ) 2006
    minus (any day in) 2005 is 1, I'd suggest working with day of year
    or month of year and day of month to work out "delta years" instead of
    "calendar years". All the fields I mentioned are available from
    java.util.Calendar.

    -Paul
    P.Hill, Jun 15, 2006
    #4
  5. manzur

    Seamus Guest

    P.Hill wrote:
    > Seamus wrote:
    > > long elapsed = d2.getTime() - d1.getTime();
    > > // elapsed is in milliseconds so we have to convert to year
    > > int years = elapsed * 1000 * 60 * 60 * 24 * 365;

    >
    > // leap years make it more complex TEISTTR
    >
    > Oh and since we don't use a calendar with leaps (nor funny days with 23
    > and 24 hours) then it really isn't important. Oh wait we do, so your
    > solution is pretty poor suggestion, particularly for those real world
    > calculations like right about the same time last year to right about the
    > same time this year.
    >
    > Suggestion to OP, use the Calendar object and convert properly to
    > either calendar year and if you want more than (any day in ) 2006
    > minus (any day in) 2005 is 1, I'd suggest working with day of year
    > or month of year and day of month to work out "delta years" instead of
    > "calendar years". All the fields I mentioned are available from
    > java.util.Calendar.
    >
    > -Paul


    Yes, and I mentioned that - smartass.
    Seamus, Jun 20, 2006
    #5
    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. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    859
    Rob Schieber
    Sep 30, 2005
  2. thebjorn
    Replies:
    23
    Views:
    1,542
    thebjorn
    Jul 29, 2006
  3. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    189
    Mark Schupp
    Aug 9, 2004
  4. Li Chen
    Replies:
    2
    Views:
    114
    Li Chen
    Mar 4, 2010
  5. Bambero

    counting years between two dates

    Bambero, Nov 19, 2004, in forum: Javascript
    Replies:
    7
    Views:
    190
    Dr John Stockton
    Nov 21, 2004
Loading...

Share This Page