1z0-808 Oracle Java SE 8 Programmer – Dates and Time
- Section Overview
In this section, we’re going to look at another new API that was added to Java Eight. It’s called the Java Time Library and it’s going to give us classes to work with what it sounds like time, time and date. So we’ll look at local date, local date time, local time, and another class called period. So let’s start working with dates.
- LocalDate
Java Eight gave us a brand new set of classes to work with for dealing with dates and times and durations. And they replaced a library that really wasn’t all that popular. There were a number of reasons that people didn’t like it and this new set of classes and enums and so on addressed a lot of those concerns. So we’re going to look at some of those classes and we’re going to focus on the classes that you’ll find in the Java associate exam. So if you’re taking the associate exam, these are the four you need to know. The local date class, the Local date time, the local time, and finally period. And we’re going to go over the more common methods and discuss how these objects are used so you have a really good understanding of them after the lecture.
However, if you are going to be taking the exam, you’re going to want to go in and study these a little bit more. You can never have enough practice for this exam. So with that said, let’s do some code. Let’s go to a class that I’ve made here, just a real simple class so that we can test some of these different constructs. I made a very simple print method just called P so that I don’t have to keep wasting space on my line with system out print line, just so that it’s just going to be easier for teaching it’s nothing I probably would do in my real world code. I would just type system out print line.
But just wanted you to understand what P was. When you see that in my code, we’re just printing out the object. So let’s start with a local Date. And Local Date represents, as it says, a date, it does not represent a time. So when we’re dealing with a local date, if we try to use any print format specifiers now I’ll show you that in a little bit too. So there’s a way for us to take an object, print it out with some kind of a format that we like. And when we do that, if we accidentally include any format specifiers that would include time, then it will fail because this is just a date. The other thing to keep in mind with all four of these classes is that it is immutable. Just like our strings.
Once a local date is created, you can’t change it. Now there’s plenty of methods that make you think you’re changing it. We’ve got plus weeks, minus days and so on, plus hours, minus months, lots and lots of these methods. What looks like we’re changing the date, but what we’re doing, just like a string, is we’re really creating a brand new local data object. And so I’ll show you some examples of that. But just remember that we are dealing with an immutable object. So we’ve got local Date and we’ll just create one called now and there is no constructor that we’ll be using for these immutable objects.
We will always be using static methods on the class itself. So if you’re taking the exam and you see something like this, that is a gotcha. There is no constructor that we can call using new. That’s for local date, local time, local date time and period. So what we’ll do is we’ll say Local date and we’ll make one that’s really simple. This is local date now. So what I’m doing is I’m taking the date time that’s right now and putting it sorry, just the date and putting it into this variable. Now let’s print it out so you can see what it is. So I’ll just print out now and we’ll run this. And if you look below, this is the default way it’s printing it out so you can see that it is 2016. It’s May 21.
Like I said, if you don’t like how it’s printing this out later, I’ll show you a formatter that we can use and that will change the way that this is printed. What are some other ways that we can create a local date? Well, let’s say I’ve got my hire date and I want to put that in there. One of the ways we can do this is with a method called of. And if you look at down below here, we’ve got a few different of methods that we can use. We can use the year, the month and the day of month as ints. We can also use the year, and then we can use an enumeration called month. And that’s really recommended. I’ll show you how to do that in a minute. Along with the day of month, there’s of the epoch day.
The epoch day is January 1, 1970. It’s how a lot of computers base their time and then of year day. And so that is what year and then what day of year. Those are our options with of. So what I’m going to do is choose this one right here, the second one. And when was I hired? At Intertech. I believe it was 2008. The month was April. There you see, it’s an enum. And the day of month was somewhere around the 21st. All right, so now I’ve got a higher date. And once again, if we just print that out, run that there, you can see the date. So now we’ve got a date that represents April 21, 2008. Once we created dates, how do we change them? Well, there are a number of different methods.
So what if I want to have a local date that is going to be a week from now. What I do is I’d say now plus, and we’ll get to the generic plus where we can just pass in a temporal amount. That’s where we can pass in a period. I’ll show you how to do that in a little bit. But notice we got some nice helper methods here. We’ve got plus days plus months, plus weeks plus years. So if I wanted to add a week, I just passed in the number one. And now if I print that out a week from now and run that, here we go. It was May 21 and now it’s going to be May 28. You can go in the other direction as well. So I can say local date a week ago equals now minus weeks one.
Print that out a week ago and then we see. We’ve got May 14. Now here would be the exam. Got you. We want to find out what a week ago is. So we want to change now. If they do something like this, obviously it can’t print it out if I’m not assigning it to new variable. But so here I say now minus weeks. That doesn’t do anything. Let’s print out now again after we do minus weeks. And you can see that the original was May 21 and the new version is May 21 because we didn’t really change the object. It’s an immutable object. That’s a big got you. So make sure you’re always assigning the value.
If I really wanted to change now, all I’d have to do is say now equals and now when we look at it, now has been changed to May 14 because we didn’t really change the date, we changed the reference and what it’s referring to. And there are other interesting methods that we can use. Like if I want to make sure that my higher date was before now, I could say hire date is before now and that will return a true or false. Let’s print that out. So was my higher date before now? Now obviously we would think it would be, but let’s just run it and it does say true and you can do is before is after. And there are a number of different methods that you can use. So that’s local date. In the next lecture we’re going to look at local time and local date time.
- LocalTime, LocalDateTime, and Period
So the other two types of classes that we have, local time and local daytime are very, very similar. The difference of course, is what type of data they hold. So if I’ve got localtime now equals localtime now. So a lot of the same methods that we’ve seen before and then I’m going to print out now. Of course I do have to comment out this other code since I’m reusing the same variable. But now I’ve got local time now and let’s print it out. And so notice that it’s printing out just the time. It doesn’t print out the date because it’s not a date object, it is a time object. And so what we’re seeing here is we’ve got the time ten, that’s the hours, the minutes, the seconds and then the nanoseconds.
Just as we did with a local date. We can use the of method. So let’s say that I thought the local Time Hired was of and just let’s take a look and see what we can add here. If we go down to this one, the third one down, we can add the hour, the minute, the second and then the nanosecond. We can also just add the hour, minute and second, and we can also add the hour and the minute. There’s a couple of other methods that start with of nano of day, the nano of day and then of second of day. But we’re going to start with this one. Let’s start with what’s probably more common. Not going to deal with nanoseconds, we’ll just deal with the hour, minute and seconds. And let’s say that it was 01:00.
So I’ll say 13, don’t even need that o and o. Now let’s print out Time hired. Run that. And there you can see that it is 1300 or 01:00. Now, if we don’t like that format, we can use a formatter which I’ll show you in the next lecture. And then we’ll do that with all the dates and times as well. We can certainly create new dates based on old dates, just as we did with the local date. So I can say local time an hour from now equals now plus. And notice that we’ve got plus hours plus minutes plus seconds and plus nanos. So an hour from now would be one. And if I print out an hour from now, we’d see that it went from 1034 to 1134. We can go in the opposite direction just as we did before.
Local time an hour earlier equals now dot minus hours. And we’ll print that out and sure enough, it’s 935. There are comparison methods as well. So if I do the same thing I did with local date, we could say that the time hired is after is before, let’s say is after now and that’ll give us a boolean. So we would just wrap that in a system out print line just to see what the value is. And in this case it’s true. The time I was hired was 01:00 p. m. The time now is approximately 1035. And so sure Time Hired is after now. Then we’ve got the local date time. Just give us a little more space here. So local Date time now equals local Date Time now p now printed out. Let’s comment this other stuff and let’s put a semicolon at the end and run it.
And so now you can see that we have both the date and a timestamp and the timestamp is eliminated here with the T. So we’ve got the date, we’ve got a T and then we have our timestamp. And now we’ve got a lot of options of what we can do with the of method. So local Date Time Hired equals local date time of and now we can just choose one that has a whole bunch of stuff. So what was the year? 2008. The month was April the day of the month the 21st. The hours 1300 the minute. And we’ll put the second as well. And now let’s print out Time Hired and run that. And here it’s printing out 2008, April 21 at 01:00. We have comparison methods just like we have with local Date local time. Let’s just take a look. System out print line.
Now is before, time hired is false and that’s what we would expect. Now there’s one more class that I want to share in this lecture and that’s called period. So period, just like everything else we’ve seen in this section, is immutable. So when I create it, I have to make sure that I’m assigning any changes or what appears to be changes to a reference variable. Also we can’t use a constructor, we have to use a static method. So in this case I’m going to say rather than saying new period, which would be wrong, it’s not legal. What I’ll do is I’ll say period of and we could do of days of, months of weeks of years. But I’m going to just do the regular of method which will allow me to put in years, months and days.
So I’m going to say three years, five months and twelve days. And this is a generic period. This isn’t a specific date time. This is something that we can use to add to another date or time. We could use it to subtract. In fact, let me show you. We’ve got our local date time now let’s subtract this period from it. So what I’m going to do is reassign now. I’m going to say now equals and we will then say now minus. And we’ll just pass in that period that we just created. Let’s print it out, see what the new value of now is. And it did exactly what it should. This was our now date time over here. Here’s the three years, five months and twelve days subtracted. Notice that it wasn’t just maths, just subtracting three years.
Because if that was the case, it would have been 2016, would have been then 2013. But it actually respects the local calendar, the the actual local date that you’re working in, hence the name local date time. And we could have gone the other direction. We could have said now equals now plus and passed in the period. Period also has some nice comparison methods. So let’s say that I want to create and find out what the duration is between, oh, let’s comment out all this stuff that we have so far, and I’m just going to uncomment out this local date now line and the higher date, get both of those back. So now we’ve got our local date now and higher date. And let’s say I wanted to get a period that’s between the two dates.
So what I could do is I could say period between and I could say the start date was the higher date and the now is the end date. That’s an exclusive date. So it’s going up to now, and we’ll want to assign that to a period called period, and we could even print that out. So I’ll just say P period and it’ll look a little funky. Let’s see what we got. So what it’s telling me is that the period is eight years and one month. That’s the difference between how long I’ve been hired at intertech. So those are the basics of local date, local time, local date time and period. What we’re going to do now is look at how we can format the dates and times and so on into something that we’d like that’ll happen in the next lecture.
- DateTimeFormatter
There’s one last class that I want to talk about before we finish the section, and that’s on the Date time formatter. Now, although the exam objectives don’t state explicitly that you have to know a lot about that class, when we go into things like Local Date, we’ll see that there is a method here called format, and it takes a date time formatter. So it’s a good idea to know about this just in case they asked any of the questions. And really, just for coding, you’re going to want to know this. So I think it’s important to cover. Also, I’m going to want you to check out the API documentation, the Java doc on this class as well, just like the other ones. Local date? Local time. Local date, time and period.
And the reason is that it’s got a lot of great information about different constants that it has that we can use for formatting dates and what they’ll look like. And then it goes through and tells you all the date format specifiers that we can use. So we have different symbols that we can add to a pattern, and then that pattern can be used to change what that date time looks like. So they give an example here where they’re using DMMU. And that will take our date object, which would be in this case 2011 Twelve Three, and change that into 3 December 2011. So let’s write some code and take a look and see how the date time formatter can be used. Some of the more common use cases jump to our code and we’ve got a date.
Let’s run this right now. It’s printing out 2016 521. Let’s look at some of our options. First of all, we’ll create the date time formatter, and I’ll just call this Dtf. And we’re not going to say new Date time formatter. We’re going to use the static methods. So datetime formatter and I’m going to use this method called Of Localized Date. Notice that there’s an of localized date there’s an of localized time and an of localized date time. So make sure that you’re using the right one for the type of object that you’re working with. If I happen to choose Of Localized Time and try to apply this formatter to the local Date, it will fail. In fact, let’s do that. Let’s see it fail.
This is an exam type of Gotcha that you want to watch out for. So here I can say it’s asking you for a format style. Format Style has just a few options. We’ve got full, long, medium and short. I will say full. And what we would do is we would print out the date. So now and we use a method on our local date called Formatter. And so if I try to use this method or this object that I’ve just created, the Dtf, let’s see what happens. First of all, no compile errors. Let’s run it. We get a runtime exception and it’s just telling me unsupported temporal type execution, unsupported field clock hour of amp M. So it was trying to add some time information because we said it’s a localized time and we don’t have time, we just have a date, totally different objects.
So keep that in mind with the exam, make sure everything is matching that you have the right of localized method being called. So what do we want? We want of localized date. That’s the method we want. Rerun this. And now you can see the difference between the two. Now we’ve got a full, which is Saturday, May 21, 2016. If we do a long, that will just get rid of the Saturday. So now we have May 21. If we do a medium, that one is exactly the same as long. Now in some locales it might not be, but for our locale or for my locale, here it is. And then we’ve got short if we run that. Now we’ve got the 521 16. So now let’s look at using patterns with daytime formatters.
Once again, I’ll create my date time formatter variable, call it my pattern formatter. We’ll say date time formatter using a static method like everything we’ve seen in this section so far, there are no constructors that you’re going to use. You’re going to use the static methods. In fact, there are no constructors you can use. Okay, so if you see on the exam that they say new date time formatter, that’s a got you. That will not compile. So we’ve got our date time formatter. There’s some constants, we could look at some of those constants, but as I mentioned, they’re all shown in that documentation. Take a look. You can see what those formatters look like.
What we’re going to do is of pattern. So I’m just going to do an of pattern and then the pattern that we can do, that’s what’s listed in that documentation as well, what all the different letters mean. But let’s just put something here. I could say I want to have a pattern of I’m going to do the four digit year and then I’m going to put a slash and then I’m going to just swap this around just so you can see things swapped. I’m going to put the day as two digits and then I’m going to put a one digit month. Unless of course it’s eleven or twelve, 1011 or twelve, it’ll be a one digit. So that’s going to be my pattern. I need to put that in as a string. Running out of space here on this line. There we go. All right, now I’ve got my pattern formatter.
Let’s print out now using that format. There’s my pattern formatter and let’s run it. And so now you can see that it swapped it out. So now it’s the year, then the day, and then a one digit month. If I wanted two digit months, I could put another M there and run this. And now you can see it’s. Zero five. If I wanted to have any text, maybe I want to say year. Let’s say I want to say the day at year. So I’d say DD, and then I put two single quotes in the year of and close that. And then I would put the year, save that, run it. And now you can say as 21 in the year of 2016. And I actually have too many spaces here, I could get rid of that space, make it a little cleaner. 21 in the year of 2016.
So any kind of literal text that you want just goes within single quotes. Otherwise, use the format specifiers that are documented on that Java doc page. In any case, anytime we want to use the formatter, we take our object, we say now and then format. I did show you that you have to be careful with the of localized methods, choosing the right ones. It matters as well with the patterns. Let me just show you. We’ll create a local time. Time equals local time now. And we’ll format that. We’ll say, let’s create our date time formatter. Well, actually, you know what? I’ll just pass in the pattern formatter that we made here. There’s nothing about time in here. This is all date stuff. So I’ve got that little, say, PNow or timeTime format.
We use that same pattern formatter. Now in terms of it compiling, sure, it’s going to compile we’re passing it a date time format. It’s not until runtime that it realizes that it can’t apply it. And so when we run it, we get this exception unsupported field day of month. So it’s telling us right there, we got a problem. You’re trying to apply a pattern that would be applied to a local date. You’re applying it to a local time, and so it fails at runtime. So that’s your introduction to the Java time library. As I mentioned before, if you’re going to be taking the exam, spend a little bit of time playing around with patterns, date time formatters, create local dates, local date time, local time and period objects. And spend a little bit of time in the documentation, the Java docs for those classes.
- Dates and Time Lab
The Java Timelab has two parts. The first part you’re going to do right now, and that part is you’re going to make another lambda and you’re going to determine whether or not the rush job should occur based on a date. So if it’s been longer than a month and it still hasn’t gotten out the door, then that’s going to become a rush job. The second part is going to be once you’re finished with the course and what you’ll do is you’ll go in and you’ll replace the my day that we’ve been working with with the local date. And that’s going to hit the entire application. So there’ll be a lot of rewriting that you’ll have to do. The instructions for this lab can be found in Lab 13 date Time PDF, and it’s in the resources for this lecture.