views:

156

answers:

3

How do I take a birthdate entered by a user and turn into milliseconds so that I can calculate how old they would be on a different planet

Here is the code I have so far:

DateFormat df = new SimpleDateFormat("MM dd yyyy");
Date dateBirth = df.parse(birthdate);
Calendar calBirth = new GregorianCalendar();
calBirth.setTime(dateBirth);


Edit 1: Yes I'm looking to get the milliseconds between the user's birthdate and the current time in order to divide that by a planet's days in a year

+7  A: 

dateBirth.getTime() will give you the number of milliseconds since the epoch, if that's what you're looking for?

EDIT -

In order to get the difference between now and the birthday, you can obviously just get now as a Date object, convert that to milliseconds, and subtract - eg now.GetTime() - dateBirth.GetTime().

dsolimano
You beat me to the answer, but I'm leaving mine up since you failed to make any time travel jokes in yours.
jball
What's with all the capitalization of the first letters of methods in a question about Java?
ColinD
@jball, +1 for you because I didn't have the guts to make the joke. @ColinD, that's what happens after I answer a Java question after spending all day programming in C#.
dsolimano
+2  A: 

Not quite sure I fully understand the question, but Calendar has the method right in it...

getTimeInMillis

public long getTimeInMillis()

Gets this Calendar's current time as a long.

Returns: the current time as UTC milliseconds from the epoch.

See Also: getTime(), setTimeInMillis(long)

MarkPowell
+5  A: 
Date d = new Date();
long msSinceBirth = d.getTime() - dateBirth.getTime();

This assumes the user is born in the past. Time travellers will produce negative values for msSinceBirth.

jball
No +1 - time travel joke not up to required standard.
Stephen C
@Stephen, if I could go back in time and write a better one, I would.
jball
Paradoxically, the "edit" button means that you don't need to go back in time to write a better joke :-)
Stephen C
@Stephen, going back in time would be the trivial part. It's the other half of the problem that I can't solve.
jball