Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore how to format and parse the local date time using date time
parameter.
For this purpose, what I'm going to do, I'm going to create a class called formatting.
Local.
Date time.
Make this class executable by adding public static void main method.
Now let's create two methods.
For that.
Presentation mode.
Here the two methods are going to be static methods.
Public static.
Void pass local date time.
And then the next method is going to be.
Format.
Local date and time.
Thus.
Going to be format.
Local date and time.
So now I'm going to create a string.
So first thing is we are going to convert a string to a local date.
So let's give it a name.
Going to be 2018.
Zero four.
1880.
We all know the local dating format by default puts this T in between the date and time.
I'm going to have the time as 1433 and 33.
So this is a local date time.
We want to parse this.
So this is a local date time string.
We want to convert this to a local date time instance, right?
So how do we do it?
Let's use the local date time dot parse method and then parse this date time.
So even the parse method has two different versions of it.
One without the date time formatter and with the date time formatter, you go to this parse method,
it uses the local date and time.
We take a look at the local date and time.
It uses the local date and local time which is separated by a key.
So in here, let's see what is the output that it is going to give.
So here we are trying to convert a string to a local date time instance.
I'm going to print this in the console so that we can validate what is a value that gets mapped.
Now I'm going to call these two methods.
First one is going to be parse.
Local date and time, and the next one is going to be format, local date and time.
Want to run this example and check the result.
We take a look at the result?
Pass the string to a date time.
Now what I'm going to do, I'm going to use the another version of the parse method.
Local datetime dot parse, which takes in the string and the datetime formatter.
So that's going to be the datetime again.
Followed by that I'm going to use the datetime formatter.
Not going to be the date.
It's going to be local date time.
Right.
And then this is going to give us the local daytime as the output.
I'm gonna give it a name.
There you go.
And let's print this and check the result.
Take a look at the result.
This and check the result.
Though it gave the result as yet.
Now we are going to play with the custom date time format.
Going to be custom.
Format.
So in here, what I'm going to do, I'm going to create a string, right?
String.
And let's give it a name.
Date time one.
And then this is going to be.
Similar kind, but.
In the place of colon.
I'm going to have pipe.
Pipe.
So now we have to build our own datetime formatter instance because these are not these won't be available
as part of the DateTime formatter as a predefined format.
Now what we are going to do, we are going to create an instance within formatter.
Let's give it a name.
It's going to be datetime formatter dot of pattern.
So the pattern is y y y y hyphen.
The month we always represent in capital M followed by that date.
We represent that in small D and going to be.
A church which is in the 24 hour hour format and then followed by the let's give the pipe.
More.
Lemon And then.
Yes.
So this maps to the actual format that we have here.
Now, what we will do, we will call local date time dot parse will parse the date time one, which
is the new format.
And then we use the date time formatter that we just created.
So this is going to give you the local date time.
Give it a name.
Go.
Let's print this and check the result.
So daytime won in local.
Daytime won.
There you go.
Let's run this.
You run this?
We ran into an issue.
Okay.
Let's take a look at the issue in here.
We did not mention the T, That's mandatory, right?
If you do not, then it is going to have some issues.
Again, we have an issue.
If you take a look at the line which is causing the issue, the line is 25.
That is our new format.
I believe we have to provide this in single quotes, otherwise it's going to cost.
There you go.
It was able to successfully format the string.
Of our own format and it converted that to a local date using the datetime formatter that we just created.
Keep in your mind, whenever you are working with a local datetime, this is really important.
If you want to have the string to be successfully converted to a local date time.
Now I'm going to have another string.
And then in here, it's going to be day time to the place of tea.
It is going to have A, B, C.
In that case, will this work?
Won't work.
So in order to make this work, basically what we have to do, we have to provide ABC here.
That's the only way to make it work.
So we have this data formatter defined.
Now what we will do create a local datetime instance, give it a name, and then I'm going to use the
local datetime dot parse.
When I use this and then pass the date time to followed by that.
Let's pass the date time formatter.
One.
Then let's print this and check the result.
We print this.
It is going to give me the result that I'm looking for.
Run this and check the result.
There you go.
Was able to successfully format and if you take a look at it, the local datetime will always have tea
in between the date and time.
So that's what it has here.
Even though you have the string here as ABC, it will actually convert that to a appropriate local datetime
instance when it gets converted.
This will be replaced by tea.
So this is all about parsing a local datetime.
Now we will talk about how to convert a date to a string.
So I'm going to use.
Not date date time to a string.
So I'm going to use a same formatter here.
And then what I'm going to do, I'm going to create an instance of local date time.
Want to give it a name?
Going to be local date time.
Not now.
And then what we are going to do, we are going to use.
I'm going to define a converted datetime.
So in here I'm going to use the local datetime dot going to be the format and pass the formatter.
No, it is going to give you the string back with ABC in it in the place of T, because when the local
date time creates an instance, it's going to have a T by default and we are replacing that with ABC.
So I'm going to copy this.
Run this and check the result.
And thus we take a look at it.
It was able to successfully convert the local date time to the string format that we have defined here.
And then in the place of colon is a default rate.
We have pipe here was able to successfully convert that to a string.
So this is my current machines time, which is zero eight.
That's the reason why it is different from 1433 to this one.
So this is all about converting a local datetime to a string and string to a local datetime using datetime
parameter.
But this we came to the end of this tutorial.
Thank you for watching.