Skip to content

MonthDay

주진현 edited this page Aug 30, 2024 · 2 revisions

java.time.MonthDay 클래스는 월(Month)과 일(Day)을 나타내는 클래스입니다.

1 Instance 생성

1.1 String.toMonthDay(): MonthDay

주어진 StringMonthDay로 파싱합니다. 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

1.2 String.toMonthDayOrNull(): MonthDay?

주어진 StringMonthDay로 파싱하고 예외가 발생하면 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

1.3 String.toMonthDay(String): MonthDay

주어진 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

1.4 String.toMonthDayOrNull(String): MonthDay?

주어진 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

1.5 String.toMonthDay(DateTimeFormatter): MonthDay

주어진 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

1.6 String.toMonthDayOrNull(DateTimeFormatter): MonthDay?

주어진 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

2 기간

2.1 MonthDay.months: Period

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

2.2 MonthDay.days: Period

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

3 연산

3.1 MonthDay.plus(TemporalAmount): MonthDay

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

3.2 MonthDay.minus(TemporalAmount): MonthDay

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

4 결합

4.1 MonthDay.at(Year): LocalDate

MonthDayYear를 결합하여 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

5 비교

5.1 MonthDay.between(MonthDay): Period

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

6 포맷

6.1 MonthDay.toString(String): String

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

6.2 MonthDay.toString(DateTimeFormatter): String

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
Clone this wiki locally