Skip to content

Commit 30ce976

Browse files
committed
BAEL-896 Working with dates in Kotlin
- Added code for working with dates tutorial
1 parent bbc4cdd commit 30ce976

4 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.kotlin.dates
2+
3+
import java.time.LocalDate
4+
import java.time.format.DateTimeFormatter
5+
6+
fun createDateUsingParseMethodDefaultFormat() {
7+
8+
var date = LocalDate.parse("2018-12-31")
9+
println(date)
10+
}
11+
12+
fun createDateUsingParseMethodCustomFormat() {
13+
14+
var formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
15+
16+
var date = LocalDate.parse("31-12-2018", formatter)
17+
18+
println(date)
19+
}
20+
21+
fun createDateUsingOfMethod() {
22+
var date = LocalDate.of(2018, 12, 31)
23+
println(date)
24+
25+
}
26+
27+
fun main(args: Array<String>) {
28+
29+
createDateUsingParseMethodCustomFormat()
30+
createDateUsingOfMethod()
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.kotlin.dates
2+
3+
import java.time.LocalDate
4+
5+
fun extractingCommonComponents() {
6+
var date = LocalDate.parse("2018-12-31")
7+
8+
println(date.year)
9+
println(date.month)
10+
println(date.dayOfMonth)
11+
}
12+
13+
fun extractingEraDowDoy() {
14+
var date = LocalDate.parse("2018-12-31")
15+
16+
println(date.era)
17+
println(date.dayOfWeek)
18+
println(date.dayOfYear)
19+
}
20+
21+
fun main(args: Array<String>) {
22+
extractingCommonComponents()
23+
extractingEraDowDoy()
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.kotlin.dates
2+
3+
import java.time.LocalDate
4+
import java.time.Period
5+
6+
fun createAPeriod() {
7+
var period = Period.of(1, 2, 3)
8+
9+
println(period)
10+
}
11+
12+
fun addAPeriod() {
13+
var period = Period.of(1, 2, 3)
14+
15+
var date = LocalDate.of(2018, 6, 25)
16+
var modifiedDate = date.plus(period)
17+
18+
println(modifiedDate)
19+
}
20+
21+
fun subtractAPeriod() {
22+
var period = Period.of(1, 2, 3)
23+
24+
var date = LocalDate.of(2018, 6, 25)
25+
var modifiedDate = date.minus(period)
26+
27+
println(modifiedDate)
28+
}
29+
30+
fun getAPeriod() {
31+
32+
var date1 = LocalDate.parse("2018-06-25")
33+
var date2 = LocalDate.parse("2018-12-25")
34+
35+
var period = Period.between(date1, date2)
36+
println(period)
37+
}
38+
39+
fun main(args: Array<String>) {
40+
createAPeriod()
41+
addAPeriod()
42+
subtractAPeriod()
43+
getAPeriod()
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.kotlin.dates
2+
3+
import java.time.LocalDate
4+
import java.time.format.DateTimeFormatter
5+
6+
fun printDateDefaultFormat() {
7+
8+
var date = LocalDate.parse("2018-12-31")
9+
println(date)
10+
}
11+
12+
fun printDateUsingCustomFormat() {
13+
14+
var date = LocalDate.parse("2018-12-31")
15+
16+
var formatter = DateTimeFormatter.ofPattern("dd-MMMM-yyyy")
17+
var formattedDate = date.format(formatter)
18+
println(formattedDate)
19+
}
20+
21+
fun main(args: Array<String>) {
22+
23+
printDateDefaultFormat()
24+
25+
printDateUsingCustomFormat()
26+
}

0 commit comments

Comments
 (0)