0
# Local Date and Time Types
1
2
Enhanced functionality for local date and time types including arithmetic operations and property access. Local types represent dates and times without timezone information, making them ideal for calendar operations and user interface display.
3
4
## Capabilities
5
6
### Enhanced LocalDate Operations
7
8
Rich wrapper providing arithmetic operations and property access for LocalDate.
9
10
```scala { .api }
11
/**
12
* Enhanced LocalDate with operator overloading and property access
13
* @param ld The underlying Joda Time LocalDate
14
*/
15
implicit class RichLocalDate(ld: LocalDate) extends AnyVal {
16
// Arithmetic operations
17
def +(period: ReadablePeriod): LocalDate
18
def -(period: ReadablePeriod): LocalDate
19
20
// Property accessors
21
def day: LocalDate.Property
22
def week: LocalDate.Property
23
def month: LocalDate.Property
24
def year: LocalDate.Property
25
26
// Fluent setters
27
def withDay(day: Int): LocalDate
28
def withMonth(month: Int): LocalDate
29
def withYear(year: Int): LocalDate
30
}
31
```
32
33
### Enhanced LocalDate Property Operations
34
35
Enhanced property operations for LocalDate components.
36
37
```scala { .api }
38
/**
39
* Enhanced LocalDate property operations
40
* @param pty The underlying LocalDate.Property
41
*/
42
implicit class RichLocalDateProperty(pty: LocalDate.Property) extends AnyVal {
43
// Enhanced property operations for LocalDate fields
44
// Provides fluent API for property manipulation
45
}
46
```
47
48
### Enhanced LocalDateTime Operations
49
50
Rich wrapper providing arithmetic operations and property access for LocalDateTime.
51
52
```scala { .api }
53
/**
54
* Enhanced LocalDateTime with operator overloading and property access
55
* @param dt The underlying Joda Time LocalDateTime
56
*/
57
implicit class RichLocalDateTime(dt: LocalDateTime) extends AnyVal {
58
// Arithmetic operations
59
def +(period: ReadablePeriod): LocalDateTime
60
def -(period: ReadablePeriod): LocalDateTime
61
def +(duration: ReadableDuration): LocalDateTime
62
def -(duration: ReadableDuration): LocalDateTime
63
64
// Property accessors
65
def millis: LocalDateTime.Property
66
def second: LocalDateTime.Property
67
def minute: LocalDateTime.Property
68
def hour: LocalDateTime.Property
69
def day: LocalDateTime.Property
70
def month: LocalDateTime.Property
71
def year: LocalDateTime.Property
72
73
// Fluent setters
74
def withSecond(second: Int): LocalDateTime
75
def withMinute(minute: Int): LocalDateTime
76
def withHour(hour: Int): LocalDateTime
77
def withDay(day: Int): LocalDateTime
78
def withMonth(month: Int): LocalDateTime
79
def withYear(year: Int): LocalDateTime
80
}
81
```
82
83
### Enhanced LocalDateTime Property Operations
84
85
Enhanced property operations for LocalDateTime components.
86
87
```scala { .api }
88
/**
89
* Enhanced LocalDateTime property operations
90
* @param pty The underlying LocalDateTime.Property
91
*/
92
implicit class RichLocalDateTimeProperty(pty: LocalDateTime.Property) extends AnyVal {
93
// Enhanced property operations for LocalDateTime fields
94
// Provides fluent API for property manipulation
95
}
96
```
97
98
### Enhanced LocalTime Operations
99
100
Rich wrapper providing arithmetic operations and property access for LocalTime.
101
102
```scala { .api }
103
/**
104
* Enhanced LocalTime with operator overloading and property access
105
* @param lt The underlying Joda Time LocalTime
106
*/
107
implicit class RichLocalTime(lt: LocalTime) extends AnyVal {
108
// Arithmetic operations
109
def +(period: ReadablePeriod): LocalTime
110
def -(period: ReadablePeriod): LocalTime
111
def +(duration: ReadableDuration): LocalTime
112
def -(duration: ReadableDuration): LocalTime
113
114
// Property accessors
115
def millis: LocalTime.Property
116
def second: LocalTime.Property
117
def minute: LocalTime.Property
118
def hour: LocalTime.Property
119
120
// Fluent setters
121
def withSecond(second: Int): LocalTime
122
def withMinute(minute: Int): LocalTime
123
def withHour(hour: Int): LocalTime
124
def withMillis(millis: Int): LocalTime
125
}
126
```
127
128
### Enhanced LocalTime Property Operations
129
130
Enhanced property operations for LocalTime components.
131
132
```scala { .api }
133
/**
134
* Enhanced LocalTime property operations
135
* @param pty The underlying LocalTime.Property
136
*/
137
implicit class RichLocalTimeProperty(pty: LocalTime.Property) extends AnyVal {
138
// Enhanced property operations for LocalTime fields
139
// Provides fluent API for property manipulation
140
}
141
```
142
143
**Usage Examples:**
144
145
```scala
146
import com.github.nscala_time.time.Imports._
147
148
// LocalDate operations
149
val today = LocalDate.now()
150
val nextWeek = today + 1.week
151
val lastMonth = today - 1.month
152
val birthday = today.withMonth(12).withDay(25)
153
154
// LocalDate property access
155
val currentYear = today.year.get()
156
val endOfMonth = today.day.withMaximumValue()
157
val startOfYear = today.dayOfYear.withMinimumValue()
158
159
// LocalDateTime operations
160
val now = LocalDateTime.now()
161
val meetingTime = now
162
.withHour(14)
163
.withMinute(30)
164
.withSecond(0)
165
166
val later = now + 2.hours + 30.minutes
167
val yesterday = now - 1.day
168
169
// LocalTime operations
170
val currentTime = LocalTime.now()
171
val lunchTime = currentTime.withHour(12).withMinute(0)
172
val afternoon = currentTime + 4.hours
173
val morning = currentTime - 6.hours
174
175
// Working with milliseconds
176
val preciseTime = currentTime.withMillis(500)
177
val currentMillis = currentTime.millis.get()
178
```
179
180
### Static Factory Methods
181
182
Enhanced static factory methods for creating local date/time objects.
183
184
```scala { .api }
185
object StaticLocalDate {
186
def now(): LocalDate
187
def now(zone: DateTimeZone): LocalDate
188
def now(chronology: Chronology): LocalDate
189
def parse(str: String): LocalDate
190
}
191
192
object StaticLocalDateTime {
193
def now(): LocalDateTime
194
def now(zone: DateTimeZone): LocalDateTime
195
def now(chronology: Chronology): LocalDateTime
196
def parse(str: String): LocalDateTime
197
}
198
199
object StaticLocalTime {
200
def now(): LocalTime
201
def now(zone: DateTimeZone): LocalTime
202
def now(chronology: Chronology): LocalTime
203
def parse(str: String): LocalTime
204
}
205
```
206
207
**Static Factory Usage Examples:**
208
209
```scala
210
import com.github.nscala_time.time.Imports._
211
212
// Current local date/time
213
val today = LocalDate.now()
214
val now = LocalDateTime.now()
215
val currentTime = LocalTime.now()
216
217
// Parsing from strings
218
val specificDate = LocalDate.parse("2023-12-25")
219
val specificDateTime = LocalDateTime.parse("2023-12-25T14:30:00")
220
val specificTime = LocalTime.parse("14:30:00")
221
222
// With specific time zones (gets local component)
223
val localInUTC = LocalDate.now(DateTimeZone.UTC)
224
val localInTokyo = LocalDateTime.now(DateTimeZone.forID("Asia/Tokyo"))
225
```
226
227
### Conversion Between Local Types
228
229
Converting between different local date/time representations.
230
231
```scala
232
import com.github.nscala_time.time.Imports._
233
234
val date = LocalDate.now()
235
val time = LocalTime.now()
236
val dateTime = LocalDateTime.now()
237
238
// Combining LocalDate and LocalTime to LocalDateTime
239
val combined = date.toLocalDateTime(time)
240
241
// Extracting components
242
val dateFromDateTime = dateTime.toLocalDate
243
val timeFromDateTime = dateTime.toLocalTime
244
245
// Converting to DateTime (requires timezone)
246
val withTimezone = dateTime.toDateTime(DateTimeZone.getDefault)
247
val atUTC = dateTime.toDateTime(DateTimeZone.UTC)
248
```
249
250
### Calendar Operations
251
252
Common calendar operations using local date/time types.
253
254
```scala
255
import com.github.nscala_time.time.Imports._
256
257
val today = LocalDate.now()
258
259
// Month boundaries
260
val startOfMonth = today.day.withMinimumValue()
261
val endOfMonth = today.day.withMaximumValue()
262
263
// Year boundaries
264
val startOfYear = today.dayOfYear.withMinimumValue()
265
val endOfYear = today.dayOfYear.withMaximumValue()
266
267
// Week operations
268
val startOfWeek = today.dayOfWeek.withMinimumValue() // Monday
269
val endOfWeek = today.dayOfWeek.withMaximumValue() // Sunday
270
271
// Business day calculations
272
val nextMonday = today.dayOfWeek.setCopy(DateTimeConstants.MONDAY)
273
val nextFriday = today.dayOfWeek.setCopy(DateTimeConstants.FRIDAY)
274
275
// Time operations
276
val timeNow = LocalTime.now()
277
val startOfDay = timeNow.withMillisOfDay(0) // 00:00:00.000
278
val noon = timeNow.withHour(12).withMinute(0).withSecond(0)
279
val endOfDay = timeNow.withTime(23, 59, 59, 999) // 23:59:59.999
280
```
281
282
### Comparison and Ordering
283
284
Local date/time types support natural ordering and comparison operations.
285
286
```scala
287
import com.github.nscala_time.time.Imports._
288
289
val date1 = LocalDate.now()
290
val date2 = date1 + 1.day
291
292
// Comparisons
293
val isBefore = date1 < date2 // true
294
val isAfter = date1 > date2 // false
295
val isEqual = date1 == date1 // true
296
297
// Sorting collections
298
val dates = List(
299
LocalDate.parse("2023-12-25"),
300
LocalDate.parse("2023-01-01"),
301
LocalDate.parse("2023-06-15")
302
)
303
304
val sortedDates = dates.sorted // Chronological order
305
val reverseSorted = dates.sorted(Ordering[LocalDate].reverse)
306
307
// Working with ranges (if available through additional operations)
308
val startDate = LocalDate.parse("2023-01-01")
309
val endDate = LocalDate.parse("2023-01-31")
310
311
// Generate sequence of dates (custom implementation)
312
def dateRange(start: LocalDate, end: LocalDate): List[LocalDate] = {
313
def loop(current: LocalDate, acc: List[LocalDate]): List[LocalDate] = {
314
if (current > end) acc.reverse
315
else loop(current + 1.day, current :: acc)
316
}
317
loop(start, Nil)
318
}
319
320
val januaryDates = dateRange(startDate, endDate)
321
```