Join David Gassner for an in-depth discussion in this video Calculating time spans with Instant and Duration, part of Java SE 8 New Features.
- View Offline
JAVA SC8 includes a complete new API for managing date and time values. The classes that actually hold the data in this new API are all immutable and thread safe. So that means you don't have to worry about passing objects around in a multi threading environment. And if you're using them in parallel streams, everything will always work perfectly. All of the classes in this new API are members of the package java.time. And I'll start with two of the core classes named Instant and Duration.
I'm working in a project named InstantAndDuration. And my main class is named main. And I'll place all this code inside the main method. The first class I'll describe is named instant. I'll type the name of the class and press Ctrl+Space, and choose the class from the java.time package, and it's imported above. An instant object represents an instant on the Java timeline. Just as with the date class which is a part of the old way of managing dates and times, an instance represents a number of milliseconds.
Since the Java epoch time, January 1st 1970. To declare an instant object, I'll declare it with its type, and give it a name of start. And then I'll call a static method of the instant class called now. And this represents the moment on the current machine when the code was called. Then I'll output that value as a string, using standard system output. Starting off with the date in year, month, date format and then the time after the letter T.
Once you have a moment in time, you can use it to calculate a difference between that and another moment in time. So I'll create another instant, which I'll call end. And I'll get it's value from the method now as well. Then I'll use system output, and output that value. Notice that there is a slight difference between the two values, and that's the amount of time it is taking on my system. To process this line of code, that's outputting the start value. If I were to move that line of code down, so I wasn't doing any other processing between the two calls to the now method, the two values would be identical.
Or they might be off by a thousandth of a second. Now, I'll show you how to calculate the difference between these two values. When you compare two instants to each other, you'll get an object called a duration. It's represented by the duration class, which is also a member of Java.time. I'll name this object elapsed. An I'll call a static method of the duration class called between. Notice that it's looking for objects typed as something called Temporal. The Instant class is a sub-class of Temporal.
I'll pass in start and end as my two temporal values. And then I'll output the difference. I'll pass in a literal label of elapsed, and then I'll pass in my variable. That duration object starts with the letter p and then t for time. This is again an ISO formatted value. And then it shows me zero seconds. Well let's see what happens if we toss in a call to the sleep method. I'll place the cursor here between the start and end calls. And I'll use the thread class.
I'll press the period, and then press Ctrl+Space. And I'll dismiss my problems dialog. And then I'll call the sleep method and pass in a value of 1,000. Meaning sleep for one second. The sleep method can throw an error, so I'll use a quick fix, and I'll add a throws declaration to the main methods signature. I'll save and run the code, and I see that my lapse time is now 1.001 seconds. You can never really count on things being exact, it all depends on what's going on, on the processing computer.
Next, I'll take this printline call, and move it back to its original location. So now after I get the start value, I'll be executing a printline command. And I'll be sleeping for one second. And I'll run the code. And now my lapse time is 1.057 seconds. To make this a little bit more readable, I'll add a call to the method of the duration object using elapsed.to millis. That means, get the milliseconds equivalent. And I'll append to that, milliseconds, and I'll run the code.
And now I see, a readable value, of 1,054 milliseconds. So, that's the instant class and the duration class. Two of the core classes, of the new date time API, in Java SE8.
- Installing Java SE 8
- Working with lambda expressions and method references
- Traversing collections with streams
- Calculating timespans with the new DateTime API