You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
907 B
Kotlin

package moe.odango.index.utils
import java.sql.Date
import java.text.SimpleDateFormat
import java.util.*
data class IntDate(val year: Int, val month: Int, val day: Int) {
fun toDate(): Date = Date(year - 1900, month, day)
companion object {
fun parse(format: String, input: String, locale: Locale = Locale.getDefault(Locale.Category.FORMAT)): IntDate? {
val formatter = SimpleDateFormat(format, locale)
return try {
formatter.parse(input)?.let {
val nr = GregorianCalendar().apply {
time = it
}
IntDate(nr.get(Calendar.YEAR), nr.get(Calendar.MONTH), nr.get(Calendar.DAY_OF_MONTH))
}
} catch (t: Throwable) {
null
}
}
}
}
fun Date.toIntDate() = IntDate(this.year + 1900, this.month, this.date)