diff --git a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/CreateDate.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/CreateDate.kt deleted file mode 100644 index 78705fc151..0000000000 --- a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/CreateDate.kt +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.kotlin.dates - -import java.time.LocalDate -import java.time.format.DateTimeFormatter - -fun createDateUsingParseMethodDefaultFormat() { - - var date = LocalDate.parse("2018-12-31") - println(date) -} - -fun createDateUsingParseMethodCustomFormat() { - - var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy") - - var date = LocalDate.parse("31-12-2018", formatter) - - println(date) -} - -fun createDateUsingOfMethod() { - var date = LocalDate.of(2018, 12, 31) - println(date) - -} - -fun main(args: Array) { - - createDateUsingParseMethodCustomFormat() - createDateUsingOfMethod() -} \ No newline at end of file diff --git a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/ExtractDate.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/ExtractDate.kt deleted file mode 100644 index f5291b63db..0000000000 --- a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/ExtractDate.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.kotlin.dates - -import java.time.LocalDate - -fun extractingCommonComponents() { - var date = LocalDate.parse("2018-12-31") - - println(date.year) - println(date.month) - println(date.dayOfMonth) -} - -fun extractingEraDowDoy() { - var date = LocalDate.parse("2018-12-31") - - println(date.era) - println(date.dayOfWeek) - println(date.dayOfYear) -} - -fun main(args: Array) { - extractingCommonComponents() - extractingEraDowDoy() -} \ No newline at end of file diff --git a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/PeriodDate.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/PeriodDate.kt deleted file mode 100644 index f8689c55be..0000000000 --- a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/PeriodDate.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.kotlin.dates - -import java.time.LocalDate -import java.time.Period - -fun createAPeriod() { - var period = Period.of(1, 2, 3) - - println(period) -} - -fun addAPeriod() { - var period = Period.of(1, 2, 3) - - var date = LocalDate.of(2018, 6, 25) - var modifiedDate = date.plus(period) - - println(modifiedDate) -} - -fun subtractAPeriod() { - var period = Period.of(1, 2, 3) - - var date = LocalDate.of(2018, 6, 25) - var modifiedDate = date.minus(period) - - println(modifiedDate) -} - -fun getAPeriod() { - - var date1 = LocalDate.parse("2018-06-25") - var date2 = LocalDate.parse("2018-12-25") - - var period = Period.between(date1, date2) - println(period) -} - -fun main(args: Array) { - createAPeriod() - addAPeriod() - subtractAPeriod() - getAPeriod() -} \ No newline at end of file diff --git a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/PrintDate.kt b/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/PrintDate.kt deleted file mode 100644 index 73380e3152..0000000000 --- a/kotlin-libraries/src/main/kotlin/com/baeldung/kotlin/dates/PrintDate.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.kotlin.dates - -import java.time.LocalDate -import java.time.format.DateTimeFormatter - -fun printDateDefaultFormat() { - - var date = LocalDate.parse("2018-12-31") - println(date) -} - -fun printDateUsingCustomFormat() { - - var date = LocalDate.parse("2018-12-31") - - var formatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy") - var formattedDate = date.format(formatter) - println(formattedDate) -} - -fun main(args: Array) { - - printDateDefaultFormat() - - printDateUsingCustomFormat() -} \ No newline at end of file diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/CreateDateUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/CreateDateUnitTest.kt new file mode 100644 index 0000000000..d52a2f0f19 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/CreateDateUnitTest.kt @@ -0,0 +1,34 @@ +package com.baeldung.kotlin.dates + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.time.LocalDate +import java.time.format.DateTimeFormatter + +class CreateDateUnitTest { + + @Test + fun givenString_whenDefaultFormat_thenCreated() { + + var date = LocalDate.parse("2018-12-31") + + assertThat(date).isEqualTo("2018-12-31") + } + + @Test + fun givenString_whenCustomFormat_thenCreated() { + + var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy") + var date = LocalDate.parse("31-12-2018", formatter) + + assertThat(date).isEqualTo("2018-12-31") + } + + @Test + fun givenYMD_whenUsingOf_thenCreated() { + var date = LocalDate.of(2018, 12, 31) + + assertThat(date).isEqualTo("2018-12-31") + } + +} \ No newline at end of file diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/ExtractDateUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/ExtractDateUnitTest.kt new file mode 100644 index 0000000000..ef3841752b --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/ExtractDateUnitTest.kt @@ -0,0 +1,29 @@ +package com.baeldung.kotlin.dates + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.time.DayOfWeek +import java.time.LocalDate +import java.time.Month + +class ExtractDateUnitTest { + + @Test + fun givenDate_thenExtractedYMD() { + var date = LocalDate.parse("2018-12-31") + + assertThat(date.year).isEqualTo(2018) + assertThat(date.month).isEqualTo(Month.DECEMBER) + assertThat(date.dayOfMonth).isEqualTo(31) + } + + @Test + fun givenDate_thenExtractedEraDowDoy() { + var date = LocalDate.parse("2018-12-31") + + assertThat(date.era.toString()).isEqualTo("CE") + assertThat(date.dayOfWeek).isEqualTo(DayOfWeek.MONDAY) + assertThat(date.dayOfYear).isEqualTo(365) + } + +} \ No newline at end of file diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/FormatDateUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/FormatDateUnitTest.kt new file mode 100644 index 0000000000..11ff6ec9f0 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/FormatDateUnitTest.kt @@ -0,0 +1,29 @@ +package com.baeldung.kotlin.dates + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.time.LocalDate +import java.time.format.DateTimeFormatter + +class FormatDateUnitTest { + + @Test + fun givenDate_whenDefaultFormat_thenFormattedString() { + + var date = LocalDate.parse("2018-12-31") + + assertThat(date.toString()).isEqualTo("2018-12-31") + } + + @Test + fun givenDate_whenCustomFormat_thenFormattedString() { + + var date = LocalDate.parse("2018-12-31") + + var formatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy") + var formattedDate = date.format(formatter) + + assertThat(formattedDate).isEqualTo("31-December-2018") + } + +} \ No newline at end of file diff --git a/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/PeriodDateUnitTest.kt b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/PeriodDateUnitTest.kt new file mode 100644 index 0000000000..e6b66634d3 --- /dev/null +++ b/kotlin-libraries/src/test/kotlin/com/baeldung/kotlin/dates/PeriodDateUnitTest.kt @@ -0,0 +1,48 @@ +package com.baeldung.kotlin.dates + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.time.LocalDate +import java.time.Period + +class PeriodDateUnitTest { + + @Test + fun givenYMD_thenCreatePeriod() { + var period = Period.of(1, 2, 3) + + assertThat(period.toString()).isEqualTo("P1Y2M3D") + } + + @Test + fun givenPeriod_whenAdd_thenModifiedDate() { + var period = Period.of(1, 2, 3) + + var date = LocalDate.of(2018, 6, 25) + var modifiedDate = date.plus(period) + + assertThat(modifiedDate).isEqualTo("2019-08-28") + } + + @Test + fun givenPeriod_whenSubtracted_thenModifiedDate() { + var period = Period.of(1, 2, 3) + + var date = LocalDate.of(2018, 6, 25) + var modifiedDate = date.minus(period) + + assertThat(modifiedDate).isEqualTo("2017-04-22") + } + + @Test + fun givenTwoDate_whenUsingBetween_thenDiffOfDates() { + + var date1 = LocalDate.parse("2018-06-25") + var date2 = LocalDate.parse("2018-12-25") + + var period = Period.between(date1, date2) + + assertThat(period.toString()).isEqualTo("P6M") + } + +} \ No newline at end of file