Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore how to format and parse a local time using DateTime formatter.
We're going to create a class called.
Murmuring.
Local time example.
Want to make this class executable by adding.
Public static void main method.
Let me put the presentation mode.
Here we go.
I'm going to create two methods.
To be public.
Derek.
White.
Pass time.
And then the another method is going to be public.
Eric Boyd, former time.
So we're going to follow the same approach that we used for the local date.
But in here we're going to talk about format local time and pass local time.
The first thing is I'm going to use.
Ring because pass is something which is going to convert a string to a local time, right?
So I'm going to give it a time.
Going to be 1300.
There you go.
Now what I'm going to do, I'm going to use the local time dot parse method.
So let's use the one parameter version and then we'll cover the parameter version in a bit.
So in here, I'm going to pass the time.
You go to this method, it is going to use the local time in the scenes for you.
You take a look at it, it is expecting a R and two digits followed by the colon and then in it in two
digits, the optional start rate.
So if there is a second, it is going to consider that If there is no second, then it is not even going
to consider that.
So in here, I'm going to give you a local time.
I'm going to give it a name.
So let's print this and check the result.
There you go.
Now I'm going to call both of these methods.
Pastime then.
Format.
Time.
So now what we will do is we will run this example and check the result.
There you go.
It successfully passed the time.
Basically, it converted the string to a local time.
Now let's explore the two parameter version.
Right.
Local time dot parse.
I'm going to pass the same time followed by that I'm going to use the datetime formatter and we are
going to use datetime formatter.
We're going to use the ISO.
Not coming up at.
It informative that it's not be this I used to local time.
Right.
So this is a format which is going to support the R colon the minute.
Format.
I'm going to give it a local time.
And it's gonna give it a name.
And let's print this and check the result.
So if I copy this and put it here, this is going to print the time as expected.
At the time as expected.
Now the next step is the custom defined format.
No comment here.
Custom defined format.
So in here, what is that going to happen is that we're going to have a time.
Ring.
Time one is going to be 13 DA and then 100.
Right.
So this is a custom custom format, which is there is no predefined time in this format and the date
time format class.
Now we are going to create a formatter, the time formatter equal to date in formatter dot of pattern.
The pattern we provide which I want the result in 24 hour format.
That's the reason why I'm using the capital H.
We want the date in a color format.
Then you can use one h and then followed by that it's going to be small M because Capital M represents
the month.
That is something which you have to keep in your mind.
So I'm going to use the local time dot parse.
So we're going to pass the time one followed by the date time formatter that we just created.
There you go.
So now it's going to give you the local time.
I'm going to assign that to a variable, then print the result.
No result.
Go run this.
This is going to give me the local time instance as expected.
We want can play with this by changing this one from capital hedge to small edge.
Run this and check the result.
Take a look at it has some issues.
Let's go ahead and take a look at it.
So the church is expecting only the ranges between 1 to 12.
If you give 13, it is not going to have any clue how to put that value in the color format.
So I'm going to put it back to its old value.
We go run the example and make sure working is expected.
But right now, I'm going to create a time.
Spring time to equal to.
1300.
So I'm going to use the same star.
1300 zero two.
We have seconds also in it.
In those kind of use cases, you're going to define a new pattern.
The name going to have to just represents the seconds.
So I'm going to use similar code here.
So in the place of.
Full time to I'm going to use local time three the place of time.
One, I'm going to use time two because time two is the one which represents the second included ring,
and then followed by that we're going to use the data formatter.
Now let's print this one.
Going to print the local time three.
And local time.
Three.
Run this and check the result.
So it gave you the second included date as expected.
So these are the different ways of converting a string to a local time.
Now, let's go ahead and explore how do you convert a local time instance to a string?
So the reverse approach.
The first thing is I want the date to be in this format.
One, the string that is going to be returned from the local time in this format.
So the first step is I want to create a time instance local time dot.
Now it's going to give you the time instance.
I'm going to give it a name.
There you go.
So we have the local time instance ready.
Let's convert this to a string using the format method, which is part of the local time.
So local time dot call.
The format method basically is going to accept.
Take a look at the format method.
This is going to accept a date time format, for instance.
I'm going to pass that.
So this is going to return a string.
Let's assign this result to a string.
String.
Formatted.
Thymus.
Let's print this and check the result.
So just to just to have some difference in the output, I'm going to have this value as pipe.
This value also as pipe.
If you take a look at it here, I have the current machine's time.
So let me print.
The current machine's time so that we'll be able to relate to the converted date.
So here.
We run it.
Going to give you the result, the pipe symbol.
So the current time of the machine is 650, which is in which is a m and then successfully converted
that to a string of the format that we define here.
So this is all about converting a string to a local time and local time to string.
For this.
We came to the end of this tutorial.
Thank you for watching.