converting individual date entry to long
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.
scias23
Junior Poster in Training
69 posts since Jan 2009
Reputation Points: 11
Solved Threads: 0
You should use the GregorianCalendar class, which implements the Calendar methods, which include setting a date from yy,mm,dd, comparing dates etc.
JamesCherrill
Posting Genius
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
i'll research about that. thanks.
but is it possible to do this without using that class?
scias23
Junior Poster in Training
69 posts since Jan 2009
Reputation Points: 11
Solved Threads: 0
... 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 knowsand uses the API.
JamesCherrill
Posting Genius
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
hmm what method in the gregoriancalendar class should i use to compare user input date with the current date?
scias23
Junior Poster in Training
69 posts since Jan 2009
Reputation Points: 11
Solved Threads: 0
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
JamesCherrill
Posting Genius
6,337 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,070
i can't get the comparison to work.
birthDate.set(yyyy, mm, dd);
age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
if(currentDate.compareTo(new GregorianCalendar(currentDate.get(Calendar.YEAR),
birthDate.get(Calendar.MONTH),
birthDate.get(Calendar.DAY_OF_MONTH))) < 0) {
age--;
}
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.
scias23
Junior Poster in Training
69 posts since Jan 2009
Reputation Points: 11
Solved Threads: 0