Last active
February 16, 2020 02:44
-
-
Save dan085/3922c47b1d39d7f8297ce22d3c95478c to your computer and use it in GitHub Desktop.
comparador de fecha con kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.* | |
| import java.util.concurrent.TimeUnit | |
| fun main() { | |
| println("Hello, world!!!") | |
| compareDateTime() | |
| } | |
| fun compareDateTime() { | |
| val today = Calendar.getInstance() | |
| val yesterday = Calendar.getInstance() | |
| yesterday.add(Calendar.HOUR, -23) | |
| println("differenceOfDaysByDateAndTime - With Time component") | |
| println("%s - %s = %s days" | |
| .format( | |
| today.time, | |
| yesterday.time, | |
| diffOfDaysByDateAndTime(today, yesterday) | |
| )) | |
| println() | |
| println("differenceOfDaysByDate - Ignore Time component") | |
| println("${today.time} - ${yesterday.time} = " + | |
| "${diffOfDaysByDate(today, yesterday)} days") | |
| } | |
| private fun diffOfDaysByDateAndTime( a: Calendar, b: Calendar ): Int { | |
| val duration = a.timeInMillis - b.timeInMillis | |
| return TimeUnit.MILLISECONDS.toDays(duration).toInt() | |
| } | |
| private fun diffOfDaysByDate( a: Calendar, b: Calendar ): Int { | |
| clearTimeComponent(a) | |
| clearTimeComponent(b) | |
| return diffOfDaysByDateAndTime(a, b) | |
| } | |
| private fun clearTimeComponent( date: Calendar ) { | |
| date.set(Calendar.HOUR_OF_DAY, 0) | |
| date.set(Calendar.MINUTE, 0) | |
| date.set(Calendar.SECOND, 0) | |
| date.set(Calendar.MILLISECOND, 0) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment