WEBVTT

00:00.320 --> 00:00.890
Hi everyone.

00:00.890 --> 00:02.150
Welcome back to this tutorial.

00:02.150 --> 00:09.440
In this tutorial, we will learn about how to make use of the new Java Time libraries to work with time

00:09.440 --> 00:10.070
zones.

00:10.070 --> 00:17.360
Until now we have seen different examples of date and time, but all of the examples are local date

00:17.360 --> 00:18.110
and time.

00:18.110 --> 00:25.250
Java eight introduced some zone specific date time libraries that will be covering in this tutorial,

00:25.250 --> 00:30.950
such as the zoned date, time zone ID and zone offset.

00:30.980 --> 00:33.620
Let's talk about the zoned date time.

00:33.620 --> 00:40.220
First, it represents the date and time with its associated time zone and offset.

00:40.490 --> 00:45.010
Here is an example of the output of the zoned date time instance.

00:45.020 --> 00:50.930
In general, time zones are categorized by zone offset and zone ID.

00:51.080 --> 00:58.310
If you take a look at the example here, it has the time along with the zone offset and the zone ID

00:58.550 --> 01:08.910
zone offset here is nothing but -0 five colon zero zero zone offset represents the offset time from

01:08.910 --> 01:09.990
the UTC time.

01:12.280 --> 01:15.640
The example here is from my local machine time.

01:15.640 --> 01:21.590
It has the offset value of -five hours from the UTC time.

01:21.610 --> 01:22.990
What is the zone ID?

01:23.140 --> 01:23.950
Zone ID.

01:24.040 --> 01:30.820
This is another unique identifier which can be used to determine the zone to which the time belongs

01:30.820 --> 01:31.300
to.

01:32.930 --> 01:36.740
Let's go ahead and explore the time zones using some examples.

01:36.740 --> 01:38.090
I'm going to go back to the IntelliJ.

01:38.750 --> 01:39.650
There we go.

01:41.960 --> 01:42.980
Let me close this.

01:43.430 --> 01:47.810
I'm going to create a class called zoned Date time Example.

01:48.920 --> 01:53.300
Be zoned date time Example.

01:54.280 --> 01:57.100
Wanna to add the public static void main method here.

01:57.610 --> 01:58.570
There you go.

01:59.430 --> 02:01.060
Gonna put this in presentation mode.

02:01.970 --> 02:02.780
Now.

02:03.640 --> 02:06.700
I'm going to create a zoned date time instance.

02:06.700 --> 02:07.940
How can we create it?

02:07.960 --> 02:13.210
We're going to use the zoned date time class dot now.

02:14.720 --> 02:18.860
And this is going to return me an instance of zoned datetime.

02:18.860 --> 02:20.300
I'm going to give it a name.

02:21.670 --> 02:22.450
Here we go.

02:22.660 --> 02:24.550
Let's print this and check the result.

02:30.100 --> 02:30.750
Monoprint.

02:30.820 --> 02:32.170
Zoned date and time.

02:32.590 --> 02:32.950
We run.

02:32.970 --> 02:35.050
This is going to give me.

02:36.260 --> 02:38.120
The date time.

02:38.240 --> 02:45.710
Along with that, it gave me the offset and the time zone ID to which it belongs to.

02:46.010 --> 02:52.070
How can we get this offset value and the zone ID value from the zoned date time instance?

02:52.340 --> 02:54.530
It has some handy methods in it.

02:54.890 --> 02:56.960
The first thing is I'm going to get.

02:58.140 --> 02:58.800
Zone.

03:00.100 --> 03:00.700
Offset.

03:03.630 --> 03:09.510
So this zoned daytime instance has a method called get offset.

03:09.540 --> 03:15.960
If you give that value, if you call that method, it is going to give you the offset time.

03:18.820 --> 03:21.370
Though the officer time is returned as minus five.

03:21.460 --> 03:30.310
Basically, this gives me my local times zone information and my local time is -five hours from the

03:30.310 --> 03:31.390
UTC time.

03:31.420 --> 03:34.810
Now how can we get this zone ID out of it?

03:35.110 --> 03:41.230
Because we have seen different examples of getting the date, time, year, month, date and all those

03:41.230 --> 03:42.640
things in the prior tutorials.

03:42.640 --> 03:45.930
Now we are going to focus mainly on the zone related information.

03:45.940 --> 03:47.260
How can we get the zone ID?

03:47.650 --> 03:48.190
Again?

03:48.190 --> 03:54.640
It has a handy method called get zone id using which I'm going to give zone date time dot.

03:54.820 --> 03:58.450
If we give get zone, it's going to give you the zone ID.

03:58.480 --> 04:03.340
If you go to the method, it gives you the zone ID as a response.

04:04.630 --> 04:06.730
More on this example and check the result.

04:08.270 --> 04:09.050
To check the result.

04:09.050 --> 04:14.210
It gave you the zone ID as America slash Chicago.

04:14.240 --> 04:19.460
Now let's take a look at what are the different zones it supports.

04:19.490 --> 04:20.120
Right.

04:20.120 --> 04:21.740
So how can we check?

04:21.740 --> 04:24.620
What are the list of available zone IDs?

04:24.980 --> 04:26.750
There is a handy method called.

04:27.790 --> 04:32.440
We have to use the zone ID class dot, get available zone IDs.

04:32.440 --> 04:34.210
I'm just going to print this one.

04:39.210 --> 04:40.620
Available zones.

04:40.620 --> 04:46.350
So this is going to give you the list of zones that this Java API supports.

04:47.320 --> 04:48.070
I'm going to run this.

04:48.070 --> 04:51.370
It's going to give you a lot of zones, actually.

04:52.190 --> 04:57.050
If we take a look at it, it print it printed a lot of information and it is not in a readable format.

04:57.080 --> 05:04.340
What I'm going to do, I'm going to make use of the Frames library to print this data in a readable

05:04.340 --> 05:04.820
format.

05:04.850 --> 05:11.990
Get available zone IDs, dot frame, and then we're going to make use of the for each method, which

05:11.990 --> 05:13.100
is part of the.

05:14.290 --> 05:17.440
Dream, and then I'm going to give.

05:18.800 --> 05:19.370
John.

05:19.580 --> 05:21.620
The Forejt takes in a consumer, right?

05:21.620 --> 05:23.330
And then shuts out.

05:25.170 --> 05:25.500
Zone.

05:28.550 --> 05:30.080
Now I'm going to run this example.

05:30.080 --> 05:33.230
It is going to print the result in a readable format.

05:35.160 --> 05:35.690
There you go.

05:35.700 --> 05:38.790
So these are the different zones it supports.

05:38.790 --> 05:39.930
It's a lot of zones.

05:39.930 --> 05:41.700
I believe the number is 600.

05:41.730 --> 05:42.780
Let's go ahead and check that.

05:42.780 --> 05:47.310
Also, what I'm going to do, I'm going to come on this piece of code.

05:49.960 --> 05:52.780
Let's check how many zones it supports.

05:54.280 --> 05:57.000
I'm going to give number of zones.

05:58.040 --> 06:06.980
And it's going to be zone ID dot get available zone IDs, dot not stream.

06:06.980 --> 06:08.210
It's going to be size.

06:09.110 --> 06:11.090
So this is going to return you a set.

06:11.090 --> 06:13.970
And then we are calling the size method in that set.

06:17.210 --> 06:20.900
So the total number of zones it supports is 600.

06:21.350 --> 06:27.890
Now, in this tutorial, what I'm going to do, I'm going to show you a demo of how to get different

06:27.890 --> 06:29.660
time zones in America.

06:29.660 --> 06:33.440
So basically in America there are four different time zones.

06:33.980 --> 06:34.850
One is east.

06:37.700 --> 06:41.900
DST and CST change this order a little bit.

06:42.680 --> 06:43.460
Going to be.

06:44.930 --> 06:50.360
Este este este and how do you get the time?

06:50.660 --> 06:52.970
So I'm going to give.

06:56.610 --> 07:01.410
Chicago, because Chicago, if you take a look at it, I'm I'm in the zone.

07:01.440 --> 07:04.590
America, Chicago will give you the time.

07:06.630 --> 07:11.550
And then we are going to do zoned date time dot.

07:11.730 --> 07:16.200
Now in here, the now is going to give you the local machines, right?

07:16.200 --> 07:18.680
But I want to pass a zone ID as part of it.

07:18.690 --> 07:23.970
So if you take a look at the now, now has three different versions.

07:23.970 --> 07:27.150
We looked at the now just now method without any parameters.

07:27.150 --> 07:30.060
This is going to give you that machines time zone.

07:30.060 --> 07:35.280
Now we are going to make use of the now method which accepts the zone ID as an input.

07:35.550 --> 07:42.060
So if you go here, zone ID dot off, you pass the string, right?

07:42.060 --> 07:45.180
The string is going to be America slash.

07:46.120 --> 07:46.780
Chicago.

07:47.490 --> 07:50.610
So this will give you the time.

07:51.930 --> 07:52.530
Of the.

07:53.010 --> 07:59.200
So if you take a look at it, it gives it gave you the time of the day and the time is 609.

07:59.220 --> 08:04.680
Now what I want I want the Eastern Time zones, current time for the Eastern Time zone.

08:04.680 --> 08:06.030
I'm going to use Detroit.

08:07.930 --> 08:08.650
Applied.

08:10.330 --> 08:11.170
Going to be.

08:12.660 --> 08:13.950
It right now.

08:13.950 --> 08:15.720
Let's take a look at the time difference here.

08:15.750 --> 08:18.300
Detroit is one hour ahead of CST.

08:20.250 --> 08:25.770
If you take a look out here, it printed the time as 709 and the time is.

08:26.700 --> 08:28.770
6 or 9, to name this one as ist.

08:29.010 --> 08:34.170
Now what I'm going to do, I want to check the Pacific Standard time.

08:34.880 --> 08:37.350
Ist going to be L.A.

08:38.720 --> 08:41.740
I want to keep this colon in sync so that it will be easy to read.

08:42.040 --> 08:45.220
I'm going to give Los Angeles.

08:49.390 --> 08:49.810
Right.

08:50.140 --> 08:51.190
They give the value, right.

08:51.220 --> 08:53.680
Let's go and check this by running this example.

08:55.700 --> 08:57.980
Run this example in Los Angeles.

08:58.010 --> 08:59.720
The current time is.

09:01.020 --> 09:01.890
There is a current time.

09:02.340 --> 09:07.080
The current time is zero four, which is two hours before the CST.

09:07.110 --> 09:19.020
The time is six and the time is seven and time is zero four, which is two hours before the time now.

09:20.180 --> 09:22.550
I'm going to print the time.

09:23.210 --> 09:23.750
Right.

09:23.750 --> 09:25.040
And it's going to be Denver.

09:28.200 --> 09:28.410
Oh.

09:30.420 --> 09:33.360
And I'm going to pass this value as Denver.

09:34.290 --> 09:35.460
Let's go ahead and run this.

09:39.420 --> 09:46.350
Gave you the value as zero five colon 1111 I'm going to use as a benchmark here because since I'm on,

09:46.760 --> 09:48.390
it is easy for me to compare.

09:48.390 --> 09:54.990
If you take a look at it, the time is an hour ahead, an hour before the time.

09:54.990 --> 10:03.210
So using the zoned date time class, you can get the times of that particular time zone.

10:03.390 --> 10:05.100
Time zones, local date and time.

10:05.100 --> 10:06.420
So if you take a look at it.

10:07.080 --> 10:10.410
This one gave you the time of cst's local time.

10:10.410 --> 10:13.710
This one gave you the time of its local time.

10:13.710 --> 10:19.770
This one gave you the time of its local time and this one gave you the time of its local time.

10:19.770 --> 10:23.460
This is advantage of its zoned date time class.

10:23.460 --> 10:30.150
This was never possible with local date and time because it was able to give you only the time of that

10:30.150 --> 10:30.660
machine.

10:30.660 --> 10:36.720
Actually, whenever you call local daytime dot now it is going to give you that machine's local date

10:36.720 --> 10:37.440
and time.

10:37.440 --> 10:44.490
Now we have seen the zone Datetime.now, which takes in the zone ID as an input.

10:44.490 --> 10:44.970
Right?

10:44.970 --> 10:47.790
There is another version which takes in.

10:48.790 --> 10:52.020
Zone date and time dot now, which takes in a clock.

10:52.030 --> 10:52.330
Right?

10:52.330 --> 10:53.930
Let's go ahead and explore that one too.

10:54.130 --> 11:00.490
So I'm going to pass the clock dot system and then I'm going to pass the.

11:01.940 --> 11:03.140
Time zone of this.

11:04.500 --> 11:11.520
This is going to give me again the zoned date and time of this particular zone.

11:11.520 --> 11:16.030
But if you take a look at the instance, it took clock as an input, right?

11:16.170 --> 11:17.010
So I'm going to.

11:19.260 --> 11:20.490
Give it a comment here.

11:21.210 --> 11:22.920
Zoned date time.

11:24.750 --> 11:25.860
Using clock.

11:28.460 --> 11:28.880
Block.

11:30.120 --> 11:31.230
Now the code is not readable.

11:31.230 --> 11:33.270
Now I'm going to copy this and put it here.

11:36.730 --> 11:37.400
There you go.

11:37.420 --> 11:38.620
Let's run this.

11:42.600 --> 11:47.580
If you take a look at the Denver time, the Denver time is 513.

11:47.580 --> 11:50.070
And here the Denver time is also 513.

11:50.070 --> 11:57.570
But this one, we got the zone date and time using the clock as an input to that zone date time dot

11:57.570 --> 11:58.230
now method.

11:58.260 --> 12:04.560
Now let's explore some other approaches of creating a local date time using the time zone.

12:04.830 --> 12:06.330
What I'm going to do.

12:07.710 --> 12:12.630
Until now, we have seen examples like local date time dot now, right?

12:12.630 --> 12:16.200
But if you take a look at it, it accepts something called time zone.

12:16.200 --> 12:17.790
Let's go ahead and explore that.

12:17.820 --> 12:25.230
If you give the zone ID, I'm going to give the zone ID as Detroit and then check what is the local

12:25.230 --> 12:27.630
date time it is going to give as part of it.

12:29.390 --> 12:32.120
Want to give local date time, local date time.

12:32.840 --> 12:33.830
I'm going to print it.

12:39.270 --> 12:40.920
System.out.println.

12:40.920 --> 12:45.060
And in here I'm going to give the Detroit.

12:50.660 --> 12:51.560
And then.

12:53.170 --> 12:54.490
Local daytime.

12:55.410 --> 12:57.700
Want to just print the local daytime and check?

12:59.340 --> 13:06.720
So if we take a look at the time it gave you the local date time of Detroit, but it did not have any

13:06.720 --> 13:09.250
of the time zone related information.

13:09.270 --> 13:14.600
I'll be covering how to convert a local date time to a zoned date time in the next tutorial.

13:14.610 --> 13:21.810
For now, you can get the local date time of that particular zone using the local daytime dot now which

13:21.840 --> 13:25.140
accepts the zone ID as an input.

13:25.930 --> 13:31.090
Now let's explore the another version of it which accepts the clock.

13:32.500 --> 13:34.870
I'm going to give clock Dot.

13:36.410 --> 13:39.500
System which accepts a zone ID as an input.

13:41.640 --> 13:47.940
So again, this is going to be Detroit, but it accepts the clock as an input instead of just zone ID.

13:49.060 --> 13:51.040
Going to print local daytime here.

13:51.490 --> 13:52.840
Let's go ahead and run this one.

13:55.080 --> 13:55.550
There you go.

13:55.560 --> 13:57.870
It gave you the same value as the.

13:58.640 --> 14:01.160
Local date time dot now which accepts the zone ID.

14:01.700 --> 14:08.360
So these are the different ways of creating the local date time using the zone as as an input.

14:08.390 --> 14:15.020
Now what I'm going to do, I'm going to create an instant using the instant I'm going to create the

14:15.020 --> 14:17.840
appropriate zone related information.

14:17.840 --> 14:22.040
So I'm going to use local date time dot of instance.

14:22.040 --> 14:27.380
There is a handy method called off instant to which you will pass the current zones.

14:28.530 --> 14:29.910
The first input to this one.

14:29.910 --> 14:33.750
If you go to the instant method, it accepts the instant and zone ID as an input.

14:33.780 --> 14:35.310
I'm going to do instant.

14:36.450 --> 14:41.820
Dot now, which is going to give me this current machines local date and time and then I'm going to

14:41.820 --> 14:46.950
give zone ID dot there is a system default.

14:46.950 --> 14:49.860
This is going to give you the current machines zone.

14:50.130 --> 14:56.280
And then I'm going to create local daytime instance and local date time two instance.

14:56.280 --> 15:03.600
Basically we are creating a local date time using the off instance method that is part of the local

15:03.600 --> 15:05.250
date time class.

15:06.390 --> 15:08.180
I'm going to give off instant.

15:08.210 --> 15:10.670
It's going to be local date time to.

15:11.410 --> 15:12.940
I'm going to run this example.

15:14.940 --> 15:19.470
But on this example, this will give you this machine's local date and time.

15:19.470 --> 15:25.290
But the method that we have used in order to create the local date time is of instant.

15:25.560 --> 15:30.960
So these are the different methods that you can use if you have any zone related use case.

15:30.960 --> 15:37.140
And this one is really handy using which you can determine the different time zones that Java Library

15:37.140 --> 15:37.620
supports.

15:37.620 --> 15:41.070
Basically, it supports all the time zones throughout the world.

15:42.480 --> 15:44.250
So with this we came to the end of this tutorial.

15:44.250 --> 15:50.550
Let's explore how to convert a local date time and an instant to a zoned date time in the next tutorial.

15:50.640 --> 15:51.660
Thank you for watching.
