Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will learn about how to make use of the new Java Time libraries to work with time
zones.
Until now we have seen different examples of date and time, but all of the examples are local date
and time.
Java eight introduced some zone specific date time libraries that will be covering in this tutorial,
such as the zoned date, time zone ID and zone offset.
Let's talk about the zoned date time.
First, it represents the date and time with its associated time zone and offset.
Here is an example of the output of the zoned date time instance.
In general, time zones are categorized by zone offset and zone ID.
If you take a look at the example here, it has the time along with the zone offset and the zone ID
zone offset here is nothing but -0 five colon zero zero zone offset represents the offset time from
the UTC time.
The example here is from my local machine time.
It has the offset value of -five hours from the UTC time.
What is the zone ID?
Zone ID.
This is another unique identifier which can be used to determine the zone to which the time belongs
to.
Let's go ahead and explore the time zones using some examples.
I'm going to go back to the IntelliJ.
There we go.
Let me close this.
I'm going to create a class called zoned Date time Example.
Be zoned date time Example.
Wanna to add the public static void main method here.
There you go.
Gonna put this in presentation mode.
Now.
I'm going to create a zoned date time instance.
How can we create it?
We're going to use the zoned date time class dot now.
And this is going to return me an instance of zoned datetime.
I'm going to give it a name.
Here we go.
Let's print this and check the result.
Monoprint.
Zoned date and time.
We run.
This is going to give me.
The date time.
Along with that, it gave me the offset and the time zone ID to which it belongs to.
How can we get this offset value and the zone ID value from the zoned date time instance?
It has some handy methods in it.
The first thing is I'm going to get.
Zone.
Offset.
So this zoned daytime instance has a method called get offset.
If you give that value, if you call that method, it is going to give you the offset time.
Though the officer time is returned as minus five.
Basically, this gives me my local times zone information and my local time is -five hours from the
UTC time.
Now how can we get this zone ID out of it?
Because we have seen different examples of getting the date, time, year, month, date and all those
things in the prior tutorials.
Now we are going to focus mainly on the zone related information.
How can we get the zone ID?
Again?
It has a handy method called get zone id using which I'm going to give zone date time dot.
If we give get zone, it's going to give you the zone ID.
If you go to the method, it gives you the zone ID as a response.
More on this example and check the result.
To check the result.
It gave you the zone ID as America slash Chicago.
Now let's take a look at what are the different zones it supports.
Right.
So how can we check?
What are the list of available zone IDs?
There is a handy method called.
We have to use the zone ID class dot, get available zone IDs.
I'm just going to print this one.
Available zones.
So this is going to give you the list of zones that this Java API supports.
I'm going to run this.
It's going to give you a lot of zones, actually.
If we take a look at it, it print it printed a lot of information and it is not in a readable format.
What I'm going to do, I'm going to make use of the Frames library to print this data in a readable
format.
Get available zone IDs, dot frame, and then we're going to make use of the for each method, which
is part of the.
Dream, and then I'm going to give.
John.
The Forejt takes in a consumer, right?
And then shuts out.
Zone.
Now I'm going to run this example.
It is going to print the result in a readable format.
There you go.
So these are the different zones it supports.
It's a lot of zones.
I believe the number is 600.
Let's go ahead and check that.
Also, what I'm going to do, I'm going to come on this piece of code.
Let's check how many zones it supports.
I'm going to give number of zones.
And it's going to be zone ID dot get available zone IDs, dot not stream.
It's going to be size.
So this is going to return you a set.
And then we are calling the size method in that set.
So the total number of zones it supports is 600.
Now, in this tutorial, what I'm going to do, I'm going to show you a demo of how to get different
time zones in America.
So basically in America there are four different time zones.
One is east.
DST and CST change this order a little bit.
Going to be.
Este este este and how do you get the time?
So I'm going to give.
Chicago, because Chicago, if you take a look at it, I'm I'm in the zone.
America, Chicago will give you the time.
And then we are going to do zoned date time dot.
Now in here, the now is going to give you the local machines, right?
But I want to pass a zone ID as part of it.
So if you take a look at the now, now has three different versions.
We looked at the now just now method without any parameters.
This is going to give you that machines time zone.
Now we are going to make use of the now method which accepts the zone ID as an input.
So if you go here, zone ID dot off, you pass the string, right?
The string is going to be America slash.
Chicago.
So this will give you the time.
Of the.
So if you take a look at it, it gives it gave you the time of the day and the time is 609.
Now what I want I want the Eastern Time zones, current time for the Eastern Time zone.
I'm going to use Detroit.
Applied.
Going to be.
It right now.
Let's take a look at the time difference here.
Detroit is one hour ahead of CST.
If you take a look out here, it printed the time as 709 and the time is.
6 or 9, to name this one as ist.
Now what I'm going to do, I want to check the Pacific Standard time.
Ist going to be L.A.
I want to keep this colon in sync so that it will be easy to read.
I'm going to give Los Angeles.
Right.
They give the value, right.
Let's go and check this by running this example.
Run this example in Los Angeles.
The current time is.
There is a current time.
The current time is zero four, which is two hours before the CST.
The time is six and the time is seven and time is zero four, which is two hours before the time now.
I'm going to print the time.
Right.
And it's going to be Denver.
Oh.
And I'm going to pass this value as Denver.
Let's go ahead and run this.
Gave you the value as zero five colon 1111 I'm going to use as a benchmark here because since I'm on,
it is easy for me to compare.
If you take a look at it, the time is an hour ahead, an hour before the time.
So using the zoned date time class, you can get the times of that particular time zone.
Time zones, local date and time.
So if you take a look at it.
This one gave you the time of cst's local time.
This one gave you the time of its local time.
This one gave you the time of its local time and this one gave you the time of its local time.
This is advantage of its zoned date time class.
This was never possible with local date and time because it was able to give you only the time of that
machine.
Actually, whenever you call local daytime dot now it is going to give you that machine's local date
and time.
Now we have seen the zone Datetime.now, which takes in the zone ID as an input.
Right?
There is another version which takes in.
Zone date and time dot now, which takes in a clock.
Right?
Let's go ahead and explore that one too.
So I'm going to pass the clock dot system and then I'm going to pass the.
Time zone of this.
This is going to give me again the zoned date and time of this particular zone.
But if you take a look at the instance, it took clock as an input, right?
So I'm going to.
Give it a comment here.
Zoned date time.
Using clock.
Block.
Now the code is not readable.
Now I'm going to copy this and put it here.
There you go.
Let's run this.
If you take a look at the Denver time, the Denver time is 513.
And here the Denver time is also 513.
But this one, we got the zone date and time using the clock as an input to that zone date time dot
now method.
Now let's explore some other approaches of creating a local date time using the time zone.
What I'm going to do.
Until now, we have seen examples like local date time dot now, right?
But if you take a look at it, it accepts something called time zone.
Let's go ahead and explore that.
If you give the zone ID, I'm going to give the zone ID as Detroit and then check what is the local
date time it is going to give as part of it.
Want to give local date time, local date time.
I'm going to print it.
System.out.println.
And in here I'm going to give the Detroit.
And then.
Local daytime.
Want to just print the local daytime and check?
So if we take a look at the time it gave you the local date time of Detroit, but it did not have any
of the time zone related information.
I'll be covering how to convert a local date time to a zoned date time in the next tutorial.
For now, you can get the local date time of that particular zone using the local daytime dot now which
accepts the zone ID as an input.
Now let's explore the another version of it which accepts the clock.
I'm going to give clock Dot.
System which accepts a zone ID as an input.
So again, this is going to be Detroit, but it accepts the clock as an input instead of just zone ID.
Going to print local daytime here.
Let's go ahead and run this one.
There you go.
It gave you the same value as the.
Local date time dot now which accepts the zone ID.
So these are the different ways of creating the local date time using the zone as as an input.
Now what I'm going to do, I'm going to create an instant using the instant I'm going to create the
appropriate zone related information.
So I'm going to use local date time dot of instance.
There is a handy method called off instant to which you will pass the current zones.
The first input to this one.
If you go to the instant method, it accepts the instant and zone ID as an input.
I'm going to do instant.
Dot now, which is going to give me this current machines local date and time and then I'm going to
give zone ID dot there is a system default.
This is going to give you the current machines zone.
And then I'm going to create local daytime instance and local date time two instance.
Basically we are creating a local date time using the off instance method that is part of the local
date time class.
I'm going to give off instant.
It's going to be local date time to.
I'm going to run this example.
But on this example, this will give you this machine's local date and time.
But the method that we have used in order to create the local date time is of instant.
So these are the different methods that you can use if you have any zone related use case.
And this one is really handy using which you can determine the different time zones that Java Library
supports.
Basically, it supports all the time zones throughout the world.
So with this we came to the end of this tutorial.
Let's explore how to convert a local date time and an instant to a zoned date time in the next tutorial.
Thank you for watching.