๋ฐ์ํ
Intro
์๋ ํ์ธ์. ์ฝํ๋ฆฐ ์คํธ๋ง์ ์ฝํ๋ฆฐ ๋ ์ง๋ก ๋ณํํ๋ ๋์ค ์๋์ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค.
import java.math.BigDecimal
import java.math.RoundingMode
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
fun main(args: Array<String>) {
val input = "2301"
val formatter = DateTimeFormatter.ofPattern("yyMM", Locale.KOREA)
val date = LocalDate.parse(input, formatter).minusYears(1)
val output = date.format(formatter)
print(output)
}
Text '..' could not be parsed: Unable to obtain LocalDate from TemporalAccessor
How to solve the problem
๋ ์ง๊ฐ์ 2302, yyMM ํ์์ ์ด์ฉํด์ ํ์ฉํ๋ ค๊ณ ํ์ง๋ง, ๊ณ์ ์๋ฌ๊ฐ ๋ฌ์ต๋๋ค. yyyyMM ํ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์๋ฌ๊ฐ ๋๋๊ตฐ์. ๊ทธ๋์ ์๋์ ๊ฐ์ด ๋ณ๊ฒฝํด์ ์คํํ๋ ์ ์ ์๋ํ์ต๋๋ค.
fun main(args: Array<String>) {
val input = "20" + "2301" + "01"
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd", Locale.KOREA)
val date = LocalDate.parse(input, formatter).minusYears(1)
val output = date.format(formatter)
print(output) // 20220101
}
ofPatteren์ ๋ ์์ผ์ด (yyyyMMdd) ํํ๋ก ๋ค์ด๊ฐ์ผ ๋๋ ๊ฒ ๊ฐ์ต๋๋ค :)
๋ฐ์ํ