DateService
Name | DateService |
Class Path |
|
Versions | 9 10 11 |
Overview
The DateService
class provides various utilities for working with dates, including formatting, epoch conversions, range validation, and date calculations.
Available Methods
String now(String format)
Returns the current date and time formatted according to the provided pattern.
Parameters:
format
(String
): The format pattern (e.g.,"yyyy-MM-dd HH:mm:ss"
).
Returns:
String
: The formatted current date.
long epoch()
Retrieves the current epoch timestamp in milliseconds.
Returns:
long
: Current timestamp.
long epoch(String date, String format)
Parses a date string into an epoch timestamp.
Parameters:
date
(String
): The date string.format
(String
): The format of the date string.
Returns:
long
: Epoch timestamp or-1
if parsing fails.
boolean inRange(long start, long end)
Checks if the current time falls within a specified range.
Parameters:
start
(long
): Start timestamp.end
(long
): End timestamp.
Returns:
boolean
:true
if the current time is within the range, otherwisefalse
.
String nowISO8601()
Gets the current date and time in ISO 8601 format.
Returns:
String
: ISO 8601 formatted date.
String formatISO8601(Number epoch)
Formats an epoch timestamp into an ISO 8601 date string.
Parameters:
epoch
(Number
): The epoch timestamp.
Returns:
String
: ISO 8601 formatted date.
String formatISO8601(Number epoch, String timezone)
Formats an epoch timestamp into an ISO 8601 date string with a specific timezone.
Parameters:
epoch
(Number
): The epoch timestamp.timezone
(String
): The desired timezone.
Returns:
String
: ISO 8601 formatted date.
String format(Number epoch, String format)
Formats an epoch timestamp using a specified format pattern.
Parameters:
epoch
(Number
): The epoch timestamp.format
(String
): The format pattern.
Returns:
String
: Formatted date string.
String format(Number epoch, String format, String timezone)
Formats an epoch timestamp with a specific format and timezone.
Parameters:
epoch
(Number
): The epoch timestamp.format
(String
): The format pattern.timezone
(String
): The desired timezone.
Returns:
String
: Formatted date string.
String calcDateISO8601(String addOrSubtract)
Calculates a new date by adding or subtracting a time unit from the current date and returns the result in ISO 8601 format.
Parameters:
addOrSubtract
(String
): Time adjustment (e.g.,"+1d"
,"-3h"
).
Returns:
String
: ISO 8601 formatted date.
String calcDate(String addOrSubtract, String format)
Calculates a new date by adding or subtracting a time unit from the current date and returns the result in a specified format.
Parameters:
addOrSubtract
(String
): Time adjustment (e.g.,"+1d"
,"-3h"
).format
(String
): The desired format pattern.
Returns:
String
: Formatted date.
String calcDate(long epoch, String addOrSubtract, String format)
Calculates a new date based on a given epoch timestamp and returns it in the specified format.
Parameters:
epoch
(long
): The reference epoch timestamp.addOrSubtract
(String
): Time adjustment (e.g.,"+1d"
,"-3h"
).format
(String
): The desired format pattern.
Returns:
String
: Formatted date.
long calcDateAsEpoch(String addOrSubtract)
Calculates a new epoch timestamp by adding or subtracting a time unit from the current date.
Parameters:
addOrSubtract
(String
): Time adjustment (e.g.,"+1d"
,"-3h"
).
Returns:
long
: Updated epoch timestamp.
long calcDateAsEpoch(long dateInEpoch, String addOrSubtract)
Calculates a new epoch timestamp based on a given epoch and time adjustment.
Parameters:
dateInEpoch
(long
): The reference epoch timestamp.addOrSubtract
(String
): Time adjustment (e.g.,"+1d"
,"-3h"
).
Returns:
long
: Updated epoch timestamp.
Date Calculation Units
When using the calcDateAsEpoch
and related methods, the following units can be used:
s
→ secondsm
→ minutesh
→ hoursd
→ days
For example:
"+1d"
→ Adds one day"-3h"
→ Subtracts three hours
Summary
The DateService
class provides an extensive set of methods for handling date and time operations, including:
Formatting timestamps in various formats.
Converting dates to and from epoch timestamps.
Validating time ranges.
Performing calculations on dates (addition/subtraction).
This utility simplifies date-related operations, making it efficient for applications that require precise time handling.