Welcome back, so we can select the date now, and that works.
But now let's actually calculate how much time has passed ever since.
So let's go back to coding.
So we have the date now, and this is just using this simple date format that we have prepared.
And it's parsing that into the data variable.
But I wants to know the selected state in minutes.
So today that we're getting there is not giving us.
So the selected date in minutes, so how much time has passed ever since and minutes?
So what I want to use for that is going to be a variable that will be called selected date in minutes.
And in order to get that, I can use to date variable here and get its time property and then divide
that by 60000.
So why would I divide it by 60000?
Because date so the date property for look at it here gives us the details and to time that has passed
ever since a given moment, so here does get time.
Method, for example, gives us the number of milliseconds to January the 1st 1970 at midnight.
Okay, so when you use get time, you get a super long value.
This is a long data type.
It's a very long value.
And if you want to get minutes instead of milliseconds, you, first of all, have to divide by thousand
to get two seconds and then another time by 60 in order to get from seconds to minutes.
So if you do both in one step, you divide by 60000 and then you get the time that has passed since
1970 in minutes.
And this time property is the same thing as if we were asking for get time.
So it's the same as if I would use get time here, for example.
Like So but this is this great time is the old fashioned way we can just get this time by using the
property.
Access syntax does time, as you can see here.
So now we have the selected date, and we need to calculate how much time has passed between the selected
date and the current date.
So let's get to current date.
Current date is going to use to SDF pass.
And here we need to use the format, so we could, of course, create the format object here and so
forth, but I'm just going to do it in one step as the format where I use system that current millis.
So what this will do is it will give me the time that has passed since the 1st of January 1970 in milliseconds.
That's how I can basically use this approach here.
Here we get how much time has passed since the ball between the selected date and 1970 1st of January.
And here we get the current time.
So how much time has passed between now and 1970?
Okay, so that's the current date that we get.
And now we need to calculate the difference in minutes.
OK, so let's get the current date.
And we need to calculate that in minutes, actually.
So let's create a vault current date in minutes.
Which will be the current date time divided by 60000.
And now we can use the current date in minutes minus the selected date in minutes.
And this will give us how much time is between those two time points.
So between the selected and the current date?
OK, so now we can use this difference in minutes and actually assign it to our text view that wants
to know about it, and it's this text view here which currently doesn't have an ID, so let's give it
an ID.
You probably know how to do it.
You just use this Android ID at plus ID slash, or you could have used a designer to find ID here or
here directly.
OK, well, I'm going to show you how to do it in the code.
It's called a TV age in minutes.
OK, so now let's access this text view in our code, and therefore we need to create another variable
up here.
So private VAR and I'm going to call the TV age in minutes, which will also be a text you optional.
I'm going to set it to null and then I can initialize it here in my own create.
Method, and I will find it by its side, which is our Dart ID, dot tbe h in minutes.
OK, so here we are signing the text use and this age in minutes is this text view here, by the way,
you can get there by holding the control key, clicking into it and you see which text you.
It is in the split view.
You can click into here and you will see that it's this one here.
What currently says 14 million?
So that's the one that I want to overwrite.
OK.
So in the main activity, now we have access to this TV age in minutes and we can actually assign it.
So here TV age in minutes.
That text should now be the difference in minutes.
Now, unfortunately, the difference in minutes is of type along, so we need to convert it into a string
for this to work.
And TV age in minutes here, because it's another bill, we need to add the question mark here for type
safety.
OK, so at this point, let's actually test us.
So let's test once again.
And that selected date, let's select between.
Yesterday and today, it's not yesterday, but it's a little longer, so you see here, 14, 40 minutes
have passed.
Ever since that day that I've selected and no.
So let me select the first of September ever since 20 minutes have passed, but me select the date in
the future because today's the 21st.
So let me select the 22nd.
And you see, it's some sometime in the past, even though for some reason my phone thinks it's a different
date, I believe it thinks that it's September the 15th, so definitely something wrong with my phone
here and still stuck at the 15th and thinks the current date is 15th, even though it's the 21st.
But if I were to test this on a real phone, this would work.
So how can I limit the selection to dates in the past because they cannot be born in the future, right?
Well, that is something that we can do with a neat little trick.
So instead of showing this date picker dialogue here directly, what we can do is we can put this entire
date picker dialogue, which is all of this code.
This is all of the code that this part of our date picker dialogue, so we can put that into a variable
so I can call the DPD, for example, and paste all of that in there.
So this DPD value more variable will now be our data picker dialog.
So let me shrink it down.
And now, while in here with this minus, I can collapse the code.
You see, all of this code now is going to be reduced, at least from a visible point of view.
And now we can show the date picker dialog.
Let's just test if the date picker dialog will still appear as we wanted it so we can see it's the works.
Let me select the date here.
OK, that still works.
And now, instead of just showing it, I want to add another property to it.
And here I can set the date picker property.
To be set to a specific max date, so here does this mix date, and I can assign he makes date to it.
And here, in order to set the max date to be yesterday, I can just go ahead and set it to a system
that current time in milliseconds minus eighty six four zero zero, so 86 million, 400000 milliseconds.
So there are 3.6 million milliseconds in one hour.
So multiply that by 24 and you get eighty six point four million milliseconds.
So let's test that once again, and I'll select the date in the future.
While you can see I cannot select dates in the future anymore, I can only select dates in the past.
So, for example, one day in the past and you can see now it's 14 40, that's how many minutes I'm
old.
If I'm one day old.
Exactly.
OK, so now we can make a couple of changes here to our code.
Because we are using to date here.
And now, if you look at our code, you will see that there are some parts of the code marked, OK,
so this dead date, for example, is highlighted or has a background color here, and that is because
we're not using type safety.
So what you can do here instead, is you can make it null safe by using the following approach by basically
creating this selected date in minutes.
If the date is not empty, so here's the date question mark.
Don't let.
And now do all of this part here inside of it.
OK, so only if the date is not empty, then run this code.
The same goes for the current date.
So here we can use current date.
Question mark.
Don't let.
And then execute the rest only if current date is not empty.
So this date let is just an approach to make sure that you we only execute this code if the date is
in fact not empty.
If the date is empty for some reason, then we will not execute the code.
So this way we're making sure that our code is not crashing.
And this is just null safety that we are implementing here.
And then another thing that we can optimize is that we can make sure that this click data picker is
private as well, so we can make it private so that it will only be accessible from this class and from
the other class, which may totally make sense for our particular case.
So if we ever want to use this method from somewhere else, we would just have to make it public again
and then we can access it from the other classes.
But in our case, as this is never going to be required for now, we can replace that.
And another thing you see here, this view is never used, so we can basically just replace it with
an underscore and the code will work exactly as it did before.
OK, so that's the highly optimized code that we have here and now we can feel confident when we published
this application.
So in the next lecture, I have a little challenge for you where I would like you to rebuild this application,
not displaying the age in minutes, but actually the age and hours.
So good luck with that and see you in the next.