Merge pull request #5572 from dkapil/feature/BAEL-896

BAEL-896 Working with dates in Kotlin
This commit is contained in:
Loredana Crusoveanu 2018-11-09 19:31:30 +02:00 committed by GitHub
commit 1f8acbde98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 0 deletions

View File

@ -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")
}
}

View File

@ -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)
}
}

View File

@ -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")
}
}

View File

@ -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")
}
}