g1501_1600.s1507_reformat_date.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1501_1600.s1507_reformat_date
// #Easy #String #2023_06_12_Time_147_ms_(100.00%)_Space_34_MB_(100.00%)
class Solution {
fun reformatDate(date: String): String {
val sb = StringBuilder()
val map: MutableMap = HashMap()
map["Jan"] = "01"
map["Feb"] = "02"
map["Mar"] = "03"
map["Apr"] = "04"
map["May"] = "05"
map["Jun"] = "06"
map["Jul"] = "07"
map["Aug"] = "08"
map["Sep"] = "09"
map["Oct"] = "10"
map["Nov"] = "11"
map["Dec"] = "12"
sb.append(date.substring(date.length - 4))
sb.append('-')
sb.append(map[date.substring(date.length - 8, date.length - 5)])
sb.append('-')
if (Character.isDigit(date[1])) {
sb.append(date, 0, 2)
} else {
sb.append('0')
sb.append(date[0])
}
return sb.toString()
}
}