Merge pull request #2898 from sbmaggarwal/master

BAEL-896 : Working with dates in Kotlin
This commit is contained in:
Carsten Gräf 2017-10-29 16:04:53 +01:00 committed by GitHub
commit f1831535cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 292 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.datetime
import java.time.Duration
import java.time.LocalTime
class UseDuration {
fun modifyDates(localTime: LocalTime, duration: Duration): LocalTime {
return localTime.plus(duration)
}
fun getDifferenceBetweenDates(localTime1: LocalTime, localTime2: LocalTime): Duration {
return Duration.between(localTime1, localTime2)
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.datetime
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import java.time.temporal.TemporalAdjusters
class UseLocalDate {
fun getLocalDateUsingFactoryOfMethod(year: Int, month: Int, dayOfMonth: Int): LocalDate {
return LocalDate.of(year, month, dayOfMonth)
}
fun getLocalDateUsingParseMethod(representation: String): LocalDate {
return LocalDate.parse(representation)
}
fun getLocalDateFromClock(): LocalDate {
return LocalDate.now()
}
fun getNextDay(localDate: LocalDate): LocalDate {
return localDate.plusDays(1)
}
fun getPreviousDay(localDate: LocalDate): LocalDate {
return localDate.minus(1, ChronoUnit.DAYS)
}
fun getDayOfWeek(localDate: LocalDate): DayOfWeek {
return localDate.dayOfWeek
}
fun getFirstDayOfMonth(): LocalDate {
return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth())
}
fun getStartOfDay(localDate: LocalDate): LocalDateTime {
return localDate.atStartOfDay()
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.datetime
import java.time.LocalDateTime
class UseLocalDateTime {
fun getLocalDateTimeUsingParseMethod(representation: String): LocalDateTime {
return LocalDateTime.parse(representation)
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.datetime
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.temporal.ChronoUnit
class UseLocalTime {
fun getLocalTimeUsingFactoryOfMethod(hour: Int, min: Int, seconds: Int): LocalTime {
return LocalTime.of(hour, min, seconds)
}
fun getLocalTimeUsingParseMethod(timeRepresentation: String): LocalTime {
return LocalTime.parse(timeRepresentation)
}
fun getLocalTimeFromClock(): LocalTime {
return LocalTime.now()
}
fun addAnHour(localTime: LocalTime): LocalTime {
return localTime.plus(1, ChronoUnit.HOURS)
}
fun getHourFromLocalTime(localTime: LocalTime): Int {
return localTime.hour
}
fun getLocalTimeWithMinuteSetToValue(localTime: LocalTime, minute: Int): LocalTime {
return localTime.withMinute(minute)
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.datetime
import java.time.LocalDate
import java.time.Period
class UsePeriod {
fun modifyDates(localDate: LocalDate, period: Period): LocalDate {
return localDate.plus(period)
}
fun getDifferenceBetweenDates(localDate1: LocalDate, localDate2: LocalDate): Period {
return Period.between(localDate1, localDate2)
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.datetime
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZonedDateTime
class UseZonedDateTime {
fun getZonedDateTime(localDateTime: LocalDateTime, zoneId: ZoneId): ZonedDateTime {
return ZonedDateTime.of(localDateTime, zoneId)
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.kotlin.datetime
import com.baeldung.datetime.UseLocalDateTime
import java.time.LocalDate
import java.time.LocalTime
import java.time.Month
import org.junit.Test
import org.junit.Assert.assertEquals
class UseLocalDateTimeUnitTest {
var useLocalDateTime = UseLocalDateTime()
@Test
fun givenString_whenUsingParse_thenLocalDateTime() {
assertEquals(LocalDate.of(2016, Month.MAY, 10), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30")
.toLocalDate())
assertEquals(LocalTime.of(6, 30), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30")
.toLocalTime())
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.kotlin.datetime
import com.baeldung.datetime.UseLocalDate
import org.junit.Assert
import org.junit.Test
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalDateTime
class UseLocalDateUnitTest {
var useLocalDate = UseLocalDate()
@Test
fun givenValues_whenUsingFactoryOf_thenLocalDate() {
Assert.assertEquals("2016-05-10", useLocalDate.getLocalDateUsingFactoryOfMethod(2016, 5, 10)
.toString())
}
@Test
fun givenString_whenUsingParse_thenLocalDate() {
Assert.assertEquals("2016-05-10", useLocalDate.getLocalDateUsingParseMethod("2016-05-10")
.toString())
}
@Test
fun whenUsingClock_thenLocalDate() {
Assert.assertEquals(LocalDate.now(), useLocalDate.getLocalDateFromClock())
}
@Test
fun givenDate_whenUsingPlus_thenNextDay() {
Assert.assertEquals(LocalDate.now()
.plusDays(1), useLocalDate.getNextDay(LocalDate.now()))
}
@Test
fun givenDate_whenUsingMinus_thenPreviousDay() {
Assert.assertEquals(LocalDate.now()
.minusDays(1), useLocalDate.getPreviousDay(LocalDate.now()))
}
@Test
fun givenToday_whenUsingGetDayOfWeek_thenDayOfWeek() {
Assert.assertEquals(DayOfWeek.SUNDAY, useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")))
}
@Test
fun givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth() {
Assert.assertEquals(1, useLocalDate.getFirstDayOfMonth()
.dayOfMonth.toLong())
}
@Test
fun givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight() {
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")))
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.kotlin.datetime
import com.baeldung.datetime.UseLocalTime
import java.time.LocalTime
import org.junit.Assert
import org.junit.Test
class UseLocalTimeUnitTest {
internal var useLocalTime = UseLocalTime()
@Test
fun givenValues_whenUsingFactoryOf_thenLocalTime() {
Assert.assertEquals("07:07:07", useLocalTime.getLocalTimeUsingFactoryOfMethod(7, 7, 7).toString())
}
@Test
fun givenString_whenUsingParse_thenLocalTime() {
Assert.assertEquals("06:30", useLocalTime.getLocalTimeUsingParseMethod("06:30").toString())
}
@Test
fun givenTime_whenAddHour_thenLocalTime() {
Assert.assertEquals("07:30", useLocalTime.addAnHour(LocalTime.of(6, 30)).toString())
}
@Test
fun getHourFromLocalTime() {
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1, 1)).toLong())
}
@Test
fun getLocalTimeWithMinuteSetToValue() {
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10, 10), 20))
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.kotlin.datetime
import com.baeldung.datetime.UsePeriod
import java.time.LocalDate
import java.time.Period
import org.junit.Assert
import org.junit.Test
class UsePeriodUnitTest {
var usingPeriod = UsePeriod()
@Test
fun givenPeriodAndLocalDate_thenCalculateModifiedDate() {
val period = Period.ofDays(1)
val localDate = LocalDate.parse("2007-05-10")
Assert.assertEquals(localDate.plusDays(1), usingPeriod.modifyDates(localDate, period))
}
@Test
fun givenDates_thenGetPeriod() {
val localDate1 = LocalDate.parse("2007-05-10")
val localDate2 = LocalDate.parse("2007-05-15")
Assert.assertEquals(Period.ofDays(5), usingPeriod.getDifferenceBetweenDates(localDate1, localDate2))
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.kotlin.datetime
import com.baeldung.datetime.UseZonedDateTime
import org.junit.Assert
import org.junit.Test
import java.time.LocalDateTime
import java.time.ZoneId
class UseZonedDateTimeUnitTest {
internal var zonedDateTime = UseZonedDateTime()
@Test
fun givenZoneId_thenZonedDateTime() {
val zoneId = ZoneId.of("Europe/Paris")
val zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId)
Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime))
}
}