-
Notifications
You must be signed in to change notification settings - Fork 0
MonthDay
java.time.MonthDay
클래스는 월(Month)과 일(Day)을 나타내는 클래스입니다.
주어진 String
을 MonthDay
로 파싱합니다.
String
은 라이브러리 기본 구성의 패턴을 따라야 합니다.
import io.github.harryjhin.java.time.extension.toMonthDay
import java.time.MonthDay
val monthDay: MonthDay = "01-01".toMonthDay()
println(monthDay) // --01-01
String
값이 지정한 형식이 아니라면 DateTimeParseException
이 발생합니다.
val monthDay: MonthDay = "01/01".toMonthDay() // DateTimeParseException
주어진 String
을 MonthDay
로 파싱하고 예외가 발생하면 null
을 반환합니다.
String
은 라이브러리 기본 구성의 패턴을 따라야 합니다.
import io.github.harryjhin.java.time.extension.toMonthDayOrNull
import java.time.MonthDay
val monthDay: MonthDay? = "01-01".toMonthDayOrNull() // --01-01
String
값이 지정한 형식이 아니라면 null
을 반환합니다.
val monthDay: MonthDay? = "01/01".toMonthDayOrNull() // null
주어진 String
을 지정한 패턴을 사용하여 MonthDay
로 파싱합니다.
import io.github.harryjhin.java.time.extension.toMonthDay
import java.time.MonthDay
val monthDay: MonthDay = "01/01".toMonthDay("MM/dd") // --01-01
패턴이 올바른 형식이 아니라면 IllegalArgumentException
이 발생합니다.
val monthDay: MonthDay = "01/01".toMonthDay("ABC") // throw IllegalArgumentException
String
값이 지정한 형식이 아니라면 DateTimeParseException
이 발생합니다.
val monthDay: MonthDay = "01-01".toMonthDay("MM/dd") // throw DateTimeParseException
주어진 String
을 지정한 패턴을 사용하여 MonthDay
로 파싱하고 예외가 발생하면 null
을 반환합니다.
import io.github.harryjhin.java.time.extension.toMonthDayOrNull
import java.time.MonthDay
val monthDay: MonthDay? = "01/01".toMonthDayOrNull("MM/dd") // --01-01
패턴이 올바른 형식이 아니라면 null
을 반환합니다.
val monthDay: MonthDay? = "01/01".toMonthDayOrNull("ABC") // null
String
값이 지정한 형식이 아니라면 null
을 반환합니다.
val monthDay: MonthDay? = "01-01".toMonthDayOrNull("MM/dd") // null
주어진 String
을 지정한 DateTimeFormatter
를 사용하여 MonthDay
로 파싱합니다.
import io.github.harryjhin.java.time.extension.toDateTimeFormatter
import io.github.harryjhin.java.time.extension.toMonthDay
import java.time.MonthDay
import java.time.format.DateTimeFormatter
val formatter: DateTimeFormatter = "MM/dd".toDateTimeFormatter()
val monthDay: MonthDay = "01/01".toMonthDay(formatter) // --01-01
String
값이 지정한 형식이 아니라면 DateTimeParseException
이 발생합니다.
val monthDay: MonthDay = "01-01".toMonthDay(formatter) // throw DateTimeParseException
주어진 String
을 지정한 DateTimeFormatter
를 사용하여 MonthDay
로 파싱하고 예외가 발생하면 null
을 반환합니다.
import io.github.harryjhin.java.time.extension.toDateTimeFormatter
import io.github.harryjhin.java.time.extension.toMonthDayOrNull
import java.time.MonthDay
import java.time.format.DateTimeFormatter
val formatter: DateTimeFormatter = "MM/dd".toDateTimeFormatter()
val monthDay: MonthDay? = "01/01".toMonthDayOrNull(formatter) // --01-01
MonthDay
인스턴스에서 월(month
) 정보를 Period
로 가져옵니다.
import io.github.harryjhin.java.time.extension.toMonthDay
import io.github.harryjhin.java.time.extension.months
import java.time.MonthDay
import java.time.Period
val monthDay: MonthDay = "01-01".toMonthDay()
val period: Period = monthDay.months // P1M
MonthDay
인스턴스에서 일(day
) 정보를 Period
로 가져옵니다.
import io.github.harryjhin.java.time.extension.toMonthDay
import io.github.harryjhin.java.time.extension.days
import java.time.MonthDay
import java.time.Period
val monthDay: MonthDay = "01-01".toMonthDay()
val period: Period = monthDay.days // P1D
MonthDay
인스턴스에 Period
를 더합니다.
import io.github.harryjhin.java.time.extension.toMonthDay
import io.github.harryjhin.java.time.extension.days
import java.time.MonthDay
var monthDay: MonthDay = "01-02".toMonthDay()
monthDay += 1.days // --01-03
MonthDay
인스턴스에서 Period
를 뺍니다.
import io.github.harryjhin.java.time.extension.toMonthDay
import io.github.harryjhin.java.time.extension.days
import java.time.MonthDay
var monthDay: MonthDay = "01-02".toMonthDay()
monthDay -= 1.days // --01-01
MonthDay
와 Year
를 결합하여 LocalDate
인스턴스를 생성합니다.
import io.github.harryjhin.java.time.extension.at
import io.github.harryjhin.java.time.extension.toMonthDay
import io.github.harryjhin.java.time.extension.toYear
import java.time.MonthDay
import java.time.Year
import java.time.LocalDate
val monthDay: MonthDay = "01-01".toMonthDay()
val year: Year = 2024.toYear()
val date: LocalDate = monthDay at year // 2024-01-01
두 MonthDay
인스턴스 사이의 날짜 간격을 계산하여 Period
로 반환합니다.
import io.github.harryjhin.java.time.extension.between
import io.github.harryjhin.java.time.extension.toMonthDay
import java.time.MonthDay
import java.time.Period
val start: MonthDay = "01-01".toMonthDay()
val end: MonthDay = "02-01".toMonthDay()
val period: Period = start between end // P1M
MonthDay
인스턴스를 지정한 패턴을 사용하여 String
으로 포매팅합니다.
import io.github.harryjhin.java.time.extension.toString
import io.github.harryjhin.java.time.extension.toMonthDay
import java.time.MonthDay
val monthDay: MonthDay = "01-01".toMonthDay()
val string: String = monthDay.toString("MM/dd") // 01/01
패턴이 올바른 형식이 아니라면 IllegalArgumentException
이 발생합니다.
val string: String = monthDay.toString("ABC") // throw IllegalArgumentException
MonthDay
인스턴스를 DateTimeFormatter
를 사용하여 String
으로 포매팅합니다.
import io.github.harryjhin.java.time.extension.toDateTimeFormatter
import io.github.harryjhin.java.time.extension.toString
import io.github.harryjhin.java.time.extension.toMonthDay
import java.time.MonthDay
import java.time.format.DateTimeFormatter
val formatter: DateTimeFormatter = "MM/dd".toDateTimeFormatter()
val monthDay: MonthDay = "01-01".toMonthDay()
val string: String = monthDay.toString(formatter) // 01/01