WEBVTT

00:00.170 --> 00:00.710
Hi everyone.

00:00.710 --> 00:02.080
Welcome back to this tutorial.

00:02.090 --> 00:08.990
In this tutorial we will code and explore how to format and parse the local date time using date time

00:09.020 --> 00:09.860
parameter.

00:10.010 --> 00:16.190
For this purpose, what I'm going to do, I'm going to create a class called formatting.

00:18.360 --> 00:18.840
Local.

00:20.050 --> 00:21.400
Date time.

00:25.520 --> 00:28.880
Make this class executable by adding public static void main method.

00:30.770 --> 00:33.050
Now let's create two methods.

00:34.410 --> 00:35.040
For that.

00:35.990 --> 00:37.040
Presentation mode.

00:37.280 --> 00:40.850
Here the two methods are going to be static methods.

00:41.240 --> 00:42.740
Public static.

00:43.860 --> 00:49.350
Void pass local date time.

00:50.660 --> 00:52.730
And then the next method is going to be.

00:54.620 --> 00:55.140
Format.

00:55.190 --> 00:56.300
Local date and time.

00:57.500 --> 00:58.000
Thus.

01:00.200 --> 01:01.610
Going to be format.

01:02.910 --> 01:04.350
Local date and time.

01:05.350 --> 01:08.110
So now I'm going to create a string.

01:08.110 --> 01:10.930
So first thing is we are going to convert a string to a local date.

01:11.980 --> 01:13.300
So let's give it a name.

01:13.690 --> 01:16.210
Going to be 2018.

01:17.170 --> 01:18.130
Zero four.

01:19.530 --> 01:21.140
1880.

01:21.360 --> 01:27.440
We all know the local dating format by default puts this T in between the date and time.

01:27.450 --> 01:30.860
I'm going to have the time as 1433 and 33.

01:30.870 --> 01:33.360
So this is a local date time.

01:33.360 --> 01:35.100
We want to parse this.

01:35.100 --> 01:37.230
So this is a local date time string.

01:37.230 --> 01:41.100
We want to convert this to a local date time instance, right?

01:41.100 --> 01:42.630
So how do we do it?

01:42.840 --> 01:48.150
Let's use the local date time dot parse method and then parse this date time.

01:48.150 --> 01:51.210
So even the parse method has two different versions of it.

01:51.240 --> 01:56.010
One without the date time formatter and with the date time formatter, you go to this parse method,

01:56.040 --> 01:58.950
it uses the local date and time.

01:58.980 --> 02:01.050
We take a look at the local date and time.

02:01.050 --> 02:05.760
It uses the local date and local time which is separated by a key.

02:07.230 --> 02:12.150
So in here, let's see what is the output that it is going to give.

02:12.540 --> 02:16.650
So here we are trying to convert a string to a local date time instance.

02:17.070 --> 02:21.900
I'm going to print this in the console so that we can validate what is a value that gets mapped.

02:23.200 --> 02:25.360
Now I'm going to call these two methods.

02:25.830 --> 02:27.940
First one is going to be parse.

02:29.560 --> 02:33.310
Local date and time, and the next one is going to be format, local date and time.

02:34.120 --> 02:36.370
Want to run this example and check the result.

02:38.290 --> 02:39.520
We take a look at the result?

02:40.390 --> 02:43.870
Pass the string to a date time.

02:43.900 --> 02:48.880
Now what I'm going to do, I'm going to use the another version of the parse method.

02:49.390 --> 02:54.280
Local datetime dot parse, which takes in the string and the datetime formatter.

02:54.550 --> 02:57.400
So that's going to be the datetime again.

02:57.610 --> 03:00.880
Followed by that I'm going to use the datetime formatter.

03:01.420 --> 03:02.680
Not going to be the date.

03:02.680 --> 03:05.020
It's going to be local date time.

03:06.300 --> 03:06.600
Right.

03:06.600 --> 03:10.320
And then this is going to give us the local daytime as the output.

03:11.210 --> 03:12.220
I'm gonna give it a name.

03:12.240 --> 03:13.050
There you go.

03:14.770 --> 03:16.660
And let's print this and check the result.

03:20.750 --> 03:21.890
Take a look at the result.

03:24.310 --> 03:25.300
This and check the result.

03:27.160 --> 03:29.780
Though it gave the result as yet.

03:29.830 --> 03:34.420
Now we are going to play with the custom date time format.

03:36.800 --> 03:38.390
Going to be custom.

03:43.030 --> 03:43.570
Format.

03:43.600 --> 03:47.410
So in here, what I'm going to do, I'm going to create a string, right?

03:47.620 --> 03:47.920
String.

03:47.920 --> 03:49.270
And let's give it a name.

03:50.050 --> 03:51.040
Date time one.

03:51.830 --> 03:53.840
And then this is going to be.

03:56.420 --> 03:58.190
Similar kind, but.

03:59.440 --> 04:00.580
In the place of colon.

04:00.580 --> 04:02.140
I'm going to have pipe.

04:03.600 --> 04:03.930
Pipe.

04:05.830 --> 04:06.160
Pipe.

04:07.170 --> 04:14.670
So now we have to build our own datetime formatter instance because these are not these won't be available

04:14.670 --> 04:18.120
as part of the DateTime formatter as a predefined format.

04:18.150 --> 04:23.150
Now what we are going to do, we are going to create an instance within formatter.

04:23.160 --> 04:24.240
Let's give it a name.

04:24.690 --> 04:27.990
It's going to be datetime formatter dot of pattern.

04:27.990 --> 04:32.880
So the pattern is y y y y hyphen.

04:33.000 --> 04:37.560
The month we always represent in capital M followed by that date.

04:37.590 --> 04:42.300
We represent that in small D and going to be.

04:43.580 --> 04:50.060
A church which is in the 24 hour hour format and then followed by the let's give the pipe.

04:50.900 --> 04:51.110
More.

04:51.170 --> 04:52.250
Lemon And then.

04:52.280 --> 04:52.680
Yes.

04:53.870 --> 04:58.670
So this maps to the actual format that we have here.

04:58.700 --> 05:06.920
Now, what we will do, we will call local date time dot parse will parse the date time one, which

05:06.920 --> 05:08.120
is the new format.

05:08.120 --> 05:12.200
And then we use the date time formatter that we just created.

05:12.530 --> 05:15.680
So this is going to give you the local date time.

05:16.310 --> 05:17.240
Give it a name.

05:18.100 --> 05:18.400
Go.

05:18.850 --> 05:20.590
Let's print this and check the result.

05:23.080 --> 05:24.910
So daytime won in local.

05:24.910 --> 05:25.630
Daytime won.

05:25.780 --> 05:26.290
There you go.

05:26.320 --> 05:27.400
Let's run this.

05:29.260 --> 05:29.830
You run this?

05:29.860 --> 05:31.500
We ran into an issue.

05:31.510 --> 05:31.750
Okay.

05:31.750 --> 05:34.150
Let's take a look at the issue in here.

05:34.150 --> 05:39.010
We did not mention the T, That's mandatory, right?

05:39.010 --> 05:41.740
If you do not, then it is going to have some issues.

05:43.000 --> 05:44.320
Again, we have an issue.

05:45.490 --> 05:48.910
If you take a look at the line which is causing the issue, the line is 25.

05:48.940 --> 05:50.970
That is our new format.

05:50.980 --> 05:55.660
I believe we have to provide this in single quotes, otherwise it's going to cost.

05:57.530 --> 05:57.930
There you go.

05:57.950 --> 06:01.820
It was able to successfully format the string.

06:02.710 --> 06:09.880
Of our own format and it converted that to a local date using the datetime formatter that we just created.

06:10.030 --> 06:15.880
Keep in your mind, whenever you are working with a local datetime, this is really important.

06:15.880 --> 06:22.510
If you want to have the string to be successfully converted to a local date time.

06:22.540 --> 06:25.360
Now I'm going to have another string.

06:27.420 --> 06:31.860
And then in here, it's going to be day time to the place of tea.

06:31.890 --> 06:33.660
It is going to have A, B, C.

06:34.620 --> 06:36.450
In that case, will this work?

06:37.170 --> 06:38.010
Won't work.

06:38.040 --> 06:43.860
So in order to make this work, basically what we have to do, we have to provide ABC here.

06:44.070 --> 06:46.890
That's the only way to make it work.

06:46.890 --> 06:49.950
So we have this data formatter defined.

06:49.980 --> 06:56.370
Now what we will do create a local datetime instance, give it a name, and then I'm going to use the

06:56.370 --> 06:59.070
local datetime dot parse.

07:00.000 --> 07:04.890
When I use this and then pass the date time to followed by that.

07:04.890 --> 07:07.380
Let's pass the date time formatter.

07:07.960 --> 07:08.550
One.

07:10.880 --> 07:13.220
Then let's print this and check the result.

07:13.740 --> 07:14.320
We print this.

07:14.330 --> 07:17.360
It is going to give me the result that I'm looking for.

07:19.930 --> 07:21.730
Run this and check the result.

07:22.450 --> 07:23.050
There you go.

07:23.090 --> 07:28.810
Was able to successfully format and if you take a look at it, the local datetime will always have tea

07:28.810 --> 07:32.080
in between the date and time.

07:32.080 --> 07:33.610
So that's what it has here.

07:33.610 --> 07:40.900
Even though you have the string here as ABC, it will actually convert that to a appropriate local datetime

07:40.900 --> 07:42.820
instance when it gets converted.

07:42.820 --> 07:44.920
This will be replaced by tea.

07:45.100 --> 07:48.850
So this is all about parsing a local datetime.

07:48.850 --> 07:52.630
Now we will talk about how to convert a date to a string.

07:52.960 --> 07:54.280
So I'm going to use.

07:55.330 --> 07:57.430
Not date date time to a string.

07:57.530 --> 07:59.770
So I'm going to use a same formatter here.

07:59.770 --> 08:03.940
And then what I'm going to do, I'm going to create an instance of local date time.

08:05.510 --> 08:06.580
Want to give it a name?

08:07.400 --> 08:09.470
Going to be local date time.

08:10.380 --> 08:11.370
Not now.

08:12.950 --> 08:16.520
And then what we are going to do, we are going to use.

08:17.880 --> 08:21.430
I'm going to define a converted datetime.

08:22.050 --> 08:30.360
So in here I'm going to use the local datetime dot going to be the format and pass the formatter.

08:31.900 --> 08:37.480
No, it is going to give you the string back with ABC in it in the place of T, because when the local

08:37.480 --> 08:43.180
date time creates an instance, it's going to have a T by default and we are replacing that with ABC.

08:44.350 --> 08:46.090
So I'm going to copy this.

08:50.290 --> 08:51.580
Run this and check the result.

08:52.520 --> 08:54.670
And thus we take a look at it.

08:54.680 --> 09:01.160
It was able to successfully convert the local date time to the string format that we have defined here.

09:01.160 --> 09:03.770
And then in the place of colon is a default rate.

09:03.800 --> 09:09.080
We have pipe here was able to successfully convert that to a string.

09:09.200 --> 09:12.230
So this is my current machines time, which is zero eight.

09:12.230 --> 09:15.830
That's the reason why it is different from 1433 to this one.

09:16.760 --> 09:23.840
So this is all about converting a local datetime to a string and string to a local datetime using datetime

09:23.840 --> 09:24.570
parameter.

09:24.590 --> 09:26.510
But this we came to the end of this tutorial.

09:26.540 --> 09:27.410
Thank you for watching.
