Created
October 3, 2018 20:13
-
-
Save ljcamargo/4066a2eb6523bd754a0ec7aa2b971a73 to your computer and use it in GitHub Desktop.
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
| package mx.ncite.savinglista.common | |
| import org.joda.time.* | |
| import org.joda.time.format.DateTimeFormat | |
| import org.joda.time.format.ISODateTimeFormat | |
| object DateUtils { | |
| open val DAYS = "Dias" | |
| open val WEEKS = "Semanas" | |
| open val MONTHS = "Meses" | |
| open val YEARS = "Años" | |
| @JvmStatic | |
| open fun parse(string: String?): DateTime? { | |
| val string = string ?: return null | |
| val parser = ISODateTimeFormat.dateTimeParser() | |
| return parser.parseDateTime(string) | |
| } | |
| @JvmStatic | |
| open fun humanDate(string: String?): String? { | |
| return parse(string)?.toString(DateTimeFormat.forPattern("dd 'de' MMMM 'de' YYYY")) | |
| } | |
| open fun timeDistance(dateTime: DateTime, grain: String?): Int? { | |
| return when (grain) { | |
| DAYS -> Days.daysBetween(dateTime, DateTime()).days | |
| WEEKS -> Weeks.weeksBetween(dateTime, DateTime()).weeks | |
| MONTHS -> { | |
| val days = Days.daysBetween(dateTime, DateTime()).days | |
| return Math.round(days.toFloat() / 30f) | |
| } | |
| YEARS -> Years.yearsBetween(dateTime, DateTime()).years | |
| else -> null | |
| } | |
| } | |
| open fun timeDistanceFrom(date0: DateTime, date1: DateTime, grain: String?): Int? { | |
| return when (grain) { | |
| DAYS -> Days.daysBetween(date0, date1).days | |
| WEEKS -> Weeks.weeksBetween(date0, date1).weeks | |
| MONTHS -> Months.monthsBetween(date0, date1).months | |
| YEARS -> Years.yearsBetween(date0, date1).years | |
| else -> null | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment