Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about DateTime formatter.
This class is introduced in Java 1.8 and is part of the Java dot time dot format package.
This class really made the formatting and parsing easy.
This class is used to format and parse the local date, local time and local date and time.
I would like to highlight the difference between the definitions of parsing and formatting here.
Oftentimes it leads to confusion.
Parsing in datetime formatter context refers to converting a string to a local date or local time or
local date time formatting in DateTime formatter context refers to converting a local date or local
time or local date time to a string.
Before we get into the coding part, I would like to go to the Java documentation of DateTime formatter
in their website and explain you about some of the necessary information that we have to be aware of
when we go to the browser and then search for date time formatter and then select the first link which
has date time formatter Java platform C eight.
Click on that.
This will take you to this page.
I'm going to highlight some of the things to consider while working with DateTime formatter.
Here.
The first thing is it comes with some predefined constants.
If you take a look at it, such as ISO local date, you go to this.
This is one of the format the DateTime formatter supports by default.
And if you take a look at the format here, followed by the month and followed by the date, if your
use case is to convert a string to a local date and local date to a string and which is of this format,
then you can make use of this ISO local date constant.
And if you take a look at this example ISO offset date, it has a similar ISO local date format, but
in addition to that it has the offset information attached to it.
And if you take a look at ISO date, it supports both the ISO local date and the ISO offset date.
Similar to the date it has the support for time, which is the local time and also local date time.
I would highly recommend you guys to go through this documentation and then check whether the format
that you are looking for is already available as part of the data formatter.
Then you can make use of those constants in your application.
I'm going to go to the top and then highlight some of the key things to consider while working with
date time formatter.
Let's go from top to bottom.
If you are going to work with year, then you can either use U or Y.
You're going to work with date.
In that case, you can either use capital D or small D, The capital D represents the day of the year.
So if you take a look at it, the number can go until 365.
If you want to use the day of the month, then you have to use the small d you're going to work with
month.
In that case, you can use the capital M and you cannot use small M because small M corresponds to the
minute of R, And if you want to work with the 24 hour format, in that case you have to use capital
H for the 12 hours.
You can use the small H.
So I would highly recommend you guys to go through this because this is one of the key thing which is
going to play a vital role while working with date time formatter.
And it also has some reserved keywords.
If you take a look at it, hash open curly braces and close curly braces, those you cannot use as part
of your date time formatter pattern because those are reserved keywords.
When I go to the IntelliJ.
There we go.
In here, what I'm going to do, I'm going to create a class.
Formatting.
Local.
Eight.
Example.
And I'm going to make this class executable by adding public static void main method.
Presentation mode.
First thing that I'm going to do is I'm going to create two methods.
I'm going to be public.
Boyd.
Going be PA's local date.
Then the another method is going to be.
Format.
Local date.
Format.
Local date.
So.
Parse is like converting.
A string to a local date.
Right now I'm going to define a string format.
String date is equal to.
Zero 18 hyphen zero 428 So this is my string eight format.
I want to convert this to a local date.
How do I do it?
So I'm going to create a local date instance and give it a name.
Here what I'm going to do, I'm going to use a date time formatter.
Dot Oh, not the date time formatter we have.
There is a handy method called parse and local date itself.
Local date dot parse.
If you take a look at it, there are two different versions.
I'm going to use the first version and then we will explore the second version in a bit.
So in the parse, I'm going to parse this local date.
So if we go to this, it uses something called the ISO local date format by default.
Let's go and check what is the format.
If you remember from our presentation, we went to the website and checked the date format.
Right.
So local date expects the year in four characters, followed by the dash and followed by the month in
two characters and and a dash and followed by the day of the month in two characters.
Does it match our actual string date format, the string date that we have?
Yes, it does.
Now what I'm going to do, I'm going to print this and then check.
Being local date.
So I'm gonna run this and check the result.
I have not called this method yet.
What I'm going to do, I'm going to call both the methods.
Thus and format local date to.
Renders.
It is going to call this method and then print the result.
There you go.
It successfully converted the string to a local date.
What is the other way of doing this?
Let's say I'm going to use the second version of it.
I want to pass the data formatter so we know the data formatter has a.
It informator as a local rate.
This is a format which we have to pass.
We do this.
This again equivalent to this because behind the scenes parse uses a local date.
Here we are explicitly mentioning it.
I'm gonna call this.
And I'm going to print this method, print this local date one.
Let's run this and check the result.
There you go.
It printed the result as expected.
Now, let's say you have a date like this.
Spring date one equal to.
2018 zero 428 So this is yn y y y y.
And then basically capitalism then.
Right.
So how can we parse this?
You cannot use this.
Have to pass a different datum parameter.
So I'm going to change this variable name so that we can get it of the compilation issue In here, there
is something called basic ISO date, and we covered this as part of the presentation when I talked about
what are the different formats that it supports.
We talked about it.
We go here, there is no dash in between.
Basically this is going to be here followed by the month and followed by the day of the month.
In this.
It is going to give me the local date instance.
Going to copy the local data and then put it here.
Run this and check the example.
Check the result.
We take a look at the result, we got into some issues.
Let's go ahead and check it.
What is the issue?
The 2018?
That is fine.
Or here we are using the basic ISO date.
That is also correct.
Let's take a look at the date that we are passing.
So here we have to pass the date one instead of date, right?
Because we are trying to parse, we are trying to use the basic ISO date format, but we were passing
the date as an input.
So if you have any kind of exception when it is trying to convert a string to a date and it is not,
if it is not able to convert, then you will get this date time parse exception.
That is something which you have to handle it in your code.
So now I'm going to run this.
We run this.
Basically, this is going to give me the result as expected.
Take a look at it.
It printed the result as expected.
Now these are the predefined DateTime parameters constants, which has the predefined format in it.
And then we have used that to convert a string to a local date.
Now let's talk about some custom defined date format.
Right?
So custom find date format.
So in here, what we are going to do, I'm going to have a date.
Wannabe date two in here.
What we are going to do have a date like this.
Those in 18.
Pipe symbol, followed by zero for pipe, symbol and 28.
So this is our date format.
We want to convert this to a local date.
We take a look at the date formatter.
There is no date format constant that supports this.
Those kind of use cases we have to build our own.
That's going to be date formatter.
Give it a name then.
DateTime formatter dot of pattern in the pattern.
What we are going to do.
We are going to pass the actual pattern y y y y followed by the pipe and then the month representation
is always in capital letter that you have to be aware of.
Followed by that we'll be passing the small d.
So now using this, we will convert this to a local date.
So we are going to use the same local date dot parse method.
If you take a look at it, there is a two parameter version which takes a date and followed by the date
time parameter.
I'm going to pass the date to.
And then.
The date time format.
You're going to give us a local date.
Want to give it a name?
In this and check the result.
Going to be.
83.
Then Localdate three.
Now let's run this example and check the result.
Take a look at it.
It successfully parsed the string to a local date.
Now you have something like this.
Instead of.
The pipe.
You have a star.
Right.
So in that kind of in those kind of scenarios, basically you have to build another parameter.
Want to give it a name.
Then the place of pipe.
We will use Star.
Going to be a local date.
Played for coal to local did not pass.
Then we are going to pass the date as date three.
Followed by the local quarry.
The day time formatter.
One way date DateTime formatter one.
Going to be local date for.
And local date for.
So it could parse the date as expected.
It successfully parsed the date as expected.
Let's say you have hash.
Wait, we talked about it.
Hash is something which is a reserved keyword.
You cannot use this as part of your gate pattern.
If I try to use it, what would happen?
Run this and check the result.
We take a look at it, you get an exception.
What is that exception about?
Legal argument exception pattern includes reserved character.
Ash So this is something which you have to be aware of.
So I'm going to put it back to its old value, which is.
So these are the different examples of parsing a string to a local date.
Now we will talk about how to convert a local date to a string.
So what I'm going to do, I'm going to.
Copy this pattern and put it here.
So I have a local date instance which is going to be local date, local date, equal to local date dot
now.
So this is my local date.
I want to convert this date to a string.
How do I do it?
So we take a look at the local date.
There is a handy method called format and then it takes in an input called date time formatter.
So I'm going to pass this date time formatter instance, going to give me the string as an output.
If we go to the format method, I'm going to give it a name.
It's going to be formatted.
It.
Then.
Going to.
You me, the date in the format that we have defined here, basically year pipe, month, pipe.
And then.
Eight.
Basically the day I'm going to run this example and then check the result.
If you take a look at the result, it's successfully converted the local date, a string which is of
the format that we define here.
Let's say you want.
Start here.
Right.
So if I run it, the string that is going to have star in the place of.
So this is all about converting a date to a string and string to a date using the DateTime formatter.
Let's go ahead and explore how to convert a time to a string and string to a local time in the next
tutorial.
With this, we came to the end of this tutorial.
Thank you for watching.