f



Date, date date date....

Date is driving me crazy. I simply need to calculate the age of a person:

this is what doesn't work:

  public int getAge(Person person)
  {
    long d = new java.util.Date().getTime();
    long m =person.dateOfBirth().getTime();
    long l=d-m;
    SimpleDateFormat formatter = new SimpleDateFormat("yy");
    return (new Integer(formatter.format(age))).intValue();
  }

I'm getting 91 for a person who's born in 1983
Since getYear() is depreciated I don't like to use that
I found that Calender could do the job, but how do I cast Date into 
Calender in an easy way?

hints appreciated.....

Pete
0
pietgrijs (11)
5/28/2004 10:39:03 PM
comp.lang.java.programmer 52714 articles. 0 followers. Post Follow

11 Replies
2141 Views

Similar Articles

[PageSpeed] 20

Peter Grison wrote:

Sorry, error in the code snippet:
>  public int getAge(Person person)
>  {
>    long d = new java.util.Date().getTime();
>    long m =person.dateOfBirth().getTime();
>    long l=d-m;
>    SimpleDateFormat formatter = new SimpleDateFormat("yy");
>    return (new Integer(formatter.format(age))).intValue();
>  }

must be

  public int getAge(Person person)
  {
    long d = new java.util.Date().getTime();
    long m =mw.getGeboortedatum().getTime();
    long l=d-m;
    java.util.Date age = new java.util.Date(l);
    SimpleDateFormat formatter = new SimpleDateFormat("yy");
    return (new Integer(formatter.format(age))).intValue();
  }


  Pete
0
leov (6)
5/28/2004 10:44:14 PM
"Peter Grison" <pietgrijs@hotmeel.com> wrote in message
news:c98f4c$sbb$1@news4.tilbu1.nb.home.nl...
> Date is driving me crazy. I simply need to calculate the age of a person:

It's not a "drive" it's a "put."

>
> this is what doesn't work:
>
>   public int getAge(Person person)
>   {
>     long d = new java.util.Date().getTime();
>     long m =person.dateOfBirth().getTime();
>     long l=d-m;
>     SimpleDateFormat formatter = new SimpleDateFormat("yy");
>     return (new Integer(formatter.format(age))).intValue();
>   }
>
> I'm getting 91 for a person who's born in 1983
> Since getYear() is depreciated I don't like to use that
> I found that Calender could do the job, but how do I cast Date into
> Calender in an easy way?
>
> hints appreciated.....
>
> Pete


0
Liz19 (389)
5/28/2004 11:08:13 PM
Peter Grison <pietgrijs@hotmeel.com> wrote:
>Date is driving me crazy. I simply need to calculate the age of a person:
>
>this is what doesn't work:
>
>  public int getAge(Person person)
>  {
>    long d = new java.util.Date().getTime();
>    long m =person.dateOfBirth().getTime();
>    long l=d-m;
>    SimpleDateFormat formatter = new SimpleDateFormat("yy");
>    return (new Integer(formatter.format(age))).intValue();
>  }
>
>I'm getting 91 for a person who's born in 1983
>Since getYear() is depreciated I don't like to use that
>I found that Calender could do the job, but how do I cast Date into 
>Calender in an easy way?
>
Perhaps you should post the actual code, as "age" is undefined in this code, so
there's no telling what the bleep is going on.

0
andy_hill (29)
5/28/2004 11:55:46 PM
On Sat, 29 May 2004 00:39:03 +0200, Peter Grison
<pietgrijs@hotmeel.com> wrote or quoted :

>Date is driving me crazy. I simply need to calculate the age of a person:

see http://mindprod.com/products.html#BIGDATE

It will tell you in days, or years, months and days.

see the age function.

-- 
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming. 
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
0
look-on (4215)
5/29/2004 12:57:27 AM
"Peter Grison" <pietgrijs@hotmeel.com> wrote in message
news:c98f4c$sbb$1@news4.tilbu1.nb.home.nl...
> Date is driving me crazy. I simply need to calculate the age of a person:
>
> this is what doesn't work:
>
>   public int getAge(Person person)
>   {
>     long d = new java.util.Date().getTime();
>     long m =person.dateOfBirth().getTime();
>     long l=d-m;
>     SimpleDateFormat formatter = new SimpleDateFormat("yy");
>     return (new Integer(formatter.format(age))).intValue();
>   }
>
> I'm getting 91 for a person who's born in 1983
> Since getYear() is depreciated I don't like to use that
> I found that Calender could do the job, but how do I cast Date into
> Calender in an easy way?
>
> hints appreciated.....
>
> Pete

Here is a program that you can modify, it asks you to type in
two dates then it tells you the difference between the two in
minutes. you can play with it a little to get rid of the time part



// Created on May 2, 2004
// Sun May  2 15:25:26 CDT 2004

// @author stolen from the web someplace, maybe SUN

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

class DateSubstract
{
  public DateSubstract()
  {
    BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
    Date d1 = readDate(in);
    Date d2 = readDate(in);
    System.out.println("difference between dates in minutes: " +
(d2.getTime() - d1.getTime()) / 60000);
  }
  public static void main(String[] args)
  {
    new DateSubstract();
  }
  Date readDate(BufferedReader in)
  {
    //    SimpleDateFormat df = new SimpleDateFormat();
    //    df.setLenient(true);
    // input format is for example 1/1/04 10:20:33 AM CDT
    Object currentLocale = new Locale("en", "US", "");
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.LONG, (Locale)currentLocale);
    Date d = null;
    while (d == null)
    {
      String line = null;
      while (line == null)
      {
        System.out.println("Enter date: ");
        try
        {
          line = in.readLine();
        }
        catch (IOException e)
        {
          System.out.println(e);
          System.exit(1);
        }
      }
      try
      {
        d = df.parse(line);
      }
      catch (ParseException pe)
      {
        System.out.println("Exception caught:\n" + pe);
      }
    }
    return d;
  } // end of readDate()
}


0
Liz19 (389)
5/29/2004 6:19:22 AM
Peter Grison wrote:
> Date is driving me crazy. I simply need to calculate the age of a person:

Thanks for your support all. I found on Roedy's site that Sun starts 
counting the Date years at 1970, not on 0000.
Simple modification did it, maybe not sophisticated without all the 
getLocale stuff, but who cares that a person is actually older or 
younger in another timeregion ;-)

for feedback:

  public int getAge(Person person)
  {
    long d = new java.util.Date().getTime();
    long m =person.getDateOfBirth().getTime();
    long l=d-m;
    java.util.Date age = new java.util.Date(l);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
    return (new Integer(formatter.format(age))).intValue()-1970;
  } 	

regards

Pete
0
pietgrijs (11)
5/29/2004 9:39:14 AM
On Sat, 29 May 2004 11:39:14 +0200, Peter Grison
<pietgrijs@hotmeel.com> wrote or quoted :

> I found on Roedy's site that Sun starts 
>counting the Date years at 1970, not on 0000.

It is worse than that. Sometimes Sun start in 1970, sometimes in 1900
and sometimes in 0 (a year that did not exist).


-- 
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming. 
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
0
look-on (4215)
5/29/2004 7:29:58 PM
Roedy Green wrote:
> On Sat, 29 May 2004 11:39:14 +0200, Peter Grison
> <pietgrijs@hotmeel.com> wrote or quoted :
 >
> It is worse than that. Sometimes Sun start in 1970,

Unix does this, the standard was adopted.

> sometimes in 1900

Deprecated Date constructors maybe?  That code was not written by Sun
and is now deprecated.  Are there other examples?

> and sometimes in 0 (a year that did not exist).

What you haven't tried -1 in a java.util.Calendar? :-)

-Paul

0
5/29/2004 9:14:57 PM
"Roedy Green" <look-on@mindprod.com.invalid> wrote in message
news:d3phb0tjqrgqvsm6a2i6160h4ran2evjjp@4ax.com...
> On Sat, 29 May 2004 11:39:14 +0200, Peter Grison
> <pietgrijs@hotmeel.com> wrote or quoted :
>
> > I found on Roedy's site that Sun starts
> >counting the Date years at 1970, not on 0000.
>
> It is worse than that. Sometimes Sun start in 1970, sometimes in 1900
> and sometimes in 0 (a year that did not exist).
>
>

Perl uses 1900.

> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


0
Liz19 (389)
5/30/2004 4:23:41 AM
Peter Grison wrote:
> for feedback:
> 
>  public int getAge(Person person)
>  {
>    long d = new java.util.Date().getTime();
>    long m =person.getDateOfBirth().getTime();
>    long l=d-m;
>    java.util.Date age = new java.util.Date(l);
>    SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
>    return (new Integer(formatter.format(age))).intValue()-1970;

Yuck, what a horrible hack. It's also incorrect in many cases
involving leap years. e.g. when someone is born on February 29th
2000, it gives the age as 1 on February 28th 2001.

A person's age is a *period* of time, not a point of time. The
Date class is inappropriate to represent an age, and using
all kinds of *text formatting* to convert it to what you actually
want just compounds the mistake.

To get it right, you'll have to use GregorianCalendar to get the
numerical year, month and day of the birth date and now, then
subtract the years, and subtract 1 if the current month or day is
smaller than the birthdate.

Dates are *complicated*!
0
brazil (1213)
5/30/2004 1:20:50 PM
On Sun, 30 May 2004 15:20:50 +0200, Michael Borgwardt
<brazil@brazils-animeland.de> wrote or quoted :

>Dates are *complicated*!

For just how complicated, see 
http://mindprod.com/jgloss/calendar.html
and 
http://mindprod.com/jgloss/gotchas.html#DATE


for just how simple they can be see the TestDate class which shows you
how to solve all kinds of date problems using BigDate.

see http://mindprod.com/products.html#DATE

-- 
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming. 
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
0
look-on (4215)
5/30/2004 10:56:22 PM
Reply: