Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore how to convert Java.util.date and Java dot SQL update to local
Date instance.
The first thing is I'm going to expand this data package and then I'm going to create a class called
Date.
To.
Local date.
Sample.
Then I'm going to make this class executable by adding public static void main method.
In presentation mode.
There you go.
Now, what we are going to do, I'm going to create an instance of date.
All to new date.
So let's import this.
I'm going to use the Java util conversion first.
The Java.util.date.
Let me put some comments here.
The first use case is Java dot util dot date.
So local.
And the next use case that we are going to code is it's going to be Java dot SQL dot date.
Cool local.
Pete and vice versa.
At the same year.
And you see.
Now let's print this and check the result.
What does this date instance?
Prince.
You take a look at it, it has both the date and its appropriate time, the time zone it belongs to,
and then the year.
Now, how can we convert this to a local date?
So the way to do it is.
Let's use the instance that we created.
And then it has a handy method called to instant.
Right.
So let's call that method.
And if you go to that method, there's got introduced as part of Java 1.8.
This is going to return an instant, an instance of instant, which is going to give you the current
time.
If you go to the get time method, this is going to give you this machine's current time.
And then.
It is going to call the add zone method.
We are going to call the zone method and then I'm going to pass the system's default time zone.
And if you press dot, it is going to give you the access to different conversions.
We take a look at it.
It has a local date, local date time also, and then local time and offset date and time.
But our use cases, we are just going to convert this to local date if we want to have if you have any
use cases where you want that local date and time, you can still use the local date and time method.
So I'm going to click, I'm going to select the two local date.
So this is going to give me the local date instance.
I'm going to give it a name.
Both the local date.
The local date is part of the Java dot time dot local date package.
So now I'm going to print it.
The local date.
Good.
Run this and check the result.
So the value is 2018 zero 720, which is July 7th, July 20th, 2018.
So we have successfully converted the Java.util.date to local date.
Now let's do the reverse version How to convert the local date instance to a java.util.date.
So I'm going to use the same instance.
Whatever we have used here I'm going to do.
First thing is date.
Let's give it a name equal to new date.
Art.
There is a handy method called from.
So if you take a look at this method, this method got introduced as part of 1.8 and it accepts something
called instant.
Our use case is we are going to convert this local date to instance.
Right now we just have the local date, right?
How can we add time to it?
So I'm going to use local date dot at time.
I'm going to use this machines time itself.
It's going to be local time dot now.
So this gave you the this will give you the local date and time as a output and then I'm going to use
the Add zone.
I'm going to use zone ID dot system default.
What are below so that the code will be clear.
And then.
After this, we are going to call the instant.
So this is what the from method is expecting.
Take a look at the front method.
It expects something called instance.
So using that, you'll be able to successfully convert this local date to a actual date instance, which
is Java.util dot.
Eight, actually.
So I'm going to pass a day to one.
Then check the result.
Here you go.
We got the result as Friday, July 20th, five 3014, and the time zone is cdti and the year is 2018.
So we have successfully converted Java.util dot date to local date and vice versa.
Now let's take a look at the Java dot SQL update and then we'll try to convert that to local date.
So I'm going to use Java dot SQL dot date.
So we're going to create an instance of.
Sequel dot date.
I'm going to give it a name.
It's going to be date two.
And then I'm going to be Java dot SQL dot date.
Got it has something called.
Value of.
We take a look at it.
The value of method.
We go and check this method.
It accepts something called local date and it can also accept a string.
Now our use cases we want to convert the JavaScript date to local date.
Right?
But the first use case we are covering how to convert from local date to Java dot SQL dot date.
Then I'm going to pass the local date instance that we created here.
Now.
If I print this.
So we have successfully converted the local date to Java dot SQL SQL dot date here.
When I printed here, you run this.
Basically that is going to give you an instance of JavaScript date from local date.
Now how do you convert a JavaScript date to a local date?
It has a handy method called date to.
Got to local date.
Very simple.
And then this will give you the local date as an output.
Okay.
Local date one.
And let's print it in the console.
Dot out dot then and.
Right here.
If you run it, it is going to give me the local data as a output.
So we have successfully converted Java dot SQL update to a local date and vice versa.
This section.
So basically these are existing dates, right?
If you have any use case to move from that existing date to local date in order to make use of the new
functionalities that got introduced as part of Java eight, then these are the different conversion
techniques that you can use in order to convert your current date versions, which is Java.util.date
and Java SQL dot date to a local date and vice versa.
With this, we came to the end of this tutorial.
Thank you for watching.