com.funambol.util
Class DateUtil

java.lang.Object
  extended by com.funambol.util.DateUtil

public class DateUtil
extends java.lang.Object

Utility class for date manipulation. This class gives a simple interface for common Date, Calendar and Timezone operations. It is possible to apply subsequent transformations to an initial date, and retrieve the changed Date object at any point.


Constructor Summary
DateUtil()
          Inizialize a new instance with the current date
DateUtil(java.util.Date d)
          Inizialize a new instance with the given date
 
Method Summary
 DateUtil addMinutes(int minutes)
          Make the date go forward of the specified amount of minutes The internal date is changed after this call.
static java.lang.String formatDate(java.util.Calendar date)
          Format a date to standard format.
static java.lang.String formatDateTimeUTC(java.util.Calendar datetime)
          Format a date-time into UTC format.
static java.lang.String formatDateTimeUTC(java.util.Date date)
          Format a date-time into UTC format.
static java.lang.String formatDateTimeUTC(long date)
          Format a date-time into UTC format.
 int getDaysSince(java.util.Date date)
          Get the days passed from the specified date up to the date provided in the constructor
static java.util.Calendar getSafeTimeForAllDay(java.util.Calendar cal)
          Get a safe time for all day given a certain calendar.
 java.util.Date getTime()
          Get the current time
 java.lang.String getTZ()
          Get the current TimeZone
 boolean isAfter(java.util.Date date)
          Utility method wrapping Calendar.after method Compares the date field parameter with the date provided with the constructor answering the question: date from constructor is after the given param date ?
static java.util.Calendar parseDate(java.lang.String field)
          Deprecated. use parseDateTime instead
static java.util.Calendar parseDateTime(java.lang.String field)
          Parse date and time from a string.
 DateUtil removeDays(int days)
          Make the date go back of the specified amount of days The internal date is changed after this call.
 void setTime(java.util.Date d)
          Set a new time
 DateUtil toGMT()
          Convert the date to GMT.
 DateUtil toMidnight()
          Convert the time to the midnight of the currently set date.
 DateUtil toTZ(java.lang.String tz)
          Convert the date to the given timezone.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateUtil

public DateUtil()
Inizialize a new instance with the current date


DateUtil

public DateUtil(java.util.Date d)
Inizialize a new instance with the given date

Method Detail

setTime

public void setTime(java.util.Date d)
Set a new time


getTime

public java.util.Date getTime()
Get the current time


getTZ

public java.lang.String getTZ()
Get the current TimeZone


toMidnight

public DateUtil toMidnight()
Convert the time to the midnight of the currently set date. The internal date is changed after this call.

Returns:
a reference to this DateUtil, for concatenation.

removeDays

public DateUtil removeDays(int days)
Make the date go back of the specified amount of days The internal date is changed after this call.

Returns:
a reference to this DateUtil, for concatenation.

addMinutes

public DateUtil addMinutes(int minutes)
Make the date go forward of the specified amount of minutes The internal date is changed after this call.

Returns:
a reference to this DateUtil, for concatenation.

toGMT

public DateUtil toGMT()
Convert the date to GMT. The internal date is changed

Returns:
a reference to this DateUtil, for concatenation.

toTZ

public DateUtil toTZ(java.lang.String tz)
Convert the date to the given timezone. The internal date is changed.

Parameters:
tz - The name of the timezone to set
Returns:
a reference to this DateUtil, for concatenation.

getDaysSince

public int getDaysSince(java.util.Date date)
Get the days passed from the specified date up to the date provided in the constructor

Parameters:
date - The starting date
Returns:
number of days within date used in constructor and the provided date

isAfter

public boolean isAfter(java.util.Date date)
Utility method wrapping Calendar.after method Compares the date field parameter with the date provided with the constructor answering the question: date from constructor is after the given param date ?

Parameters:
date - The date to be used for comparison
Returns:
true if date from constructor is after given param date

formatDateTimeUTC

public static java.lang.String formatDateTimeUTC(java.util.Calendar datetime)
Format a date-time into UTC format. For example the following string 19980119T070000Z represents 19th Jan 1998 at 07:00:00 in the GMT timezone

Parameters:
datetime - is the date-time to be formatted
Returns:
the UTC representation of the give date-time

formatDate

public static java.lang.String formatDate(java.util.Calendar date)
Format a date to standard format.

Parameters:
date - is the date to be formatted
Returns:
the representation of the give date

formatDateTimeUTC

public static java.lang.String formatDateTimeUTC(java.util.Date date)
Format a date-time into UTC format. For example the following string 19980119T070000Z represents 19th Jan 1998 at 07:00:00 in the GMT timezone

Parameters:
date - is the date to be formatted. This value will be interpreted as relative to the default timezone
Returns:
the UTC representation of the give date-time

formatDateTimeUTC

public static java.lang.String formatDateTimeUTC(long date)
Format a date-time into UTC format. For example the following string 19980119T070000Z represents 19th Jan 1998 at 07:00:00 in the GMT timezone

Parameters:
date - is the date to be formatted. This value will be interpreted as relative to the default timezone
Returns:
the UTC representation of the give date-time

parseDate

public static java.util.Calendar parseDate(java.lang.String field)
Deprecated. use parseDateTime instead

Parse date and time from a string. The method supports multiple formats, in particular:
  • - yyyyMMddTHHmmssZ - yyyyMMddTHHmmss - yyyyMMdd - yyyy-MM-ddTHH:mm:ssZ - yyyy-MM-ddTHH:mm:ss

    Parameters:
    field - date time in one of the supported formats
    Returns:
    a calendar representing the date time
    Throws:
    java.lang.IllegalArgumentException - if the input date is not valid (according to the acceptable formats)

  • parseDateTime

    public static java.util.Calendar parseDateTime(java.lang.String field)
    Parse date and time from a string. The method supports multiple formats, in particular:
  • - yyyyMMddTHHmmssZ - yyyyMMddTHHmmss - yyyyMMdd - yyyy-MM-ddTHH:mm:ssZ - yyyy-MM-ddTHH:mm:ss

    Parameters:
    field - date time in one of the supported formats
    Returns:
    a calendar representing the date time
    Throws:
    java.lang.IllegalArgumentException - if the input date is not valid (according to the acceptable formats)

  • getSafeTimeForAllDay

    public static java.util.Calendar getSafeTimeForAllDay(java.util.Calendar cal)
    Get a safe time for all day given a certain calendar. The value returned by this method is guaranteed to be in the given day, even if it is shifted during UTC transformation. This transformation is usually helpful for dates associated to all day events.

    Parameters:
    cal - a calendar
    Returns:
    the same calendar whose hour is shifted in a way that UTC translation will leave the event in the same day


    Copyright © 2001-2009 Funambol.