Skip to main content

DateTime


id: py-DateTime

DateTime

Represents a point in time with date, time, and timezone offset information. Use DateTime when you need to work with timestamps, compare moments, or format dates for output.

DateTime instances are immutable — all modification methods return new instances.


At a Glance

# Current time
now = DateTime.now()

# Specific moment
meeting = DateTime.of(2024, 12, 25, 14, 30)

# From a string
parsed = DateTime.parse('2024-12-25 14:30:00', 'uuuu-MM-dd HH:mm:ss')

# Format for output
stream.log_info(meeting.toString('EEEE, MMM dd uuuu')) # "Wednesday, Dec 25 2024"

Properties

PropertyTypeDescription
yearintYear (e.g., 2024)
monthintMonth of year, 1–12
dayOfMonthintDay of month, 1–31
dayOfWeekintDay of week, 1 (Monday) – 7 (Sunday)
dayOfYearintDay of year, 1–366
hourintHour of day, 0–23
minuteintMinute of hour, 0–59
secondintSecond of minute, 0–59
nanointNanosecond of second, 0–999,999,999
dateLocalDateDate component only
timeTimeTime component only
dt = DateTime.of(2024, 3, 15, 10, 30, 45)

stream.log_info(f"{dt.year}-{dt.month}-{dt.dayOfMonth}") # "2024-3-15"
stream.log_info(f"{dt.hour}:{dt.minute}:{dt.second}") # "10:30:45"
stream.log_info(f"Day {dt.dayOfYear} of the year") # "Day 75 of the year"
stream.log_info(f"Day of week: {dt.dayOfWeek}") # 5 (Friday)

Creating DateTime

now(zone?)

Returns the current date and time.

ParameterTypeDescription
zone (optional)TimeZoneTimezone; defaults to system timezone
now = DateTime.now()
ny_now = DateTime.now(TimeZone.of('America/New_York'))

of(year, month?, day?, hour?, minute?, second?, nano?, zone?)

Creates a DateTime from individual components.

# Minimal: just year
y2k = DateTime.of(2000)

# Date only
birthday = DateTime.of(1990, 5, 15)

# Full datetime
precise = DateTime.of(2024, 12, 25, 14, 30, 0, 0)

# With timezone offset
india_time = DateTime.of(2024, 12, 25, 14, 30, 0, 0, ZoneOffset.of(5, 30))

parse(value, format?)

Parses a string into a DateTime.

# ISO format (default)
dt1 = DateTime.parse('2024-12-25T14:30:00')

# Custom format
dt2 = DateTime.parse('25/12/2024 14:30', 'dd/MM/uuuu HH:mm')
Format Patterns

See Java DateTimeFormatter for full pattern syntax. Common patterns:

  • uuuu — year (2024)
  • MM — month (03)
  • dd — day (15)
  • HH — hour 24h (14)
  • mm — minute (30)
  • ss — second (45)

Comparing DateTimes

MethodReturnsDescription
isAfter(other)boolThis moment is later than other
isBefore(other)boolThis moment is earlier than other
isEqual(other)boolSame moment in time
start = DateTime.parse('2024-01-01T00:00:00')
end = DateTime.parse('2024-12-31T23:59:59')

if end.isAfter(start):
stream.log_info('End is after start')

# Check if within business hours
now = DateTime.now()
if now.hour >= 9 and now.hour < 17:
stream.log_info('Within business hours')

Adding Time

All plusX() methods return a new DateTime. The original is unchanged.

MethodDescription
plusYears(years)Add years
plusMonths(months)Add months
plusDays(days)Add days
plusHours(hours)Add hours
plusMinutes(minutes)Add minutes
plusSeconds(seconds)Add seconds
plusNanos(nanos)Add nanoseconds
now = DateTime.now()

tomorrow = now.plusDays(1)
next_week = now.plusDays(7)
next_hour = now.plusHours(1)

# Calculate expiry (30 days from now)
expiry = now.plusDays(30)
message.setDateTime(dataDictionary.type.Order.EXPIRY_DATE, expiry)

Subtracting Time

MethodDescription
minusYears(years)Subtract years
minusMonths(months)Subtract months
minusDays(days)Subtract days
minusHours(hours)Subtract hours
minusMinutes(minutes)Subtract minutes
minusSeconds(seconds)Subtract seconds
minusNanos(nanos)Subtract nanoseconds
now = DateTime.now()
yesterday = now.minusDays(1)
last_month = now.minusMonths(1)

# Check if record is older than 90 days
created = message.getDateTime(dataDictionary.type.Record.CREATED_AT)
if created.plusDays(90).isBefore(now):
message.addStatus(Severity.WARNING, Status.create(VENDOR, 'STALE_RECORD'))

Changing Components

Set individual components without affecting others.

MethodDescription
withYear(year)Set year
withMonth(month)Set month (1–12)
withDayOfMonth(day)Set day of month (1–31)
withHour(hour)Set hour (0–23)
withMinute(minute)Set minute (0–59)
withSecond(second)Set second (0–59)
withNano(nano)Set nanosecond
dt = DateTime.of(2024, 3, 15, 10, 30)

# Same date, different time
noon = dt.withHour(12).withMinute(0)

# Same time, first of month
first_of_month = dt.withDayOfMonth(1)

Timezone Conversion

atZone(zone)

Returns the same moment in a different timezone.

utc = DateTime.now(TimeZone.UTC)
berlin = utc.atZone(TimeZone.of('Europe/Berlin'))
tokyo = utc.atZone(TimeZone.of('Asia/Tokyo'))

stream.log_info(f"UTC: {utc.toString()}")
stream.log_info(f"Berlin: {berlin.toString()}")
stream.log_info(f"Tokyo: {tokyo.toString()}")

Converting to Numbers

MethodReturnsDescription
toEpochSecond()intSeconds since Unix epoch (1970-01-01T00:00:00Z)
toEpochMilli()intMilliseconds since Unix epoch
now = DateTime.now()
epoch_seconds = now.toEpochSecond() # 1700000000
epoch_millis = now.toEpochMilli() # 1700000000000

Formatting

toString(format?)

Converts to a formatted string.

dt = DateTime.of(2024, 12, 25, 14, 30, 0)

# Default ISO format
dt.toString() # "2024-12-25T14:30:00"

# Custom formats
dt.toString('uuuu-MM-dd') # "2024-12-25"
dt.toString('dd/MM/uuuu HH:mm') # "25/12/2024 14:30"
dt.toString('EEEE, MMMM dd uuuu') # "Wednesday, December 25 2024"

Complete Example

def on_message():
order_date = message.getDateTime(dataDictionary.type.Order.ORDER_DATE)
now = DateTime.now()

# Check if order is recent (within 7 days)
if order_date.plusDays(7).isAfter(now):
stream.log_info('Recent order')

# Set expected delivery (5 business days later — simplified)
delivery_date = order_date.plusDays(5)
message.setDateTime(dataDictionary.type.Order.EXPECTED_DELIVERY, delivery_date)

# Format for customer notification
formatted = delivery_date.toString('EEEE, MMMM dd uuuu')
message.setString(dataDictionary.type.Order.DELIVERY_TEXT, formatted)

stream.emit(message, OUTPUT_PORT)

See Also