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.
(By the way, 2008/9/23 is the day when Android 1.0 came out)
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.
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.
Now you can go back to Unix Time ... you don't have to! Thank you for your hard work.
Recommended Posts