WEBVTT

00:00.460 --> 00:01.030
Hi everyone.

00:01.030 --> 00:02.350
Welcome back to this tutorial.

00:02.350 --> 00:06.580
In this tutorial we will code and learn about DateTime formatter.

00:06.700 --> 00:13.690
This class is introduced in Java 1.8 and is part of the Java dot time dot format package.

00:14.140 --> 00:18.310
This class really made the formatting and parsing easy.

00:18.340 --> 00:25.750
This class is used to format and parse the local date, local time and local date and time.

00:25.900 --> 00:31.390
I would like to highlight the difference between the definitions of parsing and formatting here.

00:31.420 --> 00:34.780
Oftentimes it leads to confusion.

00:34.780 --> 00:42.430
Parsing in datetime formatter context refers to converting a string to a local date or local time or

00:42.430 --> 00:50.830
local date time formatting in DateTime formatter context refers to converting a local date or local

00:50.830 --> 00:54.760
time or local date time to a string.

00:55.900 --> 01:01.340
Before we get into the coding part, I would like to go to the Java documentation of DateTime formatter

01:01.340 --> 01:07.040
in their website and explain you about some of the necessary information that we have to be aware of

01:07.280 --> 01:14.540
when we go to the browser and then search for date time formatter and then select the first link which

01:14.540 --> 01:18.260
has date time formatter Java platform C eight.

01:18.410 --> 01:19.310
Click on that.

01:19.340 --> 01:21.080
This will take you to this page.

01:21.800 --> 01:26.750
I'm going to highlight some of the things to consider while working with DateTime formatter.

01:26.780 --> 01:27.290
Here.

01:27.290 --> 01:30.680
The first thing is it comes with some predefined constants.

01:30.740 --> 01:34.530
If you take a look at it, such as ISO local date, you go to this.

01:34.550 --> 01:38.690
This is one of the format the DateTime formatter supports by default.

01:38.690 --> 01:44.030
And if you take a look at the format here, followed by the month and followed by the date, if your

01:44.030 --> 01:50.690
use case is to convert a string to a local date and local date to a string and which is of this format,

01:50.720 --> 01:54.260
then you can make use of this ISO local date constant.

01:54.260 --> 02:00.590
And if you take a look at this example ISO offset date, it has a similar ISO local date format, but

02:00.590 --> 02:04.280
in addition to that it has the offset information attached to it.

02:04.280 --> 02:10.830
And if you take a look at ISO date, it supports both the ISO local date and the ISO offset date.

02:10.850 --> 02:17.570
Similar to the date it has the support for time, which is the local time and also local date time.

02:17.570 --> 02:22.470
I would highly recommend you guys to go through this documentation and then check whether the format

02:22.470 --> 02:26.010
that you are looking for is already available as part of the data formatter.

02:26.020 --> 02:30.810
Then you can make use of those constants in your application.

02:30.900 --> 02:36.450
I'm going to go to the top and then highlight some of the key things to consider while working with

02:36.450 --> 02:37.740
date time formatter.

02:37.770 --> 02:39.090
Let's go from top to bottom.

02:39.090 --> 02:43.410
If you are going to work with year, then you can either use U or Y.

02:43.980 --> 02:45.840
You're going to work with date.

02:45.840 --> 02:52.680
In that case, you can either use capital D or small D, The capital D represents the day of the year.

02:52.680 --> 02:55.950
So if you take a look at it, the number can go until 365.

02:55.980 --> 03:01.320
If you want to use the day of the month, then you have to use the small d you're going to work with

03:01.320 --> 03:01.800
month.

03:01.800 --> 03:08.280
In that case, you can use the capital M and you cannot use small M because small M corresponds to the

03:08.280 --> 03:15.480
minute of R, And if you want to work with the 24 hour format, in that case you have to use capital

03:15.480 --> 03:17.160
H for the 12 hours.

03:17.160 --> 03:20.070
You can use the small H.

03:20.100 --> 03:25.170
So I would highly recommend you guys to go through this because this is one of the key thing which is

03:25.170 --> 03:28.830
going to play a vital role while working with date time formatter.

03:28.830 --> 03:31.620
And it also has some reserved keywords.

03:31.620 --> 03:38.190
If you take a look at it, hash open curly braces and close curly braces, those you cannot use as part

03:38.190 --> 03:43.290
of your date time formatter pattern because those are reserved keywords.

03:44.020 --> 03:44.980
When I go to the IntelliJ.

03:45.250 --> 03:46.100
There we go.

03:46.120 --> 03:49.360
In here, what I'm going to do, I'm going to create a class.

03:52.610 --> 03:53.420
Formatting.

03:55.400 --> 03:55.850
Local.

03:57.220 --> 03:57.730
Eight.

03:58.990 --> 03:59.800
Example.

04:00.870 --> 04:04.890
And I'm going to make this class executable by adding public static void main method.

04:06.370 --> 04:07.360
Presentation mode.

04:11.680 --> 04:14.920
First thing that I'm going to do is I'm going to create two methods.

04:15.160 --> 04:16.450
I'm going to be public.

04:19.500 --> 04:20.070
Boyd.

04:20.780 --> 04:23.720
Going be PA's local date.

04:25.690 --> 04:27.580
Then the another method is going to be.

04:28.860 --> 04:29.370
Format.

04:29.370 --> 04:30.030
Local date.

04:32.880 --> 04:33.390
Format.

04:33.420 --> 04:34.110
Local date.

04:34.720 --> 04:34.950
So.

04:36.090 --> 04:38.220
Parse is like converting.

04:39.920 --> 04:42.680
A string to a local date.

04:45.400 --> 04:48.460
Right now I'm going to define a string format.

04:48.460 --> 04:51.130
String date is equal to.

04:52.210 --> 04:59.320
Zero 18 hyphen zero 428 So this is my string eight format.

04:59.350 --> 05:01.570
I want to convert this to a local date.

05:01.600 --> 05:02.590
How do I do it?

05:03.030 --> 05:06.340
So I'm going to create a local date instance and give it a name.

05:07.120 --> 05:10.480
Here what I'm going to do, I'm going to use a date time formatter.

05:10.480 --> 05:13.030
Dot Oh, not the date time formatter we have.

05:13.270 --> 05:16.390
There is a handy method called parse and local date itself.

05:16.840 --> 05:18.600
Local date dot parse.

05:18.610 --> 05:20.790
If you take a look at it, there are two different versions.

05:20.800 --> 05:25.150
I'm going to use the first version and then we will explore the second version in a bit.

05:25.300 --> 05:27.760
So in the parse, I'm going to parse this local date.

05:30.100 --> 05:37.060
So if we go to this, it uses something called the ISO local date format by default.

05:37.060 --> 05:38.860
Let's go and check what is the format.

05:38.890 --> 05:44.230
If you remember from our presentation, we went to the website and checked the date format.

05:44.230 --> 05:44.500
Right.

05:44.740 --> 05:51.430
So local date expects the year in four characters, followed by the dash and followed by the month in

05:51.460 --> 05:56.410
two characters and and a dash and followed by the day of the month in two characters.

05:56.620 --> 06:01.390
Does it match our actual string date format, the string date that we have?

06:01.420 --> 06:02.590
Yes, it does.

06:02.620 --> 06:05.050
Now what I'm going to do, I'm going to print this and then check.

06:07.990 --> 06:09.070
Being local date.

06:12.550 --> 06:15.160
So I'm gonna run this and check the result.

06:16.230 --> 06:17.820
I have not called this method yet.

06:18.000 --> 06:20.220
What I'm going to do, I'm going to call both the methods.

06:21.370 --> 06:24.520
Thus and format local date to.

06:26.670 --> 06:27.060
Renders.

06:27.060 --> 06:29.520
It is going to call this method and then print the result.

06:32.230 --> 06:32.720
There you go.

06:32.740 --> 06:36.580
It successfully converted the string to a local date.

06:37.150 --> 06:39.550
What is the other way of doing this?

06:40.260 --> 06:42.840
Let's say I'm going to use the second version of it.

06:42.840 --> 06:48.420
I want to pass the data formatter so we know the data formatter has a.

06:49.820 --> 06:53.050
It informator as a local rate.

06:53.060 --> 06:54.890
This is a format which we have to pass.

06:54.980 --> 06:55.940
We do this.

06:56.850 --> 07:02.530
This again equivalent to this because behind the scenes parse uses a local date.

07:02.550 --> 07:04.440
Here we are explicitly mentioning it.

07:05.650 --> 07:07.810
I'm gonna call this.

07:09.220 --> 07:12.280
And I'm going to print this method, print this local date one.

07:12.910 --> 07:14.440
Let's run this and check the result.

07:16.460 --> 07:16.980
There you go.

07:17.050 --> 07:19.300
It printed the result as expected.

07:19.330 --> 07:22.870
Now, let's say you have a date like this.

07:23.570 --> 07:26.360
Spring date one equal to.

07:27.800 --> 07:34.520
2018 zero 428 So this is yn y y y y.

07:36.140 --> 07:41.280
And then basically capitalism then.

07:42.260 --> 07:42.820
Right.

07:42.830 --> 07:45.710
So how can we parse this?

07:45.740 --> 07:47.360
You cannot use this.

07:47.540 --> 07:50.840
Have to pass a different datum parameter.

07:50.990 --> 07:56.840
So I'm going to change this variable name so that we can get it of the compilation issue In here, there

07:56.840 --> 08:03.980
is something called basic ISO date, and we covered this as part of the presentation when I talked about

08:03.980 --> 08:05.900
what are the different formats that it supports.

08:05.900 --> 08:06.980
We talked about it.

08:07.050 --> 08:10.010
We go here, there is no dash in between.

08:10.010 --> 08:15.530
Basically this is going to be here followed by the month and followed by the day of the month.

08:17.040 --> 08:17.460
In this.

08:17.460 --> 08:20.310
It is going to give me the local date instance.

08:21.390 --> 08:24.020
Going to copy the local data and then put it here.

08:24.620 --> 08:26.360
Run this and check the example.

08:27.080 --> 08:27.860
Check the result.

08:29.200 --> 08:31.330
We take a look at the result, we got into some issues.

08:31.330 --> 08:32.170
Let's go ahead and check it.

08:32.170 --> 08:33.190
What is the issue?

08:33.280 --> 08:34.510
The 2018?

08:34.540 --> 08:35.910
That is fine.

08:35.920 --> 08:38.560
Or here we are using the basic ISO date.

08:38.980 --> 08:40.480
That is also correct.

08:41.050 --> 08:43.690
Let's take a look at the date that we are passing.

08:43.690 --> 08:48.220
So here we have to pass the date one instead of date, right?

08:48.370 --> 08:54.070
Because we are trying to parse, we are trying to use the basic ISO date format, but we were passing

08:54.070 --> 08:55.300
the date as an input.

08:55.300 --> 09:01.360
So if you have any kind of exception when it is trying to convert a string to a date and it is not,

09:01.360 --> 09:05.230
if it is not able to convert, then you will get this date time parse exception.

09:05.650 --> 09:08.350
That is something which you have to handle it in your code.

09:08.770 --> 09:10.360
So now I'm going to run this.

09:10.390 --> 09:11.260
We run this.

09:11.290 --> 09:14.860
Basically, this is going to give me the result as expected.

09:15.100 --> 09:15.880
Take a look at it.

09:15.910 --> 09:18.430
It printed the result as expected.

09:18.460 --> 09:25.480
Now these are the predefined DateTime parameters constants, which has the predefined format in it.

09:25.480 --> 09:29.060
And then we have used that to convert a string to a local date.

09:29.090 --> 09:32.030
Now let's talk about some custom defined date format.

09:32.360 --> 09:32.960
Right?

09:33.290 --> 09:36.740
So custom find date format.

09:37.250 --> 09:41.450
So in here, what we are going to do, I'm going to have a date.

09:45.510 --> 09:48.330
Wannabe date two in here.

09:48.330 --> 09:51.240
What we are going to do have a date like this.

09:51.270 --> 09:52.440
Those in 18.

09:53.680 --> 09:58.210
Pipe symbol, followed by zero for pipe, symbol and 28.

09:59.880 --> 10:01.030
So this is our date format.

10:01.050 --> 10:04.320
We want to convert this to a local date.

10:04.710 --> 10:06.440
We take a look at the date formatter.

10:06.450 --> 10:10.320
There is no date format constant that supports this.

10:10.350 --> 10:13.290
Those kind of use cases we have to build our own.

10:13.620 --> 10:15.330
That's going to be date formatter.

10:15.570 --> 10:17.190
Give it a name then.

10:17.190 --> 10:21.120
DateTime formatter dot of pattern in the pattern.

10:21.120 --> 10:22.110
What we are going to do.

10:22.140 --> 10:29.730
We are going to pass the actual pattern y y y y followed by the pipe and then the month representation

10:29.730 --> 10:32.640
is always in capital letter that you have to be aware of.

10:32.670 --> 10:35.390
Followed by that we'll be passing the small d.

10:36.500 --> 10:40.220
So now using this, we will convert this to a local date.

10:40.490 --> 10:44.000
So we are going to use the same local date dot parse method.

10:44.000 --> 10:49.700
If you take a look at it, there is a two parameter version which takes a date and followed by the date

10:49.820 --> 10:50.830
time parameter.

10:50.890 --> 10:52.760
I'm going to pass the date to.

10:53.640 --> 10:54.570
And then.

10:55.430 --> 10:56.780
The date time format.

10:58.920 --> 11:01.230
You're going to give us a local date.

11:02.810 --> 11:03.940
Want to give it a name?

11:04.840 --> 11:06.420
In this and check the result.

11:08.070 --> 11:08.850
Going to be.

11:11.170 --> 11:11.860
83.

11:13.400 --> 11:14.510
Then Localdate three.

11:14.780 --> 11:17.540
Now let's run this example and check the result.

11:20.470 --> 11:21.130
Take a look at it.

11:21.160 --> 11:25.300
It successfully parsed the string to a local date.

11:25.630 --> 11:27.640
Now you have something like this.

11:30.310 --> 11:31.330
Instead of.

11:32.180 --> 11:33.500
The pipe.

11:33.500 --> 11:34.880
You have a star.

11:36.970 --> 11:37.480
Right.

11:37.480 --> 11:42.370
So in that kind of in those kind of scenarios, basically you have to build another parameter.

11:43.880 --> 11:45.040
Want to give it a name.

11:45.830 --> 11:47.470
Then the place of pipe.

11:47.470 --> 11:48.700
We will use Star.

11:54.420 --> 11:55.950
Going to be a local date.

11:57.460 --> 12:02.470
Played for coal to local did not pass.

12:03.600 --> 12:06.120
Then we are going to pass the date as date three.

12:06.960 --> 12:10.110
Followed by the local quarry.

12:10.110 --> 12:11.370
The day time formatter.

12:11.870 --> 12:13.230
One way date DateTime formatter one.

12:18.780 --> 12:20.460
Going to be local date for.

12:20.790 --> 12:22.320
And local date for.

12:22.680 --> 12:25.410
So it could parse the date as expected.

12:26.550 --> 12:29.100
It successfully parsed the date as expected.

12:29.430 --> 12:31.020
Let's say you have hash.

12:31.780 --> 12:32.740
Wait, we talked about it.

12:32.770 --> 12:35.150
Hash is something which is a reserved keyword.

12:35.170 --> 12:39.010
You cannot use this as part of your gate pattern.

12:39.100 --> 12:40.780
If I try to use it, what would happen?

12:41.600 --> 12:42.800
Run this and check the result.

12:45.450 --> 12:47.280
We take a look at it, you get an exception.

12:47.280 --> 12:48.420
What is that exception about?

12:50.060 --> 12:54.890
Legal argument exception pattern includes reserved character.

12:55.010 --> 12:57.770
Ash So this is something which you have to be aware of.

12:58.220 --> 13:00.850
So I'm going to put it back to its old value, which is.

13:02.980 --> 13:07.280
So these are the different examples of parsing a string to a local date.

13:07.310 --> 13:12.150
Now we will talk about how to convert a local date to a string.

13:12.180 --> 13:14.900
So what I'm going to do, I'm going to.

13:15.790 --> 13:18.250
Copy this pattern and put it here.

13:18.250 --> 13:26.800
So I have a local date instance which is going to be local date, local date, equal to local date dot

13:26.800 --> 13:27.300
now.

13:27.310 --> 13:28.420
So this is my local date.

13:28.420 --> 13:31.930
I want to convert this date to a string.

13:31.960 --> 13:32.950
How do I do it?

13:33.220 --> 13:35.560
So we take a look at the local date.

13:36.010 --> 13:41.100
There is a handy method called format and then it takes in an input called date time formatter.

13:41.260 --> 13:48.010
So I'm going to pass this date time formatter instance, going to give me the string as an output.

13:48.010 --> 13:51.130
If we go to the format method, I'm going to give it a name.

13:51.130 --> 13:52.810
It's going to be formatted.

13:53.730 --> 13:54.090
It.

13:55.080 --> 13:55.740
Then.

13:57.670 --> 13:58.330
Going to.

13:59.250 --> 14:05.490
You me, the date in the format that we have defined here, basically year pipe, month, pipe.

14:05.490 --> 14:06.120
And then.

14:07.170 --> 14:07.440
Eight.

14:08.160 --> 14:11.340
Basically the day I'm going to run this example and then check the result.

14:12.830 --> 14:18.740
If you take a look at the result, it's successfully converted the local date, a string which is of

14:18.740 --> 14:20.570
the format that we define here.

14:20.600 --> 14:21.980
Let's say you want.

14:23.260 --> 14:23.970
Start here.

14:25.650 --> 14:25.980
Right.

14:26.550 --> 14:31.920
So if I run it, the string that is going to have star in the place of.

14:34.440 --> 14:40.740
So this is all about converting a date to a string and string to a date using the DateTime formatter.

14:41.040 --> 14:47.580
Let's go ahead and explore how to convert a time to a string and string to a local time in the next

14:47.580 --> 14:48.270
tutorial.

14:48.300 --> 14:50.160
With this, we came to the end of this tutorial.

14:50.190 --> 14:51.300
Thank you for watching.
