Cleaning Up the Date and Time8:22 with Ken Alger
We have valid data in our app, but not all of it is useful in its current form. Let's see how to convert a UNIX timestamp into a more meaningful time string.
The data model for our app is coming along quite nicely. 0:00 We've accomplished a lot those far, with parsing 0:04 the days on formatted weather data, and getting it into our current weather model. 0:06 However, if you recall from our time value, it was a bit unwieldly. 0:11 Lets take another look at the documentation for our API, 0:17 and see what we can discover. 0:21 If we go back to the dark sky documentation, and 0:23 click on the response format link, We see a list of the various object, 0:26 and their properties that are returned from the API call. 0:33 We're working in the currently object. 0:37 So let's click on this data point link here for additional information. 0:39 If we scroll down a bit, we see the time property. 0:43 It says that this is the Unix time where this data point begins. 0:53 What is UNIX time though? 0:58 UNIX time is a system for describing time, you find is the number of seconds 1:00 that have elapsed since coordinated universal time, UTC. 1:04 UTC started on Thursday, January 1st, 1970. 1:09 You may also hear this referred to, as the UNIX Epoch. 1:14 I know. 1:18 Leave it to developers to represent time in a crazy fashion. 1:18 But, this is a very standard way of handling time on UNIX systems. 1:22 Unfortunately, there are several built-in methods to handle this format. 1:27 Our mock-up calls for the time to be displayed in an easily readable format. 1:34 Right now however, our time is in a huge number of seconds. 1:38 We'll need to convert that into a more human readable format, and 1:42 put that time string into a text view. 1:46 Notice too that we're just wanting the time portion of the time since 1:48 the UNIX Epoch, we're not concerned about the date. 1:52 Let's add a new method to the current weather class called, git formatted time. 1:55 We can use the simple date format class to help us out. 1:59 Looking through the documentation for a simple date format, we see that it can 2:05 take in date and time values in different formats, including UNIX time. 2:09 It can then convert them into low-cal specific time. 2:14 This is exactly what we're looking to do. 2:18 The way this works, is that we create and configure a formatter object. 2:20 Then use it to format a date value. 2:24 There's a list here of all the different format symbols we can use. 2:27 Recover more mock-up, if we want the time to be in the format of hours 2:30 without a leading zero if it's a single digit. 2:35 Then minutes, and then AM or PM. 2:37 Let's head back to current weather, and create our formatter, and 2:41 set our time value. 2:44 So back here in current weather, 2:54 let's add a git formatted time method, under git time. 2:56 Public, will return a string called, getFormattedTime. 3:06 Inside here, we'll put SimpleDate format to use and build our formatter. 3:17 SimpleDate format Call it formatter. 3:23 Be a new SimpleDateFormat, hours without a leading zero, minutes, 3:30 including that leading zero, and then a, which will give us our AM or PM. 3:38 We're getting the time value from the forecast, from the location we specified. 3:44 In our case, that's Alcatraz Island, 3:49 which resides in the San Francisco Bay of California, in the United States. 3:51 And, it's in the Pacific Time Zone. 3:55 Therefore, we need to set the time appropriately with the time zone in mind. 3:58 So we can do formatter, SetTimeZone. 4:04 For our perimeter in here, we can use a helper from the java time zone class. 4:11 TimeZone, getTimeZone, in here, we need a time zone code as a string. 4:18 Seems like we've seen that somewhere before. 4:26 Let's look at getCurrentDetails over in main activity. 4:29 There it is. 4:34 We're logging it here in getCurrentDetails. 4:35 Let's add it to our data model, so we can use it. 4:38 Make it a private, String, TimeZone. 4:45 Now back in main activity, 4:51 we can set our timezone using the timezone variable we already have. 4:53 CurrentWeather, setTimeZone, 5:02 Passing time zone. 5:11 And it looks like we need to go back, and generate our getter and setter for that. 5:13 So Code, Generate, Getter and 5:19 Setter for timezone, great. 5:24 No more error there, if we go back to current weather. 5:32 And we can use that timezone, In here. 5:38 Our formatter is ready. 5:47 We just need to return it. 5:49 Return, formatter.format. 5:54 Now here's the tricky part. 6:01 Working with dates can be a challenge, it can require a lot of trial and 6:03 error in readings or documentation. 6:08 We need to pass in a Java Date object here, so 6:10 let's look at the Date documentation. 6:12 We look here in the Constructor Summary, 6:18 there's a constructor here that takes a time value as a long. 6:20 Nice. 6:25 That's what we're already have. 6:26 Well, wait a second. 6:28 It says here that it needs to be in milliseconds. 6:30 Our time value is currently in seconds. 6:33 So we need to make that conversion first. 6:35 This can be super frustrating, and cause lots of headaches, if you don't 6:37 realize that the time needs to be in milliseconds, and you pass in seconds. 6:42 Fortunately, it's an easy fix. 6:46 We simply need to multiply our seconds by 1,000, 6:49 since there are 1,000 milliseconds in a second. 6:52 So we'll do Date dateTime, do our import. 7:00 We want the java.util. 7:05 Date (time * 1000), 7:09 I forgot an equal sign, 7:13 that would be good to have. 7:17 Now we can pass this into our time string formatter. 7:24 All right, let's test this out by calling this new method, and 7:30 logging it in main activity in the getCurrentDetails method. 7:34 So main activity, getCurrentDetails, down here. 7:40 Log.d, our TAG, currentWeather.getFormattedTime. 7:44 And let's remove this breakpoint, since we won't be using a debugger at the moment. 7:55 And we'll run our app. 7:59 And look in the Logcat. 8:10 And there it is, our formatted time, very cool, this will be ready for us, 8:14 when we get to working on the user interface. 8:19
You need to sign up for Treehouse in order to download course files.Sign up