Welcome back and this video, we are going to add the functionality that allows us to calculate how
old we are in minutes because so far we saw that we can select a date.
But the thing is that we didn't use it yet.
So far, so far, we only created this post which said date pick works.
But now, instead of writing this text, let's try something like the year chosen or the chosen year.
Maybe that's better chosen year is.
Plus here.
All right, let's just try that real quick, because this year here, it's going to be that year that
is given from the set.
Listen up.
So on they said, listen, all right, we will have to look into that a little bit because it might
be a little confusing.
So let's select another year.
Let's select twenty seventeen and press, OK?
And you can see the chosen year is twenty seventeen.
Now let's click on it again.
Let's select this year and we see the chosen year is twenty nineteen.
So year and month and day of month are going to be the values that we get from our set listener.
So on date set listen up.
Which we then can use in order to calculate which time was selected and how many minutes are passed
since that moment.
All right.
What's important is that the year that we created here is not this year.
The month that we created here is not going to be this month.
And today is not this day.
But they are the variables that we use here at the bottom.
So we pass them to the date set listener as the value that should be set in the moment that the date
picker dialog is opened, because if you look at it, once you open it up, it uses the current date
and we got the current date by creating those variables and we pass it there.
But the ones that are the result are the ones that we get from here.
So we get the year, the month at the day of the month.
So now let's make this text a little longer.
Where we actually write a little more so.
Plus, or maybe we should use string interpolation, so this is going to be the year.
The month is.
Month and the date is.
Day of month.
OK.
So now we don't need to have this year there anymore.
And of course, it's day of month right now, if we run it again, let's see if we get to full detail.
All right, let's select a date.
Let's select the 10th.
He was 12, 19, the month was 11, the day was 10, so today was OK for some reason the month was not
OK.
Why is that?
Well, the month starts at zero, so we need to take that into consideration.
For some reason, decided or in this case decided that it would make sense to calculate the month starting
from zero.
So if I selected January 1st, you can see the month is zero two days one.
All right.
So that's something that we have to be careful with.
How about now we see that we get those values and we can use them.
Now, knowing all of that maybe makes sense to change the names here.
So instead of having yea here, we can call this one selected.
Year.
And this one here, selected month.
And selected.
Day of.
All right, that will be a little clearer.
To what is happening.
All right, so now we can go ahead and calculate based on those things.
So first of all, I'm going to create a new variable here, which is to select a date.
So I'm going to say, well, selected date.
Is equal to.
They.
Or selected day of month, then slash dollar sign, and here we could say it's the selected month,
but the problem is it's not really because as we saw earlier, selected month is not the correct value.
It starts from zero.
So what we need to do is we need those additional brackets here, those curly brackets, and then we
can do the calculation so we can just say selected month plus one and then slash.
Selected year.
So, of course, the dollar sign here as well.
So the structure will be day, month, year.
Of course, it could be a different structure in different countries, but we will see how we can take
that into consideration.
So now what we can do is we can change the TV selected date.
What is the TV?
Is it the television?
No, it's actually our TextView, so now we can go ahead and set its text to our selected date.
All right, so let's have a look at our activity, men, just to see what this TV means.
So we had this selected date, but on top of that, we had this TV select to date.
So this TextView that was empty so far now would like to access this string or this TextView and adjust
it.
So if you run it again, we will see that it will display the selected date here.
There we are, so let's select a date I'm going to select today, and it says 11, 12, 12, 19.
Now, let's select that and bring it into a right format, how do we do that?
Well, there is something called simple date format.
So there is a clause that allows us to format dates and it's called simple date format.
And what we can do is we can simply say this new object that we create asdf simple date format.
That's what I use.
This shortcut form is going to use this simple date format class and we can use the Java version.
So there are two to Android and the Java version.
I want to implement a Java one.
So here at the top it should say in Java text simple date format and now we need to pass a pattern.
So the pattern should be standing for Day-Day.
I'm standing for month, month and.
Here you you're standing for the year with four digits now, month, month with capital ends with lower
caps and why as well with lower caps.
You can, of course, all use also other patterns if you want, but that's just going to be the pattern
and I'm going to use and here you can also pass a local you can just say, OK, I want to use the English
locale.
So this is going to be an English app.
So I'll use the English locale, which means that it will build the format of the dates in a structure
that is common in the English language.
Now, what I can do is I can use this format, this simple date format in order to format my selected
date so I can go ahead and say, well, date or let's call it the date is SDF thought pass.
Selected data.
So the selected date, as we saw earlier, is the day of the month, the month plus one and the selected
year, and now we pass it into a date object, which means that the date will be of type date.
So of this class date.
All right, so you do this class here, so this will be an object of type data and we pass it, which
means that we convert it from a string that we had before because this is a string into the format of
a date object.
And we use the date format, sample date format here in order to do so because simple date former this
class contains a method called pass.
In case you ever see a class that I'm using that you don't understand and you would like to know more
about it.
And the explanation that I'm giving is not deep enough.
Then you can always check out the documentation.
For me, the biggest challenge in creating such a course is always to find the right balance between
going deep enough to give you enough of an understanding of what is going on and not going too deep
to the point where I start to bore you with all of the information and generating an overflow in your
brain.
So that's really not my goal.
I want to teach you everything, but it has to go with the right pace and it's difficult to find the
right place for everyone.
But I'm really trying here and that's why I recommend to those who really want to know everything,
to check out the documentation in between every time so you can see simple data form.
It is a concrete class for formatting and parsing dates in a local sensitive manner.
You can also see what the patterns are, so M stands for month in a year, this capital M and we have
D is the day of the month, but D as a capital T is the day in a year.
So you can see capitalization really matters when it comes to this simple date formatter.
And of course to date plus could also be interesting.
So if you want to know more about the date object and here we have two different ones, util and sequel,
you can always check out the date class and you can see what the dates are, stars, how it's implemented
and so forth.
Generally speaking, you can see you can create objects of date with a long date, which is a long number,
then with year, month and date, year, month, date, hours and minutes, even seconds and so forth.
So different structures to create a date object.
And that's exactly what we're creating here, so we're creating a date object based on whatever is the
result of simple date format which returns with this method, your returns a date object, and that's
what we store in the date.
All right.
I'd say we make another break here because the video, otherwise we'll get too long.
So I'd say we do the final calculation and it's really not that much left, but we do the final calculation
and next video.
And there you will see how you can go ahead and display the H in minutes.