Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Date, date date date....

Reply
Thread Tools

Date, date date date....

 
 
Peter Grison
Guest
Posts: n/a
 
      05-28-2004
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
 
Reply With Quote
 
 
 
 
leov
Guest
Posts: n/a
 
      05-28-2004
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
 
Reply With Quote
 
 
 
 
Liz
Guest
Posts: n/a
 
      05-28-2004

"Peter Grison" <(E-Mail Removed)> wrote in message
news:c98f4c$sbb$(E-Mail Removed)1.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



 
Reply With Quote
 
Andy Hill
Guest
Posts: n/a
 
      05-28-2004
Peter Grison <(E-Mail Removed)> 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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-29-2004
On Sat, 29 May 2004 00:39:03 +0200, Peter Grison
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      05-29-2004

"Peter Grison" <(E-Mail Removed)> wrote in message
news:c98f4c$sbb$(E-Mail Removed)1.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()
}


 
Reply With Quote
 
Peter Grison
Guest
Posts: n/a
 
      05-29-2004
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-29-2004
On Sat, 29 May 2004 11:39:14 +0200, Peter Grison
<(E-Mail Removed)> 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.
 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      05-29-2004
Roedy Green wrote:
> On Sat, 29 May 2004 11:39:14 +0200, Peter Grison
> <(E-Mail Removed)> 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

 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      05-30-2004

"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 29 May 2004 11:39:14 +0200, Peter Grison
> <(E-Mail Removed)> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting Time or Date Portions of a Date data type Kevin Buchan ASP .Net 2 12-01-2003 05:45 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP General 11 11-08-2003 11:24 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments