Skip to content

Date Wrapper

DateWrapper

Provides utility methods for manipulating a Date. On the following methods, wrap is a DateWrapper.

Default: Date format = yyyy-MM-dd'T'HH:mm:ss.SSSZ

Tip

Convert the date to a string, use the toString method or define the variable as a String

Reading

get

Retrieves the wrapped Date

Syntax
Date wrap.get()

Example

final d = new Date()
final value = p6.wrap.of d get()
assert value == d

toString

Retrieves the String representation of the wrapped value using the optional timezone and date format.

Syntax
String wrap.toString()

Example

final s = p6.wrap.of new Date() toString()
final String s = p6.wrap.of new Date()

timestamp

Returns the timestamp of the date.

Syntax
NumberWrapper wrap.timestamp()

Example

final d = new Date(1674537600000)
final value = p6.wrap.of d timestamp() get()
assert value == 1674537600000

Fields

getYear

Get the year of the date.

Syntax
NumberWrapper wrap.getYear()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d getYear() get()
assert value == 2025

getMonth

Get the month of the date. (zero indexed value)

Note

getMonth() is 0-based (January = 0) for backward compatibility. Prefer getMonthOfYear() (1-based).

Syntax
NumberWrapper wrap.getMonth()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d getMonth() get()
assert value == 0

getMonthOfYear

New Feature

Since 6.10.24

Get the month of the date, 1-based (January = 1).

Syntax
NumberWrapper wrap.getMonthOfYear()   // 1..12

Example

final q = p6.wrap.of('2025-04-15').asDate('yyyy-MM-dd').getMonthOfYear() get()
assert q == 4

getDay

Get the day of the month of the date.

Syntax
NumberWrapper wrap.getDay()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d getDay() get()
assert value == 24

getHours

Get the hours of the date.

Syntax
NumberWrapper wrap.getHours()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d getHours() get()
assert value == 12

getMinutes

Get the minutes of the date.

Syntax
NumberWrapper wrap.getMinutes()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d getMinutes() get()
assert value == 35

getSeconds

Get the seconds of the date.

Syntax
NumberWrapper wrap.getSeconds()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d getSeconds() get()
assert value == 4

getDayOfWeek / isWeekend / getWeekOfYear / getQuarter / getMillis

New Feature

Since 6.10.24

Note

getDayOfWeek() follows ISO-8601 (Monday = 1 … Sunday = 7).

Syntax
NumberWrapper wrap.getDayOfWeek()     // ISO: Mon=1..Sun=7
boolean wrap.isWeekend()
NumberWrapper wrap.getWeekOfYear()
NumberWrapper wrap.getQuarter()       // 1..4
NumberWrapper wrap.getMillis()        // epoch millis

Example

final q = p6.wrap.of('2025-04-15').asDate('yyyy-MM-dd').getQuarter() get()
assert q == 2

Manipulation

add

Adds the specified modifier to the date.

Syntax
DateWrapper wrap.add(String modifier)

Note

The modifier string should follow the pattern [+-]\d+[smhdMy], where:

  • + o - indicates addition or substraction
  • \d+ is the number of units to add or substract
  • s for seconds, m for minutes, h for hours, d for days, M for months, and y for years.

Tip

You can use multiple modifiers in the same string. (i.e. +1d-1h)

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d add '+1d' getDay() get()
assert value == 25 

setFirstOfMonth

Sets the date to the first day of the month.

Syntax
NumberWrapper wrap.setFirstOfMonth()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d setFirstOfMonth() getDay() get()
assert value == 1

setLastOfMonth

Sets the date to the last day of the month.

Syntax
NumberWrapper wrap.setLastOfMonth()

Example

final d = new Date(1737722104000)
final value = p6.wrap.of d setLastOfMonth() getDay() get()
assert value == 31

setStartOfDay / setEndOfDay

New Feature

Since 6.10.24

Syntax
DateWrapper wrap.setStartOfDay()   // 00:00:00.000
DateWrapper wrap.setEndOfDay()     // 23:59:59.999

Example

final startOfDay = p6.wrap.now().setStartOfDay()

setFirstOfWeek / setLastOfWeek / setFirstOfYear / setLastOfYear / setFirstOfQuarter / setLastOfQuarter

New Feature

Since 6.10.24

Snap to a period boundary. The time-of-day is preserved (like setFirstOfMonth / setLastOfMonth) — chain setStartOfDay() / setEndOfDay() for a full range boundary. Week boundaries are ISO (Monday → Sunday).

Syntax
DateWrapper wrap.setFirstOfWeek()      // Monday
DateWrapper wrap.setLastOfWeek()       // Sunday
DateWrapper wrap.setFirstOfYear()      // Jan 1
DateWrapper wrap.setLastOfYear()       // Dec 31
DateWrapper wrap.setFirstOfQuarter()   // first day of Q1/Q2/Q3/Q4
DateWrapper wrap.setLastOfQuarter()    // last day of the quarter

Example

final quarterStart = p6.wrap.now().setFirstOfQuarter().setStartOfDay()

Comparisons

isBefore / isAfter / isSameOrBefore / isSameOrAfter / isBetween / isSameDay

New Feature

Since 6.10.24

Compares the wrapped date against another Date or DateWrapper.

Syntax
boolean wrap.isBefore(Date other)
boolean wrap.isAfter(Date other)
boolean wrap.isSameOrBefore(Date other)
boolean wrap.isSameOrAfter(Date other)
boolean wrap.isBetween(Date start, Date end)   // inclusive
boolean wrap.isSameDay(Date other)             // same calendar day

Example

final now = p6.wrap.now()
assert now.isAfter(p6.wrap.of('2000-01-01').asDate('yyyy-MM-dd').get())

Diff

secondsUntil / minutesUntil / hoursUntil / daysUntil / weeksUntil / monthsUntil

New Feature

Since 6.10.24

Returns the signed difference (other − this) in the requested unit, truncated toward zero. monthsUntil counts complete calendar months (matching ChronoUnit.MONTHS.between).

Syntax
NumberWrapper wrap.secondsUntil(Date other)
NumberWrapper wrap.minutesUntil(Date other)
NumberWrapper wrap.hoursUntil(Date other)
NumberWrapper wrap.daysUntil(Date other)
NumberWrapper wrap.weeksUntil(Date other)
NumberWrapper wrap.monthsUntil(Date other)

Example

final start = p6.wrap.of('2025-01-01').asDate('yyyy-MM-dd')
final days = start.daysUntil(p6.wrap.of('2025-01-11').asDate('yyyy-MM-dd').get()) get()
assert days == 10

Formatting & output

format

Formats the date using the specified format.

Syntax
DateWrapper wrap.format(String format)

Example

final d = new Date(1737722104000)
final String value = p6.wrap.of d format 'yyyy-MM-dd'
assert value == '2025-01-24'     

timezone

Set the timezone of the date.

Syntax
DateWrapper wrap.timezone(String timezone)

Example

final d = new Date(1737722104000)
final String value = p6.wrap.of d format 'yyyy-MM-dd hh:mm:ss' timezone 'GMT+1'
assert value == '2025-01-24 01:35:04'

Combined example

Derive a reporting window from an issue date and measure aging against it.

final issue = p6.wrap.of('2025-01-15').asDate('yyyy-MM-dd')

assert issue.getQuarter().get() == 1
final quarterEnd = issue.setLastOfQuarter().setEndOfDay()
assert issue.isSameOrBefore(quarterEnd.get())

final due = p6.wrap.of('2025-03-31').asDate('yyyy-MM-dd')
assert issue.monthsUntil(due.get()).get() == 2
assert issue.daysUntil(due.get()).gt(0)