Skip to content

Commit eb8d65a

Browse files
Merge pull request #1513 from hkateu/nstime
SCALA-378 code for nscala-time article
2 parents 5c967ce + ce87f8f commit eb8d65a

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

build.sbt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,14 @@ lazy val refined = (project in file("scala-libraries-standalone/refined"))
501501
libraryDependencies ++= scalaTestDeps
502502
)
503503

504+
lazy val nscalatime = (project in file("scala-libraries-standalone/nscalatime"))
505+
.settings(
506+
name := "nscalatime",
507+
scalaVersion := scala3Version,
508+
libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "2.32.0",
509+
libraryDependencies ++= scalaTestDeps
510+
)
511+
504512
val spireVersion = "0.18.0"
505513
val kafkaVersion = "7.7.0-ce"
506514
val pureconfigVersion = "0.17.7"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.nscalatime
2+
3+
import com.github.nscala_time.time.Imports.*
4+
import java.util.Date
5+
import java.util.Calendar
6+
import java.util.Locale
7+
import org.joda.time.chrono.EthiopicChronology
8+
import org.joda.time.DateTime.Property
9+
import com.github.nscala_time.time.DurationBuilder
10+
import org.joda.time.format.DateTimeFormatter
11+
12+
object NScalaTime:
13+
// working with DateTime
14+
val st: DateTime = DateTime.now()
15+
val parsedDate: DateTime = DateTime.parse("2024-08-07")
16+
val parsedDate2: DateTime = DateTime.parse("2024")
17+
18+
// JDK interoperability
19+
val jdkDate: Date = new Date()
20+
val convertTo: DateTime = new DateTime(jdkDate)
21+
22+
val jdkCal: Calendar = Calendar.getInstance()
23+
val st2: DateTime = new DateTime(jdkCal)
24+
25+
val st3: DateTime = new DateTime
26+
val cal: Calendar = st3.gregorianCalendar
27+
28+
// querrying Datetime
29+
val dayOfWeek: Property = new DateTime().dayOfWeek()
30+
val shortText: String = dayOfWeek.asShortText
31+
val asText: String = dayOfWeek.asText
32+
val monthOfYearString: String = DateTime.now().monthOfYear().getAsText()
33+
val monthOfYearStringFrench: String =
34+
DateTime.now().monthOfYear().getAsShortText(Locale.FRENCH)
35+
val isLeapYear: Boolean = DateTime.now().year().isLeap()
36+
val nowToTomorrow: Interval = DateTime.now().to(DateTime.tomorrow())
37+
38+
// Accessing fields
39+
// Date fields
40+
val st4: DateTime = new DateTime()
41+
val era: String = st4.era.asText
42+
val year2: String = st4.year.asText
43+
val centOfEra: String = st4.centuryOfEra.asText
44+
val yrOfEra: String = st4.yearOfEra.asText
45+
val yrOfCent: String = st4.yearOfCentury.asText
46+
val monthOfYr: String = st4.monthOfYear.asText
47+
val dayOfYr: String = st4.dayOfYear.asText
48+
val dayOfMonth: String = st4.dayOfMonth.asText
49+
val dayOfWeek2: String = st4.dayOfWeek.asText
50+
// Time fields
51+
val hrOfDay: String = st4.hourOfDay.asText
52+
val minOfHr: String = st4.minuteOfHour.asText
53+
val secOfMin: String = st4.secondOfMinute.asText
54+
55+
// Manipulating datetimes
56+
val compare: Boolean = DateTime.now() < DateTime.lastMonth()
57+
val addVarious: Period = 2.month + 5.hours + 6.millis
58+
val less5: DateTime = DateTime.now() - 5.days
59+
val sampleDuration: DurationBuilder = 4.hours + 30.minutes + 10.seconds
60+
val datetimebeforeDuration: DateTime = sampleDuration.before(DateTime.now())
61+
val changeTZ: DateTime = less5.withZone(DateTimeZone.forID("Africa/Kampala"))
62+
val chrono: DateTime = less5.withChronology(EthiopicChronology.getInstance())
63+
val ethioYear: Int = chrono.getYear()
64+
65+
// formatters
66+
val fmt: DateTimeFormatter = DateTimeFormat.forPattern("dd-MM-yyyy")
67+
val st5: DateTime = fmt.parseDateTime("31-07-2024")
68+
69+
import NScalaTime.*
70+
@main def sTime(): Unit =
71+
println(st5)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.baeldung.nscalatime
2+
3+
import com.github.nscala_time.time.Imports.*
4+
import org.scalatest.flatspec.AnyFlatSpec
5+
import org.scalatest.matchers.should.Matchers
6+
import org.scalatest.prop.Tables.*
7+
import org.scalatest.prop.TableDrivenPropertyChecks
8+
import org.scalatest.prop.TableFor1
9+
import java.util.Date
10+
import java.util.Calendar
11+
import com.github.nscala_time.time.DurationBuilder
12+
import org.joda.time.format.DateTimeFormatter
13+
import NScalaTime.*
14+
15+
class NScalaTimeUnitTest
16+
extends AnyFlatSpec
17+
with Matchers
18+
with TableDrivenPropertyChecks:
19+
20+
"DateTime checks" should "pass as DateTime objects" in:
21+
val dateTimeObjects: TableFor1[DateTime] =
22+
Table(
23+
"datetimeobjects",
24+
parsedDate,
25+
parsedDate2,
26+
convertTo,
27+
st2,
28+
st3,
29+
st4,
30+
less5,
31+
changeTZ,
32+
chrono,
33+
st5
34+
)
35+
36+
forAll(dateTimeObjects) { n =>
37+
n shouldBe an[DateTime]
38+
}
39+
40+
"jdkDate" should "pass as a Date objects" in:
41+
jdkDate shouldBe an[Date]
42+
43+
"Calendar checks" should "pass as Calendar objects" in:
44+
val calendarObjects: TableFor1[Calendar] =
45+
Table(
46+
"calendarobjects",
47+
jdkCal,
48+
cal
49+
)
50+
51+
forAll(calendarObjects) { n =>
52+
n shouldBe an[Calendar]
53+
}
54+
55+
"strings checks" should "pass as String objects" in:
56+
val stringObjects: TableFor1[String] =
57+
Table(
58+
"stringsobjects",
59+
shortText,
60+
asText,
61+
monthOfYearString,
62+
monthOfYearStringFrench,
63+
era,
64+
year2,
65+
centOfEra,
66+
yrOfEra,
67+
yrOfCent,
68+
monthOfYr,
69+
dayOfYr,
70+
dayOfMonth,
71+
dayOfWeek2,
72+
hrOfDay,
73+
minOfHr,
74+
secOfMin
75+
)
76+
77+
forAll(stringObjects) { n =>
78+
n shouldBe an[String]
79+
}
80+
81+
"boolean checks" should "pass as Boolean objects" in:
82+
val booleanObjects: TableFor1[Boolean] =
83+
Table(
84+
"booleanobjects",
85+
isLeapYear,
86+
compare
87+
)
88+
89+
forAll(booleanObjects) { n =>
90+
n shouldBe an[Boolean]
91+
}
92+
93+
"nowToTomorrow" should "pass as an Interval object" in:
94+
nowToTomorrow shouldBe an[Interval]
95+
96+
"addVarious" should "pass as a Period object" in:
97+
addVarious shouldBe an[Period]
98+
99+
"sampleDuration" should "pass as a DurationBuilder object" in:
100+
sampleDuration shouldBe an[DurationBuilder]
101+
102+
"ethioYear" should "pass as an Int object" in:
103+
ethioYear shouldBe an[Int]
104+
105+
"fmt" should "pass as a DateTimeFormatter object" in:
106+
fmt shouldBe an[DateTimeFormatter]

0 commit comments

Comments
 (0)