A Scala wrapper for Joda Time that provides idiomatic Scala syntax and operators for date and time operations. This library transforms Joda Time into a fluent, type-safe API with natural syntax like DateTime.now + 2.months and rich implicit conversions between temporal types.
libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "1.8.0"import com.github.nscala_time.time.Imports._This single import provides access to all functionality including:
import com.github.nscala_time.time.Imports._
// Create and manipulate dates
val now = DateTime.now
val tomorrow = now + 1.day
val nextWeek = now + 1.week
// Fluent date property manipulation
val specific = DateTime.now.withYear(2024).withMonth(12).withDay(25)
// Duration and period creation
val duration = 2.hours + 30.minutes + 15.seconds
val period = 3.months + 2.weeks + 5.days
// Interval operations
val interval = DateTime.now to DateTime.tomorrow
val isWithin = DateTime.now + 12.hours within interval
// String parsing with safety
val parsed = "2024-12-25T10:30:00".toDateTime
val safe = "invalid-date".toDateTimeOption // returns Nonenscala-time extends Joda Time with several key architectural patterns:
Fluent API for creating durations and periods with automatic unit conversions and arithmetic operations.
// Integer enrichments for time units
implicit class RichInt(n: Int) {
def millis: DurationBuilder
def seconds: DurationBuilder
def minutes: DurationBuilder
def hours: DurationBuilder
def days: Period
def weeks: Period
def months: Period
def years: Period
}
class DurationBuilder(underlying: Period) {
def +(that: DurationBuilder): DurationBuilder
def -(that: DurationBuilder): DurationBuilder
def ago: DateTime
def later: DateTime
def from(dt: DateTime): DateTime
def standardDuration: Duration
}Rich operations on DateTime, LocalDate, LocalTime, and LocalDateTime with fluent property access and arithmetic.
implicit class RichDateTime(underlying: DateTime) {
def +(duration: ReadableDuration): DateTime
def -(duration: ReadableDuration): DateTime
def +(period: ReadablePeriod): DateTime
def -(period: ReadablePeriod): DateTime
def +(builder: DurationBuilder): DateTime
def -(builder: DurationBuilder): DateTime
// Property access
def year: DateTime.Property
def month: DateTime.Property
def day: DateTime.Property
def hour: DateTime.Property
def minute: DateTime.Property
def second: DateTime.Property
// Property setters
def withYear(year: Int): DateTime
def withMonth(month: Int): DateTime
def withDay(day: Int): DateTime
def withHour(hour: Int): DateTime
def withMinute(minute: Int): DateTime
def withSecond(second: Int): DateTime
}Seamless conversions between Joda Time types, Java standard library types, and Scala types with both safe and direct conversion methods.
implicit class RichString(s: String) {
def toDateTime: DateTime
def toLocalDate: LocalDate
def toInterval: Interval
def toDateTimeOption: Option[DateTime]
def toLocalDateOption: Option[LocalDate]
def toIntervalOption: Option[Interval]
}
implicit class RichDate(underlying: java.util.Date) {
def toLocalDateTime: LocalDateTime
def toLocalDate: LocalDate
def toLocalTime: LocalTime
}
implicit class RichSDuration(underlying: scala.concurrent.duration.Duration) {
def toJodaDuration: Duration
}
implicit class RichDuration(underlying: Duration) {
def toScalaDuration: scala.concurrent.duration.Duration
}Convenient factory methods and utilities for creating temporal objects with enhanced Scala-friendly APIs.
object DateTime extends StaticDateTime {
def now: DateTime
def now(zone: DateTimeZone): DateTime
def parse(str: String): DateTime
def parse(str: String, formatter: DateTimeFormatter): DateTime
def nextSecond: DateTime
def nextMinute: DateTime
def nextHour: DateTime
def nextDay: DateTime
def tomorrow: DateTime
def nextWeek: DateTime
def nextMonth: DateTime
def nextYear: DateTime
def yesterday: DateTime
def lastSecond: DateTime
def lastMinute: DateTime
def lastHour: DateTime
def lastDay: DateTime
def lastWeek: DateTime
def lastMonth: DateTime
def lastYear: DateTime
}Core type aliases that provide Scala-friendly names for Joda Time classes:
type DateTime = org.joda.time.DateTime
type LocalDate = org.joda.time.LocalDate
type LocalTime = org.joda.time.LocalTime
type LocalDateTime = org.joda.time.LocalDateTime
type Duration = org.joda.time.Duration
type Period = org.joda.time.Period
type Interval = org.joda.time.Interval
type DateTimeZone = org.joda.time.DateTimeZone
type DateTimeFormat = org.joda.time.format.DateTimeFormat
type Chronology = org.joda.time.Chronology
type YearMonth = org.joda.time.YearMonth
type MonthDay = org.joda.time.MonthDay
type Partial = org.joda.time.PartialBuilt-in ordering instances for all temporal types:
implicit val DateTimeOrdering: Ordering[DateTime]
implicit val LocalDateOrdering: Ordering[LocalDate]
implicit val LocalTimeOrdering: Ordering[LocalTime]
implicit val LocalDateTimeOrdering: Ordering[LocalDateTime]
implicit val DurationOrdering: Ordering[Duration]