WEBVTT

00:00.610 --> 00:01.180
Hi everyone.

00:01.180 --> 00:02.410
Welcome back to this tutorial.

00:02.410 --> 00:11.920
In this tutorial we will code and explore how to convert Java.util.date and Java dot SQL update to local

00:11.920 --> 00:13.030
Date instance.

00:14.030 --> 00:19.280
The first thing is I'm going to expand this data package and then I'm going to create a class called

00:19.520 --> 00:20.210
Date.

00:22.440 --> 00:22.770
To.

00:24.320 --> 00:25.780
Local date.

00:26.500 --> 00:27.080
Sample.

00:28.900 --> 00:33.280
Then I'm going to make this class executable by adding public static void main method.

00:34.890 --> 00:36.000
In presentation mode.

00:36.900 --> 00:37.530
There you go.

00:37.560 --> 00:42.720
Now, what we are going to do, I'm going to create an instance of date.

00:44.090 --> 00:45.560
All to new date.

00:46.790 --> 00:48.350
So let's import this.

00:49.250 --> 00:52.010
I'm going to use the Java util conversion first.

00:52.170 --> 00:53.720
The Java.util.date.

00:54.500 --> 00:56.450
Let me put some comments here.

00:56.600 --> 01:01.010
The first use case is Java dot util dot date.

01:02.500 --> 01:03.190
So local.

01:06.690 --> 01:13.590
And the next use case that we are going to code is it's going to be Java dot SQL dot date.

01:16.430 --> 01:17.780
Cool local.

01:18.850 --> 01:20.470
Pete and vice versa.

01:22.590 --> 01:23.550
At the same year.

01:24.970 --> 01:25.950
And you see.

01:28.140 --> 01:30.720
Now let's print this and check the result.

01:31.320 --> 01:33.930
What does this date instance?

01:35.830 --> 01:36.250
Prince.

01:39.900 --> 01:48.120
You take a look at it, it has both the date and its appropriate time, the time zone it belongs to,

01:48.120 --> 01:49.860
and then the year.

01:50.800 --> 01:54.340
Now, how can we convert this to a local date?

01:55.030 --> 01:56.860
So the way to do it is.

01:58.230 --> 01:59.970
Let's use the instance that we created.

01:59.970 --> 02:03.720
And then it has a handy method called to instant.

02:04.430 --> 02:04.970
Right.

02:05.180 --> 02:07.040
So let's call that method.

02:07.370 --> 02:11.960
And if you go to that method, there's got introduced as part of Java 1.8.

02:11.990 --> 02:18.350
This is going to return an instant, an instance of instant, which is going to give you the current

02:18.350 --> 02:18.650
time.

02:18.830 --> 02:23.870
If you go to the get time method, this is going to give you this machine's current time.

02:23.870 --> 02:24.980
And then.

02:26.560 --> 02:30.760
It is going to call the add zone method.

02:30.760 --> 02:37.030
We are going to call the zone method and then I'm going to pass the system's default time zone.

02:37.570 --> 02:43.060
And if you press dot, it is going to give you the access to different conversions.

02:43.100 --> 02:43.720
We take a look at it.

02:43.720 --> 02:51.010
It has a local date, local date time also, and then local time and offset date and time.

02:51.010 --> 02:56.020
But our use cases, we are just going to convert this to local date if we want to have if you have any

02:56.020 --> 03:01.390
use cases where you want that local date and time, you can still use the local date and time method.

03:01.390 --> 03:04.420
So I'm going to click, I'm going to select the two local date.

03:04.420 --> 03:07.450
So this is going to give me the local date instance.

03:07.450 --> 03:09.070
I'm going to give it a name.

03:11.200 --> 03:12.460
Both the local date.

03:12.670 --> 03:17.140
The local date is part of the Java dot time dot local date package.

03:17.140 --> 03:19.600
So now I'm going to print it.

03:22.110 --> 03:23.460
The local date.

03:25.880 --> 03:26.360
Good.

03:26.630 --> 03:28.010
Run this and check the result.

03:29.610 --> 03:38.250
So the value is 2018 zero 720, which is July 7th, July 20th, 2018.

03:38.280 --> 03:43.440
So we have successfully converted the Java.util.date to local date.

03:43.470 --> 03:49.590
Now let's do the reverse version How to convert the local date instance to a java.util.date.

03:49.800 --> 03:51.480
So I'm going to use the same instance.

03:51.480 --> 03:54.720
Whatever we have used here I'm going to do.

03:55.710 --> 03:57.330
First thing is date.

03:57.330 --> 04:00.660
Let's give it a name equal to new date.

04:01.870 --> 04:02.290
Art.

04:02.860 --> 04:04.690
There is a handy method called from.

04:04.960 --> 04:10.690
So if you take a look at this method, this method got introduced as part of 1.8 and it accepts something

04:10.690 --> 04:11.710
called instant.

04:12.220 --> 04:17.420
Our use case is we are going to convert this local date to instance.

04:17.440 --> 04:19.930
Right now we just have the local date, right?

04:19.960 --> 04:21.640
How can we add time to it?

04:21.820 --> 04:25.570
So I'm going to use local date dot at time.

04:26.230 --> 04:28.630
I'm going to use this machines time itself.

04:28.740 --> 04:31.870
It's going to be local time dot now.

04:32.440 --> 04:39.370
So this gave you the this will give you the local date and time as a output and then I'm going to use

04:39.370 --> 04:40.780
the Add zone.

04:41.230 --> 04:46.480
I'm going to use zone ID dot system default.

04:48.100 --> 04:50.260
What are below so that the code will be clear.

04:50.260 --> 04:51.340
And then.

04:52.760 --> 04:55.690
After this, we are going to call the instant.

04:55.700 --> 04:58.820
So this is what the from method is expecting.

05:00.220 --> 05:01.250
Take a look at the front method.

05:01.250 --> 05:02.860
It expects something called instance.

05:03.690 --> 05:11.640
So using that, you'll be able to successfully convert this local date to a actual date instance, which

05:11.640 --> 05:13.710
is Java.util dot.

05:15.330 --> 05:16.500
Eight, actually.

05:17.220 --> 05:19.680
So I'm going to pass a day to one.

05:20.630 --> 05:21.890
Then check the result.

05:25.070 --> 05:25.490
Here you go.

05:25.490 --> 05:35.510
We got the result as Friday, July 20th, five 3014, and the time zone is cdti and the year is 2018.

05:35.510 --> 05:42.910
So we have successfully converted Java.util dot date to local date and vice versa.

05:42.920 --> 05:49.340
Now let's take a look at the Java dot SQL update and then we'll try to convert that to local date.

05:50.800 --> 05:55.210
So I'm going to use Java dot SQL dot date.

05:55.930 --> 05:58.120
So we're going to create an instance of.

05:59.400 --> 06:00.360
Sequel dot date.

06:00.360 --> 06:01.410
I'm going to give it a name.

06:01.410 --> 06:02.820
It's going to be date two.

06:03.810 --> 06:08.430
And then I'm going to be Java dot SQL dot date.

06:09.790 --> 06:12.130
Got it has something called.

06:13.420 --> 06:14.680
Value of.

06:14.710 --> 06:15.760
We take a look at it.

06:15.940 --> 06:17.830
The value of method.

06:18.070 --> 06:19.380
We go and check this method.

06:19.390 --> 06:24.010
It accepts something called local date and it can also accept a string.

06:24.040 --> 06:30.580
Now our use cases we want to convert the JavaScript date to local date.

06:30.580 --> 06:30.910
Right?

06:30.910 --> 06:37.270
But the first use case we are covering how to convert from local date to Java dot SQL dot date.

06:37.480 --> 06:41.110
Then I'm going to pass the local date instance that we created here.

06:41.230 --> 06:42.340
Now.

06:43.560 --> 06:44.490
If I print this.

06:44.490 --> 06:50.490
So we have successfully converted the local date to Java dot SQL SQL dot date here.

06:53.230 --> 06:55.740
When I printed here, you run this.

06:55.750 --> 07:02.470
Basically that is going to give you an instance of JavaScript date from local date.

07:02.500 --> 07:07.600
Now how do you convert a JavaScript date to a local date?

07:07.630 --> 07:11.410
It has a handy method called date to.

07:12.340 --> 07:14.050
Got to local date.

07:14.380 --> 07:15.100
Very simple.

07:15.100 --> 07:19.660
And then this will give you the local date as an output.

07:20.360 --> 07:20.480
Okay.

07:20.590 --> 07:21.760
Local date one.

07:23.610 --> 07:25.560
And let's print it in the console.

07:26.950 --> 07:29.230
Dot out dot then and.

07:33.540 --> 07:34.110
Right here.

07:34.870 --> 07:39.010
If you run it, it is going to give me the local data as a output.

07:39.370 --> 07:45.820
So we have successfully converted Java dot SQL update to a local date and vice versa.

07:46.210 --> 07:47.320
This section.

07:47.320 --> 07:51.820
So basically these are existing dates, right?

07:51.820 --> 07:58.840
If you have any use case to move from that existing date to local date in order to make use of the new

07:58.840 --> 08:05.080
functionalities that got introduced as part of Java eight, then these are the different conversion

08:05.080 --> 08:12.070
techniques that you can use in order to convert your current date versions, which is Java.util.date

08:12.070 --> 08:16.360
and Java SQL dot date to a local date and vice versa.

08:16.480 --> 08:18.540
With this, we came to the end of this tutorial.

08:18.550 --> 08:19.660
Thank you for watching.
