Comprehensive enhancements to DateTime with operator overloading, property accessors, and fluent setters. These extensions make DateTime manipulation intuitive and type-safe while maintaining full compatibility with Joda Time.
Rich wrapper providing operator overloading and fluent API for DateTime manipulation.
/**
* Enhanced DateTime with operator overloading and fluent API
* @param underlying The underlying Joda Time DateTime
*/
implicit class RichDateTime(val underlying: DateTime) extends AnyVal {
// Arithmetic operators - subtraction
def -(duration: Long): DateTime
def -(duration: ReadableDuration): DateTime
def -(period: ReadablePeriod): DateTime
def -(builder: DurationBuilder): DateTime
// Arithmetic operators - addition
def +(duration: Long): DateTime
def +(duration: ReadableDuration): DateTime
def +(period: ReadablePeriod): DateTime
def +(builder: DurationBuilder): DateTime
// Property accessors for time components
def millis: DateTime.Property
def second: DateTime.Property
def minute: DateTime.Property
def hour: DateTime.Property
def day: DateTime.Property
def week: DateTime.Property
def month: DateTime.Property
def year: DateTime.Property
def century: DateTime.Property
def era: DateTime.Property
// Fluent setter methods
def withSecond(second: Int): DateTime
def withMinute(minute: Int): DateTime
def withHour(hour: Int): DateTime
def withDay(day: Int): DateTime
def withWeek(week: Int): DateTime
def withMonth(month: Int): DateTime
def withYear(year: Int): DateTime
def withCentury(century: Int): DateTime
def withEra(era: Int): DateTime
}Usage Examples:
import com.github.nscala_time.time.Imports._
val now = DateTime.now()
// Arithmetic operations
val future = now + 2.hours + 30.minutes
val past = now - 1.day
val withMillis = now + 5000L // Add 5000 milliseconds
val withDuration = now + Duration.standardHours(3)
// Property access
val currentYear = now.year.get()
val currentMonth = now.month.get()
val currentDay = now.day.get()
val currentHour = now.hour.get()
// Fluent setters - method chaining
val specificTime = now
.withYear(2024)
.withMonth(12)
.withDay(25)
.withHour(14)
.withMinute(30)
.withSecond(0)
// Property-based operations
val endOfMonth = now.day.withMaximumValue()
val startOfYear = now.month.withMinimumValue().day.withMinimumValue()Enhanced property classes for fine-grained DateTime manipulation.
/**
* Enhanced DateTime property operations
*/
implicit class RichDateTimeProperty(pty: DateTime.Property) extends AnyVal {
// Property-specific operations available through implicit enhancement
// Inherits all Joda Time Property methods plus Scala enhancements
}Base functionality for all DateTime-like objects.
/**
* Enhanced functionality for AbstractDateTime types
* @param dt The underlying AbstractDateTime
*/
implicit class RichAbstractDateTime(dt: AbstractDateTime) extends AnyVal {
// Base DateTime functionality that applies to all DateTime-like objects
// Provides common operations for DateTime hierarchy
}Base functionality for all instant-like objects.
/**
* Enhanced functionality for AbstractInstant types
* @param in The underlying AbstractInstant
*/
implicit class RichAbstractInstant(in: AbstractInstant) extends AnyVal {
// Base instant functionality for all instant-like objects
// Provides common time-point operations
}Operations for all readable DateTime objects.
/**
* Enhanced functionality for ReadableDateTime types
* @param dt The underlying ReadableDateTime
*/
implicit class RichReadableDateTime(dt: ReadableDateTime) extends AnyVal {
// Enhanced operations for all readable DateTime objects
// Provides consistent interface across DateTime types
}Operations for all readable instant objects.
/**
* Enhanced functionality for ReadableInstant types
* @param in The underlying ReadableInstant
*/
implicit class RichReadableInstant(in: ReadableInstant) extends AnyVal {
// Enhanced operations for all readable instant objects
// Enables operator overloading and comparisons
}Operations for Instant objects.
/**
* Enhanced functionality for Instant types
* @param in The underlying Instant
*/
implicit class RichInstant(in: Instant) extends AnyVal {
// Enhanced Instant operations
// Provides arithmetic and comparison operations
}Complex Usage Examples:
import com.github.nscala_time.time.Imports._
// Complex date arithmetic
val startDate = DateTime.parse("2023-01-01T00:00:00")
val endDate = startDate + 1.year + 2.months + 15.days + 8.hours
// Working with different time zones
val utcTime = DateTime.now(DateTimeZone.UTC)
val localTime = utcTime.withZone(DateTimeZone.getDefault)
// Property manipulations
val businessHours = DateTime.now()
.withHour(9) // 9 AM
.withMinute(0)
.withSecond(0)
.withMillisOfSecond(0)
val endOfBusinessDay = businessHours.withHour(17) // 5 PM
// Calendar operations
val firstDayOfMonth = DateTime.now().day.withMinimumValue()
val lastDayOfMonth = DateTime.now().day.withMaximumValue()
val firstDayOfYear = DateTime.now().dayOfYear.withMinimumValue()
// Complex time calculations
val meetingTime = DateTime.now()
.plusDays(7) // Next week
.withDayOfWeek(DateTimeConstants.MONDAY) // Monday
.withHour(10) // 10 AM
.withMinute(30) // 10:30 AM
// Combining with intervals
val workingHours = businessHours to endOfBusinessDay
val lunchBreak = businessHours.withHour(12) to businessHours.withHour(13)DateTime objects enhanced with comparison operators and interval creation.
import com.github.nscala_time.time.Imports._
val now = DateTime.now()
val later = now + 1.hour
// Comparisons (enabled by implicit Ordering)
val isBefore = now < later // true
val isAfter = now > later // false
val isEqual = now == now // true
// Interval creation using 'to' method
val interval = now to later
val intervalDuration = interval.toDurationMillis
val intervalPeriod = interval.toPeriod
// Checking if time falls within interval
val someTime = now + 30.minutes
val isWithin = interval.contains(someTime) // true