Let’s be honest here: dealing with date arithmetic is a pain, and working with Java or Kotlin does not exactly make things easier. In real apps, use JodaTime or JSR-310 to deal with dates. To keep things simple, we’ll use the old java.util.Date
class you know from TDT4100 (“the Java course”) and a very dirty hack to calculate the number of hours left. Android Studio will complain, if you find it annoying just add @Suppress("DEPRECATION")
at the top of our class.
To ask for a date, the easiest solution is to display a DatePickerDialog
. So, when the user taps “Change deadline”, display a date picker dialog. When the user taps OK, the date picker dialog tells us what the user selected (which means we need to listen to the date picker), and we update the deadline.
- Create a field called
deadline
, and initialize it usingDate()
. This field will hold the date selected by the user. - When our button is clicked, create and display a date picker dialog. There are many ways to create the date picker, we’ll use the variant that expects the following:```
DatePickerDialog(context: Context, listener: OnDateSetListener, year: Int, month: Int, dayOfMonth: Int)
* An `Activity` is a `Context`. `MainActivity` is an `Activity`. * `this` works the same way you are used to from Java. * `OnDateSetListener` is an interface you need to implement somehow. Use a [lambda function](https://kotlinlang.org/docs/reference/lambdas.html#lambda-expressions-and-anonymous-functions) or make `MainActivity` implement it. * In Kotlin, you list everything you inherit from at the class declaration like this:``` class MyClass : SuperDuperClass(), BestInterfaceEver { ``` * The people who made `Date` used two-digit notation for year, **so you need to add 1900 to the output of `date.year`**. * Don't confuse `Date.day`("day of week") and `Date.date` ("day of month")
- Remember to show the dialog. Run your app to test that it works before moving on. Hint:
pickerDialog.show()
- By now, you should have an empty
onDateSet
method or lambda. Use it to update thedeadline
values.
Important detail: remember to set time of day to 00:00 or 23:59 or whenever your deadlines are. Use a TimePickerDialog
if you want to be fancy, it works just like the date picker dialog.
Now we have the correct deadline, but we need to display it somehow. Let’s apply our dirty hack for calculate the difference:
- To keep things (somewhat) tidy, create a method to update the screen:
fun updateUI() {
and call it at last line ofonDateSet
. - Create a local
Date
object callednow
inside our newupdateUI
method. - Get the time in milliseconds from both dates, and subtract.
- Convert to hours:```
val hoursLeft = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS)
// Use
TimeUnit
fromjava.util.concurrent
, not fromandroid.whatever
. // (If you get this wrong, delete theimport
line at the top of the file and try again.) - Update the hext to display the actual number of hours left.
- Optional: Display the deadline in the title at the top of the screen.
title = "Whatever"
andSimpleDateFormat.getDateTimeInstance().format(date: Date)
are your friends.
Run our app again. Now you can set the deadline, and the number of days left are updated!
All done? Deadline works and everything? Turn your phone sideways, and see what happens…