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 di开发者_StackOverflow社区vide that by a planet's days in a year
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()
.
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
.
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)
精彩评论