Date, date date date....

P

Peter Grison

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
 
L

leov

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
 
A

Andy Hill

Peter Grison said:
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.
 
L

Liz

Peter Grison said:
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()
}
 
P

Peter Grison

Peter said:
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
 
R

Roedy Green

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

P.Hill

Roedy said:
On Sat, 29 May 2004 11:39:14 +0200, Peter Grison

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
 
M

Michael Borgwardt

Peter said:
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*!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top