Hi everyone.
Welcome back to this tutorial.
In this tutorial, we'll explore some of the additional methods that Localdate supports.
Before I get into that, I would like to explain one more scenario which I missed to explain in the
previous tutorial.
Now I want to attract the years by one.
How do we do it?
Normally we can use the minus years, but there is another approach which we can do if we want to subtract
the years localdate dot minus years.
If you provide the minus years value as one, it is going to subtract the year by one.
But if you take a look at it, there is a there is another version of minus that is available here.
We take a look at it minus long, the amount to subtract and then the temporal unit.
Let's explore that.
So in here, what we have to do is I want to subtract the year by one.
And what is other attribute that it accepts?
It accepts a temporal unit.
The temporal unit is an interface that is part of the Java dot time package.
And then.
There is something called Chrono Unit which actually implements a temporal unit.
The Chrono Unit is an enum that implements a temporal unit.
Let's see, what are the different attributes that it supports?
If we take a look at it, it supports minutes, years, seconds, months and all those things.
So our use case is we want to subtract the year by one, right?
So in that case, we will pass this and let's give a comment.
You know, unit minus.
Okay.
Let's go ahead and run this one.
If we take a look at the Chrono unit, minus two is 2017.
So we subtracted the year by one using this.
Again, this is one another approach of subtracting the year.
All these methods are really handy and each and every method, what are we covered?
There is an alternative for each and every approach, but ultimately it is going to give you the same
result.
It's your comfort to choose what are options that you feel comfortable with.
Now let's explore some of the additional support methods.
Additional.
A port.
Let's say you want to check whether this year is a leap year or not.
As a handy method available for that one to local date dot is leap year.
This will tell you is 2017 not sorry, 2018 is a leap year or not.
Here and 2018 is not a leap year.
It's going to give you the result as false.
Right.
Let's see which year is leap year.
2020 is a leap year.
What I'm going to do, I'm going to do local date dot of.
I'm going to create a date.
Local date dot off and I'm going to pass the year as 2020 and the day as zero one.
If you run this, this is going to give me the result as true here.
And then I want to perform what is leap year, right?
If you do not do that, it is not going to check whether it is a leap year or not.
Run this.
Take a look at the result.
It is true.
Previously the result came as false.
Let's explore some more additional different methods.
Let's say you want to check whether the two dates are equal or not.
So here we have local date, which is a current date, and then the local date one, which is the date
which is next to this day, which is 17.
So the current date is actually 16.
July 2018.
Okay, now let's compare this.
When I explore there is a method called is equal.
Zequel.
And then what is the input?
It accepts a chronological date, which is nothing but the local date.
The local date one.
So local date.
Let's put this comment here.
Local date is 16th July.
2018 and a local date one.
This is 17th July.
Chocolate one on 17th July.
2018.
If you run this.
Put some comments here so that it will be easy to read from the console.
Run this it's going to return false because is these dates are not equal.
Local date represents 16th July local date.
One represents 17th July.
Now we will explore the another method which is is before is a local date.
One before local date is local date before local date one.
Yes, because local date is 16th of July and local date one is 17th of July.
And now this is going to return the value as.
If you take a look at the result, the result came as true.
Now there is something called ease after.
Now I'm going to change this instance a little bit.
I'm going to have local date.
One is before local date is after, right.
Is after.
The method that we're exploring is after local date one.
So 17th, 17th of July is local date one, which is after the 16th of July.
It is going to give us the result as.
It gave you the result as true.
These are the handy methods which you can use in order to compare two dates.
There are a lot of different advanced methods which I'll be covering in the future tutorials, but for
now this is one simple options.
These are the options that we have in order to compare the dates.
And these are intuitive.
If you take a look at the method name, just by looking at the method name, you can say what is that
functionality that it is going to perform.
And now I'm going to give you some examples of not unsupported scenarios.
Let's go ahead and explore that.
I'm going to give something like unsupported.
So here, this is completely local data, right?
We are talking about date functionalities.
Let's say you want to try to subtract a time from a date.
What would happen?
Let's explore that.
I'm going to use this example here and then put it here in here what I'm going to do in the place of
this, I'm going to give the value as.
Minutes.
Local date doesn't have a minutes at all.
If you try to perform this.
What would happen?
Run this and check if you run this.
Basically, this is going to give you something like.
Unsupported temporal type exception unsupported unit minutes because you are working with date here,
you cannot try to perform a date functionality.
Time functionality on a date object.
So this is something which you have to keep in your mind.
There is something called is supported.
That is something which you can use in order to check whether this particular operation is supported
or not.
I'm going to show you that is supported.
What this is going to do is local date Dot is supported.
So this is a method and then you can pass the unit, the Chrono unit dot units.
Now, in our use case, this is going to print false because minutes is not supported.
Go ahead and run this and check.
Could he supported value came as false.
If you give years, let's see what it is going to print.
He supported us through so.
Using this, you can find out whether can you perform that kind of operation on this object or not.
So these are the different additional support methods that we have as part of local.
You can actually use in your use cases.
With this, we came to the end of this tutorial.
Thank you for watching.