Search code examples
javagregorian-calendar

Converting a birthdate into milliseconds


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


Solution

  • 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().