org.pentaho.platform.util
Class DateMath

java.lang.Object
  extended by org.pentaho.platform.util.DateMath

public class DateMath
extends Object

Provides a utility for calculating relative dates. The class calculates a date based upon an expression. The syntax of the expression is given below.

Date Expression

       <expression>     := <expression>+ ( ';' DATESPEC )?
       <expression>     := OPERATION? OPERAND ':' <unit> <position>?
       <unit>           := 'Y' | 'M' | 'D' | 'W' | 'h' | 'm' | 's'
       <position>       := 'S' | 'E' 
       OPERATION        := '+' | '-'
       OPERAND          := [0..9]+
       DATESPEC         := <i>any SimpleDateFormat format pattern</i>
 
The OPERAND specifies the positive or negative offset to the date. The unit inidcates the unit of the date to manipulate. The optional position indicates the relative position for the specified unit: S for start and E for end. The following are the valid unit values.
       Y        Year
       M        Month
       W        Week
       D        Day
       h        hour
       m        minute
       s        second
 

Examples:

       0:ME -1:DS       00:00:00.000 of the day before the last day of the current month
       0:MS  0:WE       23:59:59.999 the last day of the first week of the month
       0:ME             23:59:59.999 of the last day of teh current month
       5:Y              the current monty, day and time 5 years in the future
       5:YS             00:00:00.000 of the first day of the years 5 years in the future
 


Constructor Summary
DateMath()
           
 
Method Summary
static Calendar calculateDate(Calendar date, String expression)
          Calculates the date specified by the expression, relative to the indicated date/time.
static Calendar calculateDate(String expression)
          Calculates the date specified by the expression, relative to the current date/time.
static String calculateDateString(Calendar date, String expressionWithFormat)
          Calculates a date, returning the formatted string version of the calculated date.
static String calculateDateString(Calendar date, String expressionWithFormat, Locale locale)
          Calculates a date, returning the formatted string version of the calculated date.
static String claculateDateString(String expressionWithFormat)
          Calculates a date, returning the formatted string version of the calculated date.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateMath

public DateMath()
Method Detail

claculateDateString

public static String claculateDateString(String expressionWithFormat)
Calculates a date, returning the formatted string version of the calculated date. The method is a short cut for calculateDate(null,expressionWithFormat,null). If the date format is omitted, the short format for the PentahoSystem#getLocale() is used.

Parameters:
expressionWithFormat - the relative date expression with optional format specification.
Returns:
The calculated date as a string.
Throws:
IllegalArgumentException - if expressionWithFormat is invalid.

calculateDateString

public static String calculateDateString(Calendar date,
                                         String expressionWithFormat)
Calculates a date, returning the formatted string version of the calculated date. The method is a short cut for calculateDate(date,expressionWithFormat,null).

Parameters:
date - the target date against the expression will be applied.
expressionWithFormat - the relative date expression with optional format specification.
Returns:
The calculated date as a string.
Throws:
IllegalArgumentException - if expressionWithFormat is invalid.

calculateDateString

public static String calculateDateString(Calendar date,
                                         String expressionWithFormat,
                                         Locale locale)
Calculates a date, returning the formatted string version of the calculated date.

Parameters:
date - the target date against the expression will be applied. If null, the current date is used.
expressionWithFormat - the relative date expression with optional format specification.
locale - the desired locale for the formatted string.
Returns:
The calculated date as a string.
Throws:
IllegalArgumentException - if expressionWithFormat is invalid.

calculateDate

public static Calendar calculateDate(String expression)
Calculates the date specified by the expression, relative to the current date/time. The method is a short cut for calculate(null,expression).

Parameters:
expression - the date expression as described above.
Returns:
The calculated date.
Throws:
IllegalArgumentException - if expression is invalid.

calculateDate

public static Calendar calculateDate(Calendar date,
                                     String expression)
Calculates the date specified by the expression, relative to the indicated date/time.

Parameters:
date - the target date against the expression is evaluated. If null, the current date/time is used. If not null, the object is manipulated by the expression.
expression - the date expression as described above.
Returns:
The calculated date. This will be date if date is not null.