943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 620
  • Java RSS
Aug 29th, 2009
0

converting individual date entry to long

Expand Post »
i just dont know how to convert this.

i have three integers for month, date and year. year is a two digit input.

i want to convert these three into one variable type long.. but i dont know how to do it.

i tried adding the three integers, but i get weird results.

i want to do this because i want to compare user input date into the current date. [actually, it's an age calculator.]

waah, help would be greatly appreciated.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 29th, 2009
0

Re: converting individual date entry to long

You should use the GregorianCalendar class, which implements the Calendar methods, which include setting a date from yy,mm,dd, comparing dates etc.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Aug 29th, 2009
0

Re: converting individual date entry to long

i'll research about that. thanks.

but is it possible to do this without using that class?
Last edited by scias23; Aug 29th, 2009 at 1:34 pm.
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 29th, 2009
0

Re: converting individual date entry to long

Click to Expand / Collapse  Quote originally posted by scias23 ...
... is it possible to do this without using that class?
Yes, in fact you can code it in binary machine code if you want. But why? Someone has gone to a great deal of trouble to write and test a class that does exactly what you want - why re-invent the wheel?
IMHO a good Java programmer isn't just soneone who knows the language, it'ssomeone who knows and uses the API.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Aug 30th, 2009
0

Re: converting individual date entry to long

hmm what method in the gregoriancalendar class should i use to compare user input date with the current date?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 30th, 2009
0

Re: converting individual date entry to long

You can get a Calendar instance set to the current date/time with
public static Calendar getInstance()
You can compare two instances with
public int compareTo(Calendar anotherCalendar)

nb: most of the methods you need are defined in the Calendar abstract superclass, not in GregorianCalendar
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Aug 30th, 2009
0

Re: converting individual date entry to long

i can't get the comparison to work.

java Syntax (Toggle Plain Text)
  1. birthDate.set(yyyy, mm, dd);
  2.  
  3. age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
  4.  
  5. if(currentDate.compareTo(new GregorianCalendar(currentDate.get(Calendar.YEAR),
  6. birthDate.get(Calendar.MONTH),
  7. birthDate.get(Calendar.DAY_OF_MONTH))) < 0) {
  8. age--;
  9. }

for example, if i enter 1-22-1991, this returns 18.
if i enter 9-23-1991, this returns 17.
BUT if i enter 8-10-1991, this returns 17!
ALSO if i enter 8-9-1991, this returns 17!

i just can't fugure out what's wrong.
help.
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java and vc++ linking
Next Thread in Java Forum Timeline: Motion targetting in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC