[Android] Easily calculate the difference between two dates

Overview

For example, between ** 2008/9/23 ** and ** today **, ** how many days? How many months? how many years? ** **

If you try to do something like this using Java / Kotlin Date or Calender, the date will be converted to UnixTime, subtracted, and then parsed again.

LocalDate is useful in such a case, but it is difficult with minSdk26 or higher. This is the problem to be solved this time. スクリーンショット 2019-04-30 0.13.03.png

(By the way, 2008/9/23 is the day when Android 1.0 came out)

solution

That Jake Wharton god has created this backport library for Android.

ThreeTen Android Backport https://github.com/JakeWharton/ThreeTenABP

By using this, you can use LocalDate without worrying about minSdk.

How to use

First, write the following in Gradle.

app/build.gradle


dependencies {
    ...
    implementation 'com.jakewharton.threetenabp:threetenabp:1.2.0'
}

As a result java.time.LocalDate In addition to the LocalDate that existed in org.threeten.bp.LocalDate Is now available. Use this.

For example, let's check the difference days, months, and years between 2008/9/23 and today (written: 2019/4/30).

val androidFirstRelease = LocalDate.of(2008, Month.SEPTEMBER, 23)
val today = LocalDate.now()  // 2019/4/30
val period = Period.between(androidFirstRelease, today) 

> period.days -> 7
> period.months -> 7
> period.years -> 10

// java.time.Org instead of monthly.threeten.bp.Note using Month

It turned out that 10 years, 7 months and 7 days have passed since the first release of Android.

The usage of LocalDate is basically the same as the default LocalDate on Java8, so you should look there.

Write if you remember --java.time.LocalDate

Now you can go back to Unix Time ... you don't have to! Thank you for your hard work.

Recommended Posts

[Android] Easily calculate the difference between two dates
Calculate the latitude / longitude distance between two points
Calculate the difference between numbers in a Ruby array
Android Spot the Difference Game
[Android, Java] Convenient method to calculate the difference in days
Find the difference between List types
Understand the difference between each_with_index and each.with_index
Find the angle between two vectors
[Android] Difference between finish (); and return;
Output the difference between each field of two objects in Java
[Android, Java] Method to find the elapsed date from two dates
About the difference between irb and pry
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
[Java] I want to calculate the difference from the date
What is the difference between System Spec and Feature Spec?
Regarding the difference between the three Timeouts in Java's HttpClient
About the difference between classes and instances in Ruby
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
[Rails] I investigated the difference between redirect_to and render.
What is the difference between Java EE and Jakarta EE?
[Swift] UITextField taught me the difference between nil and ""
I will explain the difference between Android application development and iOS application development from the perspective of iOS engineers