• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

birthdate

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
john daniels
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



EDIT by mw: Left enough of the code for a "hint," but removed solution.
[ December 12, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In relation to his question, is it good to store Date fields as String or better to use the Date or Calendar class?

thanks
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FWIW and IMHO, always store dates as long (integer) values.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Why?
 
Chris Beckey
Ranch Hand
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic