Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Luke Wilson
Luke Wilson
2,139 Points

Finding the seconds since you were born in android studio

Hi All,

I know high-level questions can be more tricky but I am really stumped on this one.

I am trying to create an application that takes a date from a Calendar View and then takes the current time since UNIX epoch to calculate the number of seconds that have passed since your birth.

For some reason, when I use: mDatePicker.getDate() I am not returned the number of Milliseconds since UNIX epoch from my selected date like I am led to believe from the android documentation.

I think part of this is because I need to be using an OnDateChangeListener however this only seems to accept in our selected date in Year/Month/Day format rather than Milliseconds since UNIX epoch.

I am using: System.currentTimeMillis(); To get the current time. What is wrong with my approach? How do I get the milliseconds time from the selection on the CalendarView?

1 Answer

J.D. Sandifer
J.D. Sandifer
18,813 Points

From the Java 8 documentation regarding the java.util.Date class:

getDate() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_MONTH)

Also:

getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

Perhaps you could find a way to set one Date object to the calendar/birthday date and one to the current date and just subtract birthdayDate.getTime() from currentDate.getTime().