com.emarsys.ecommon.time
Class Dates

java.lang.Object
  extended by com.emarsys.ecommon.time.Dates

public class Dates
extends java.lang.Object

Common utilities for handling dates.

Author:
johan, Michael "kULO" Kulovits

Field Summary
static int DAY_IN_MILLIS
           
static java.text.DateFormat FORMAT_TIMESTAMP
           
static int HOUR_IN_MILLIS
           
static int MINUTE_IN_MILLIS
           
static int SECOND_IN_MILLIS
           
static int WEEK_IN_MILLIS
           
 
Constructor Summary
Dates()
           
 
Method Summary
static java.util.Calendar add(java.util.Calendar cal, int millis)
          Creates a new Calendar instance by cloning the passed one and adds the passed milliseconds to it.
static java.util.Calendar add(java.util.Calendar cal, int field, int amount)
          Creates a new Calendar instance by cloning the passed one and adds the time specified through the passed field and amount.
static java.lang.String format(java.util.Date date, java.text.DateFormat fmt)
          Formats the passed date with the passed DateFormat and returns the String representation or null iff the passed date is null.
static java.lang.String format(java.util.Date date, java.text.DateFormat fmt, java.lang.String nullSurrogate)
          Formats the passed date with the passed DateFormat and returns the String representation or nullSurrogate iff the passed date is null.
static int getDayOfMonthCeil(java.util.Calendar cal, int dayOfMonth)
          correct day of month regarding maximum day of month, ceiling behavior.
static java.util.Calendar getMean(java.util.Calendar start, java.util.Calendar end)
          The passed Calendar will be a clone of the first parameter.
static int nbrOfDaysFromNowToDate(java.util.Date date)
           
static int nbrOfMonthsFromNowToDate(java.util.Date date)
           
static java.lang.String timestampToString(java.util.Calendar cal)
          Formats, see format(Date, DateFormat), the passed Calendar with FORMAT_TIMESTAMP and returns the String representation or null iff the passed calendar is null.
static java.lang.String timestampToString(java.util.Date date)
          Formats, see format(Date, DateFormat), the passed Date with FORMAT_TIMESTAMP and returns the String representation or null iff the passed date is null.
static java.util.Calendar toCalendar(java.util.Date date)
          Converts the passed Date instance into a Calendar.
static java.util.Calendar toCalendar(long timeMillis)
          Creates a new Calendar with its time set to the passed milliseconds.
static java.util.Date valueOf(java.util.Calendar cal)
          Converts the passed Calendar into Date instance.
static java.util.Date valueOf(long timeMillis)
          Creates a new Date instance with its time set to the passed milliseconds.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SECOND_IN_MILLIS

public static final int SECOND_IN_MILLIS
See Also:
Constant Field Values

MINUTE_IN_MILLIS

public static final int MINUTE_IN_MILLIS
See Also:
Constant Field Values

HOUR_IN_MILLIS

public static final int HOUR_IN_MILLIS
See Also:
Constant Field Values

DAY_IN_MILLIS

public static final int DAY_IN_MILLIS
See Also:
Constant Field Values

WEEK_IN_MILLIS

public static final int WEEK_IN_MILLIS
See Also:
Constant Field Values

FORMAT_TIMESTAMP

public static final java.text.DateFormat FORMAT_TIMESTAMP
Constructor Detail

Dates

public Dates()
Method Detail

nbrOfDaysFromNowToDate

public static int nbrOfDaysFromNowToDate(java.util.Date date)
Parameters:
date -
Returns:

nbrOfMonthsFromNowToDate

public static int nbrOfMonthsFromNowToDate(java.util.Date date)
Parameters:
date -
Returns:

getDayOfMonthCeil

public static int getDayOfMonthCeil(java.util.Calendar cal,
                                    int dayOfMonth)
correct day of month regarding maximum day of month, ceiling behavior.

Parameters:
cal - - set to date within month where calculation shall be done.
dayOfMonth - - to correct.
Returns:
original day of month or maximum day of this month.

format

public static java.lang.String format(java.util.Date date,
                                      java.text.DateFormat fmt,
                                      java.lang.String nullSurrogate)
Formats the passed date with the passed DateFormat and returns the String representation or nullSurrogate iff the passed date is null.

Parameters:
date -
fmt -
Returns:
always a valid String

format

public static java.lang.String format(java.util.Date date,
                                      java.text.DateFormat fmt)
Formats the passed date with the passed DateFormat and returns the String representation or null iff the passed date is null.

Parameters:
date -
fmt -
Returns:
always a valid String or null

timestampToString

public static java.lang.String timestampToString(java.util.Date date)
Formats, see format(Date, DateFormat), the passed Date with FORMAT_TIMESTAMP and returns the String representation or null iff the passed date is null.

Parameters:
date - - the timestamp to be formated
Returns:
always a valid String or null.

timestampToString

public static java.lang.String timestampToString(java.util.Calendar cal)
Formats, see format(Date, DateFormat), the passed Calendar with FORMAT_TIMESTAMP and returns the String representation or null iff the passed calendar is null.

Parameters:
date - - the timestamp to be formated
Returns:
always a valid String or null.

toCalendar

public static java.util.Calendar toCalendar(java.util.Date date)
Converts the passed Date instance into a Calendar.

Parameters:
date - - the Date to be converted.
Returns:
the passed Date converted into a new Calendar instance with the default Locale and TimeZone, if null is passed null will be returned.

toCalendar

public static java.util.Calendar toCalendar(long timeMillis)
Creates a new Calendar with its time set to the passed milliseconds.

Parameters:
timeMillis -
Returns:
always a valid Calendar instance.

valueOf

public static java.util.Date valueOf(java.util.Calendar cal)
Converts the passed Calendar into Date instance.

Parameters:
cal - - the Calendar instance to be converted to a Date
Returns:
the Date returned by the passed instance's Calendar.getTime() method or null if the passed calendar is null.

valueOf

public static java.util.Date valueOf(long timeMillis)
Creates a new Date instance with its time set to the passed milliseconds.

Parameters:
timeMillis -
Returns:
always a valid Date instance.

getMean

public static java.util.Calendar getMean(java.util.Calendar start,
                                         java.util.Calendar end)
The passed Calendar will be a clone of the first parameter.

Parameters:
start -
end -
Returns:
a new Calendar instance that has its time set to the arithmetical mean of the passed dates.
Throws:
java.lang.NullPointerException - - if any of the passed dates is null.

add

public static java.util.Calendar add(java.util.Calendar cal,
                                     int field,
                                     int amount)
Creates a new Calendar instance by cloning the passed one and adds the time specified through the passed field and amount.

Parameters:
cal - -
field -
amount -
Returns:
always a new Calendar instance with its time set to the passed ones and added the passed amount of time.

add

public static java.util.Calendar add(java.util.Calendar cal,
                                     int millis)
Creates a new Calendar instance by cloning the passed one and adds the passed milliseconds to it.

Parameters:
cal - -
field -
amount -
Returns:
always a new Calendar instance with its time set to the passed ones and added the passed amount of time.


Copyright © 2010 emarsys AG. All Rights Reserved.