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 Build a Weather App (2015) Working with JSON Cleaning Up the Date and Time

imran esmail
imran esmail
1,464 Points

Need help understanding getFormattedTime

Correct me if I am wrong but this is how I understand the following line of code:

Date dateTime = new Date(getTime() * 1000);

The getTime() value should be 1427756168, but it's not. Looking at it's value while debugging (using alt + F8), I found that it is 1427757565.

Can someone please explain what is going on here ?

2 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hello,

It looks like you have nothing to worry about. The getTime() method returns the number of seconds since Jan 1, 1970 GMT. The value you are getting from the video is the number of seconds since that date when that video was made. The value you are now getting is a bit larger, because you're watching it later (that is to say, the number of seconds since Jan 1, 1970 has increased).

Let me know if you would like more of an explanation.

imran esmail
imran esmail
1,464 Points

Hi Jon,

Thanks for the explanation but to my understanding, we are setting the "time" attribute in the MainActivity.java using a setter method. We get the value from JSON. Now, the JSON value for time is still 1427756168. So now, when I want to access the time value in CurrentWeather.java, shouldn't I be getting the same value ? I mean, it's just setting a value in one class and accessing it in another class.

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Are you sure the JSON value for time is still 1427756168?

Go ahead and instead of using your app to get the JSON response, enter the URL into your browser and check the value there. The time value will change every time you retrieve the data, regardless of you using your app, or a browser.

imran esmail
imran esmail
1,464 Points

You are right, thanks.