This post shows sample Kotlin codes that use the Java 8 Date and Time API (java.time).
Requirements
These are Software applications used for this post.
IntelliJ
IDEA
Ultimate 2016.3- The Community Edition may be enough but we have not tried it.
Kotlin
Version 1.1- Windows 10 Enterprise
Using java.time.LocalDate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.turreta.kotlin.java.time import java.time.LocalDate /** * Created by Turreta.com on 17/6/2017. */ fun main(args: Array<String>) { var localDateNow: LocalDate = LocalDate.now() val localDate: LocalDate = LocalDate.of(2015, 6, 6) println("Current Date: " + localDateNow) println("Some Date:" + localDate) } |
Outputs:
1 2 | Current Date: 2017-06-17 Some Date:2015-06-06 |
Using java.time.LocalDateTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.turreta.kotlin.java.time import java.time.LocalDateTime /** * Created by Turreta.com on 17/6/2017. */ fun main(args: Array<String>) { // Current date/time var localDateTimeNow: LocalDateTime = LocalDateTime.now() // June 6, 2015 8:30:10 AM val localDateTime: LocalDateTime = LocalDateTime.of(2015, 6, 6, 8, 30, 10) println("Current Date/time: " + localDateTimeNow) println("Some Date/time: " + localDateTime) } |
Outputs:
1 2 | Current Date/time: 2017-06-17T12:59:20.547 Some Date/time: 2015-06-06T08:30:10 |
Using java.time.LocalTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.turreta.kotlin.java.time import java.time.LocalTime /** * Created by Turreta.com on 17/6/2017. */ fun main(args: Array<String>) { // Current time var localTimeNow: LocalTime = LocalTime.now() // 8:30:10 AM val localTime: LocalTime = LocalTime.of(8, 30, 10) println("Current time: " + localTimeNow) println("Some time: " + localTime) } |
Outputs:
1 2 | Current time: 13:02:30.968 Some time: 08:30:10 |
Using java.time.Duration
Note that java.time.Duration
works with classes that store both date and time. Depending on how we use Duration
, it works with classes too that only store time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.turreta.kotlin.java.time import java.time.Duration import java.time.LocalDateTime /** * java.time.Duration example in Kotlin * * @author Turreta.com */ fun main(args: Array<String>) { // specificDateTime = February 2, 2017 9:00 AM var specificDateTime: LocalDateTime = LocalDateTime.of(2017, 2, 1, 9, 0, 0 ) println(specificDateTime) // specificDateTime + 5 days println(specificDateTime.plus(Duration.ofDays(5))) } |
Outputs:
1 2 | 2017-02-01T09:00 2017-02-06T09:00 |
Using java.time.Period
Note that java.time.Period
works with both classes that store date/time and date only. However. it will not work with classes that store time only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.turreta.kotlin.java.time import java.time.LocalDate import java.time.LocalDateTime import java.time.Period /** * java.time.Period example in Kotlin * * @author Turreta.com */ fun main(args: Array<String>) { // specificDateTime = February 2, 2017 9:00 AM var specificDateTime: LocalDateTime = LocalDateTime.of(2017, 2, 1, 9, 0, 0 ) println(specificDateTime) // specificDateTime - 5 days println(specificDateTime.minus(Period.ofDays(5))) // specifiDateOnly = July 4, 2020 var specificDateOnly = LocalDate.of(2020, 7, 4) println(specificDateOnly) // specificDateOnly + 1 year println(specificDateOnly.plus(Period.ofDays(1))) } |
Outputs:
1 2 3 4 | 2017-02-01T09:00 2017-01-27T09:00 2020-07-04 2020-07-05 |
Using java.time.temporal.ChronoUnit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.turreta.kotlin.java.time import java.time.LocalDateTime import java.time.temporal.ChronoUnit /** * Created by Turreta.com on 17/6/2017. */ fun main(args: Array<String>) { // January 5, 2010 9:30:45 AM var startLocalDateTime: LocalDateTime = LocalDateTime.of(2010, 1, 5, 9, 30, 45) // June 6, 2015 8:30:10 AM val endLocalDateTime: LocalDateTime = LocalDateTime.of(2015, 6, 6, 8, 30, 10) println("Years: " + ChronoUnit.YEARS.between(startLocalDateTime, endLocalDateTime)) println("Months: " + ChronoUnit.MONTHS.between(startLocalDateTime, endLocalDateTime)) println("Weeks: " + ChronoUnit.WEEKS.between(startLocalDateTime, endLocalDateTime)) println("Days: " + ChronoUnit.DAYS.between(startLocalDateTime, endLocalDateTime)) println("Hours: " + ChronoUnit.HOURS.between(startLocalDateTime, endLocalDateTime)) println("Minutes: " + ChronoUnit.MINUTES.between(startLocalDateTime, endLocalDateTime)) println("Seconds: " + ChronoUnit.SECONDS.between(startLocalDateTime, endLocalDateTime)) println("ms: " + ChronoUnit.MILLIS.between(startLocalDateTime, endLocalDateTime)) println("ns: " + ChronoUnit.NANOS.between(startLocalDateTime, endLocalDateTime)) } |
Outputs:
1 2 3 4 5 6 7 8 9 | Years: 5 Months: 65 Weeks: 282 Days: 1977 Hours: 47470 Minutes: 2848259 Seconds: 170895565 ms: 170895565000 ns: 170895565000000000 |
Download the codes
https://github.com/Turreta/Kotlin-Using-Java-8-Date-and-Time-API
References
- http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html
- https://kotlinlang.org/docs/reference/
I simply wanted to thank you so much again. I am not sure the things
that I might have gone through without the type of hints revealed by
you regarding that situation.