Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the next date and time library, which is the local date time.
For this purpose, I'm going to create a class.
Called local date time example.
Let's make this class executable by adding public static void main method.
Now, how can we create an instance of local date time?
So the way to create is local date time dot now.
So there are three different versions.
For now, we are going to focus only on this version.
The zone and Clock is something which will be covering when we cover the time zones.
Dot Now this is going to give me an instance of local date time.
Let's print this and check the machines.
Current time.
A secret date and time.
I'm gonna turn.
This is going to be local date and time.
Let's run this and check the result.
So the current date and time is 17th of July and the time is 555 and 54 seconds and 994 is a nanosecond
precision.
Let's take a look at what are the different ways of creating a local date time instance.
The local date time instance as a factory method called off using which you can create a local date
time instance.
If you take a look at it, there are many options available here.
What I'm going to do, I'm going to cover a few of these in this tutorial, but I would highly recommend
you guys to go through and explore all the options that are available here.
Now what I'm going to do, I'm going to pass the year and then the month and then the date and hour,
minute, second and nanosecond.
There you go.
I'm going to put it down so that it will be visible.
It'll fit the whole screen.
Now what I'm going to do, I'm going to create a instance out of it.
So this is a instance.
Let's print this and check the result on how the local datetime instance looks like with the values,
whatever that we passed.
Let's run this when we run this.
It gave you the result as as you see, this is the date which we passed here, meaning 2018, zero 321
and T is a separator which splits the date from the time.
And then we gave the time as 23 hours, 33 minutes and 33 seconds, powered by the nanosecond precision.
Now let's explore another option, which is really, really handy.
Local date time of.
Take a look at the first option here.
It takes in a local date and local time.
Let's do this.
I'm going to pass the local.
I'm going to use the local date class and then call the now method, which is going to give me the current
machines local date and followed by that I'm going to pass the local time dot now, which is going to
return me the current machine's local time.
This is going to return me, the local datetime instance.
I'm going to give it a name and have it here.
Let's print this and check the result.
So I'm going to put it here.
I'm going to put it here.
So here we are passing the local date and time instance.
That will give you the local date time together as a response, which is of type local date time and
then give it a given name here.
That's what we are printing here.
Let's go ahead and run this and check the result.
We take a look at it.
The date and time is the current machines, local date and time, which is separated by a T so it combined
and gave the local date time as a output.
Now let's take a look at what are the different ways of getting the values from the local date time
instance.
Let's say I want to get the hour from the local date instance.
How do we do it?
Getting the time and date from local date time instance.
So first thing is I want to get the ah, how do we do it?
I'm going to have this text here.
So I'm going to use this local datetime instance dot if I give the get.
There are many options available.
I have both the R and D methods available because this local date time is a combination of both date
and time.
So I just need the R right.
I run this.
This is going to print the r that is associated with the local date time and the R six, which is 6
a.m. in the morning here.
Now what I'm going to do, I need the minute.
How do we do it?
So there is a handy method called get minute.
I'm going to use that method and print the result.
There you go.
It printed the minute as 615.
We take a look at the current machines time, the machines, current time it is 615.
So and we got the 15 out of it.
Let's say I want the day.
How do we do it?
There is a method called Get day of the Month.
We give this value going to print the day of the month.
The day of the month is 17.
So these are the different options that are available in order to get the values from the local datetime.
And it also supports the width method.
Let's explore a couple of those.
Local date.
So if we do with.
That is the way of modifying.
Let's cover this.
When we actually modify the when we want to modify the local daytime instance, there is a get method
that is the one which I wanted to cover.
So if you do a get, it accepts a temporal field, right?
So in here we can pass Chrono Field dot one the day of the month.
There we go.
And then this is going to give you the day of the month.
But here, if you take a look at it, we are using another approach in order to get a parameter or in
order to get an attribute from the local date time instance.
I'm going to run this example.
There you go.
It gave the value as 17 because that is the day of the month.
Now let's go ahead and check what are the different options of modifying the values.
In local daytime.
Let's give a simple code command in order to differentiate this from the getting getting the values
from the local daytime.
Modifying local daytime.
So let's say I want to add hours to it.
I want to increase the hours by two.
How do we do it?
So I'm going to use the same local daytime dot you can do.
Plus it has both the days and hours methods supported as part of local daytime.
So I'm going to use plus hours and increase the hours by two.
Let's put this.
Was wrong button.
So in here, I'm going to print this in the console.
Let's put this this out.
And then in here, I'm going to use the plus hours to put the plus hours.
And let's run this and check the result.
Right now, the result will give you 8 a.m. as a result.
If you take a look at the time, the time change from 6 to 8, the current time is six.
It changed the time from 6 to 8 because we have added two hours to the current time.
Now I want to subtract the days.
How do we do it?
There is a handy method, which is the minus which we have seen as part of the local date and local
time tutorials.
So I want to minus the days by two.
So this is going to reduce the date by two.
The current date is 17.
This is going to change the date to 15.
Let's run this and check.
We take a look at it.
The date changed from 17 to 15.
There you go.
Let's take a look at the width method.
So what's going to be local date time dot width.
Let's say you want to change the month.
I want to change the month to 12.
So the width method is handy.
In order to modify the dates you can still use.
Like what is a current month?
The current month is seven.
If you want to change that to 12, you will add a plus five.
So with month, with month is going to change the local times month to 12.
Then let's run this and check the result.
Here we go.
It changed a month from 5 to 12.
So these are the different handy options which you can use in order to modify the local date and time.
There are many more options available.
I would highly recommend you guys to go ahead and explore it and raise any kind of question in the Q&A
section.
I would be able to answer you as soon as I can with this.
We came to the end of this tutorial.
Thank you for watching.