File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes birthdate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "birthdate" Watch "birthdate" New topic
Author

birthdate

john daniels
Greenhorn

Joined: Dec 11, 2006
Posts: 13
I need to know how to prompt the user for their birth date and calculate and display how many days old they are. Can anyone help me out, because i am completely stuck, thanks in advance
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
I did some searching to see if there were any pre-made classes you could use, and it turns out there are some classes that are made just for such.

Gdate and Jdate. You can find them right here

It's actually an assignment in the Cattle Drive.


The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If this is homework, then you are probably expected to write your own code for this, so third-party packages might not be the best plan.

What do you have so far, and where are you stuck? Can you write the code that prompts the user for input, and get that input into your program?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
john daniels
Greenhorn

Joined: Dec 11, 2006
Posts: 13
actually it is in preparation for a test, and he said there would be something like that on the test, so i am trying to figure out how to do it. i dont have a very good teacher, because he teaches us practically nothing. I basically just need this for reference purposes.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by john doah:
actually it is in preparation for a test, and he said there would be something like that on the test, so i am trying to figure out how to do it...

In that case, you should definitely get some working code together. What do you have so far?
john daniels
Greenhorn

Joined: Dec 11, 2006
Posts: 13
i dont have anything on this one yet, i have spent so much time getting the others together, and this is my last one. which is why i turned to my last resort, online forums. I understand the concept behind it, i just dont understand how to write the actual code, like i know i will probably have to use a loop, having every 4th year add and extra day because of leap years, and then the whole JDate and GDate thing, but other than that, I am stumped.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by john doah:
...i will probably have to use a loop, having every 4th year add and extra day because of leap years...

That would be one approach.

Using java.util.Date would be an alternative. But if you've never used this before, it can be a bit intimidating to dive into (especially if you're preparing for a test and haven't worked with this in class). So I'm not sure how complex of a solution you're looking for. What level class is this?
john daniels
Greenhorn

Joined: Dec 11, 2006
Posts: 13
It is an entry level computer programming class using Java, but our teacher apparently thinks we understand more complex/difficult things than we actually do. I just really need a good grade in this class, which is why i am trying so hard to find a solution, the problem is finding one, once i find one, i can generally figure it out myself and understand it, but the problem is getting to that point.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hmmm... I might be wrong here, but if you haven't worked with Date or Calendar classes before, then I guess I would recommend working on your loop approach -- so you know you have something workable that you can demonstrate on a test.
john daniels
Greenhorn

Joined: Dec 11, 2006
Posts: 13
But the thing is that I don't know how to write it. He is the type that as long as we can find a solution he doesn't care where we get it from.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by john doah:
But the thing is that I don't know how to write it...

There are lots of ways to approach this, so where exactly are you stuck? You need to post some code.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Originally posted by marc weber:
Hmmm... I might be wrong here, but if you haven't worked with Date or Calendar classes before, then I guess I would recommend working on your loop approach -- so you know you have something workable that you can demonstrate on a test.


I'd say this would be an ideal time to dive in and do some research to expand your knowledge...
You learn by doing that, not by slavishly copying things that are spoonfed to you by others (whether teachers or helpful forum members).


42
MaryT Tsele
Greenhorn

Joined: Oct 30, 2006
Posts: 8
I agree with Jeroen, the only way you can learn is to attempt doing it. Write something so that we know exactly where you need help. Writting you code is not going to get you any where. So please try and start by writing code that will ask the user for imput even if is not date of birth, it can be a number,may be once you start you wont be that stuck and we will defenately know where to help.
[ December 12, 2006: Message edited by: MaryT Tsele ]
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

To get you started here are some of the basic concepts:
1.) The java.util.Date class in Java is not a date in the sense that the word is commonly used (i.e. 12th of December 2006). It is simply the number of milliseconds between Jan 1, 1970 and now.
2.) The java.text.DateFormat class will format and parse from a string to a java.util.Date and vice versa.
3.) The java.util.Calendar class and its derivations (usually java.util.GregorianCalendar if you are in a western country) are the things that know about a calendar. A calendar maps an instant in time (like the number of milliseconds since 1/1/1970) to some standard enumeration system and are a surprisingly complex subject. Java does attempt to deal with that complexity (which is why Java date manipulation can be complicated). You probably don't need calendars for what you are doing.

So...
You now can figure out how to turn a String into a Date.
You know (or can figure out) how many milliseconds are in a day.
That should be enough.
vinay varanasi
Greenhorn

Joined: Apr 29, 2004
Posts: 10



EDIT by mw: Left enough of the code for a "hint," but removed solution.
[ December 12, 2006: Message edited by: marc weber ]

Vinay V
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

vinay,

Welcome to JavaRanch! Thanks for you post, but in the beginners forum, we try to encourage posters to work out their own solutions. Pointing them in the right direction is great, but giving them the complete code is usually not helpful (especially when they haven't posted any of their own code).

-Marc
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
In relation to his question, is it good to store Date fields as String or better to use the Date or Calendar class?

thanks
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
[Cyrus]: is it good to store Date fields as String or better to use the Date or Calendar class?

You should avoid storing dates in String objects and use a Date or Calendar instead.

Parsing a date from a String involves a certain amount of guesswork and can be error prone even with a helper class like DateFormat.

If you ever need the date as a String, you can always generate a String in the proper format.
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

FWIW and IMHO, always store dates as long (integer) values.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Chris Beckey:
FWIW and IMHO, always store dates as long (integer) values.

Why?
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

I probably should have explained that as "store dates as system time in GMT/UCT" (a long).
i.e. one of these kinds of dates:
Date getTime()
System.currentTimeMillis()
DateFormat parse() method.

This just seems to make the math easier when doing any kind of date calculation.
In particular, don't store in local time or using a Calendar.

BTW: The following code is a illustration of the difficulties in interpreting calendar string values. The output follows the code, but the code is self contained so its easy to verify the result. This will work in US timezones only, 'cause it is specific to the shift from DST to ST.


Output is:
29Oct2006:01:10:00
29Oct2006:01:10:00

The date '1162102200000L' is within the "extra hour" on DST/ST boundary (i.e. between 1AM and 2AM on the last Sunday in October). The delta '3600000L' is exactly 1 hour in milliseconds.
Doing date math on Strings would require taking stuff like this into account.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: birthdate