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

Julie Pham
Julie Pham
12,290 Points

formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));

Can anyone please explain this line of code: formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone())); ? Thanks!

1 Answer

Scott Junner
Scott Junner
9,010 Points

formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));

Yeah. That's a fairly wild line of code to look at. I can understand the confusion. Sorry it's taken so long for you to get an answer.

Going back one step, you have a line of code which declares "formatter" as a new SimpleDateFormat object.

The SimpleDateFormat object has some methods. One of those methods is setTimeZone. So to call that method you write

formatter.setTimeZone();

Easy enough. BUT... it would be nice at this point to simply pass a String to seTimeZone(). Unfortunately it's not that simple. So the following is wrong.

formatter.setTimeZone("America/Los_Angeles");

Instead this method requires you to pass it a TimeZone object. Fortunately for you and I the TimeZone object has a method which can convert a String into something it can use. That method is getTimeZone(). Like this...

formatter.setTimeZone(TimeZone.getTimeZone());

TimeZone.getTimeZone() is a method which can take a String, parse it, and turn it into a TimeZone object. It does have to be an actual time zone. For example you couldn't do this...

formatter.setTimeZone(TimeZone.getTimeZone("guess where"));

However, it gets a little confusing when you read what's inside the parenthesis of that method as you have written it (as presented in the video). So to help, the following line would be perfectly acceptable...

formatter.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

BUT...this will cause problems down the track. This is a hard coded example. We want to fetch the String representation of the timezone programatically. We need to be able to get the time zone for each location where the app is being used. Not just one location hard coded into the app.

Within our CurrentWeather class, the teacher has suggested we declare a member variable to help handle this. Called mTimeZone. And within out CurrentWeather class we have setter and getter methods for mTimeZone. Unfortunately our getter method in this example is ALSO getTimeZone(). Which is silly, and the teacher should not have named it that.

My advice would be to refactor the name of mTimeZone and it's getter and setter methods in the CurrentWeather class to mCurrentTimeZone. That way it will read as follows and be less confusing.

formatter.setTimeZone(TimeZone.getTimeZone(getCurrentTimeZone()));

Remembering too that the following line is also perfectly acceptable

formatter.setTimeZone(TimeZone.getTimeZone(mTimeZone));