com.sibvisions.util.type
Class DateUtil

java.lang.Object
  extended by com.sibvisions.util.type.DateUtil

public class DateUtil
extends Object

The DateUtil is a utility class for date conversion and for formatting dates as string.


Constructor Summary
DateUtil()
          Constructs a new instance of DateUtil with default date format.
DateUtil(DateFormat pDateFormat)
          Deprecated. since 2.7, use DateUtil(String) or DateUtil(String, Locale) instead. Using this function might lead to undefined behavior because of a new caching mechanism based on the Locale of the DateUtil. However, there is no way to extract the Locale that has been used from a DateFormat, so it is always treated as using LocaleUtil.getDefault().
DateUtil(String pDatePattern)
          Constructs a new instance of DateUtil that supports empty Strings and null values.
DateUtil(String pDatePattern, Locale pLocale)
          Constructs a new instance of DateUtil that supports empty Strings and null values.
 
Method Summary
static Date convert(Date pDate, String pFromTimeZone, String pToTimeZone)
          Converts a date from one timezone to another timezone.
static Date convert(Date pDate, TimeZone pFromTimeZone, TimeZone pToTimeZone)
          Converts a date from one timezone to another timezone.
 String format(Date pDate)
          Formats the date to text.
static String format(Date pDate, String pFormat)
          Formats a Date into a date/time string.
static String format(long pDate, String pFormat)
          Formats a time string.
static Date getDate(int pDay, int pMonth, int pYear, int pHour, int pMinutes, int pSeconds)
          Creates a new date instance.
 DateFormat getDateFormat()
          Deprecated. 
 String getDatePattern()
          Gets the date format pattern.
 boolean isStrictFormatCheck()
          Strict format check defines whether the set pattern should be exactly by format, or be more flexibel in analysing the given date.
 Date parse(String pText)
          Parses the date from text.
 void setDateFormat(DateFormat pDateFormat)
          Deprecated. since 2.7, use setDatePattern(String) or setDatePattern(String, Locale) instead. Using this function might lead to undefined behavior because of a new caching mechanism based on the Locale of the DateUtil. However, there is no way to extract the Locale that has been used from a DateFormat, so it is always treated as using LocaleUtil.getDefault().
 void setDatePattern(String pDatePattern)
          Sets the date format pattern.
 void setDatePattern(String pDatePattern, Locale pLocale)
          Sets the date format pattern.
 void setStrictFormatCheck(boolean pStrictFormatCheck)
          Strict format check defines whether the set pattern should be exactly by format, or be more flexibel in analysing the given date.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DateUtil

public DateUtil()
Constructs a new instance of DateUtil with default date format.


DateUtil

@Deprecated
public DateUtil(DateFormat pDateFormat)
Deprecated. since 2.7, use DateUtil(String) or DateUtil(String, Locale) instead. Using this function might lead to undefined behavior because of a new caching mechanism based on the Locale of the DateUtil. However, there is no way to extract the Locale that has been used from a DateFormat, so it is always treated as using LocaleUtil.getDefault().

Constructs a new instance of DateUtil that supports empty Strings and null values.

Parameters:
pDateFormat - the formatter that should support empty Strings and null values

DateUtil

public DateUtil(String pDatePattern)
Constructs a new instance of DateUtil that supports empty Strings and null values.

As no Locale is specified, LocaleUtil.getDefault() will always be used.

Parameters:
pDatePattern - the pattern that should support empty Strings and null values. null to use the default.

DateUtil

public DateUtil(String pDatePattern,
                Locale pLocale)
Constructs a new instance of DateUtil that supports empty Strings and null values.

Parameters:
pDatePattern - the pattern that should support empty Strings and null values. null to use the default.
pLocale - the Locale to use. null to always use LocaleUtil.getDefault().
Method Detail

parse

public Date parse(String pText)
           throws ParseException
Parses the date from text.

Parameters:
pText - the text.
Returns:
the parsed date.
Throws:
ParseException - if there is an error in the conversion

format

public String format(Date pDate)
Formats the date to text.

Parameters:
pDate - the date.
Returns:
the formatted text.

getDateFormat

@Deprecated
public DateFormat getDateFormat()
Deprecated. 

Gets the date format.

Returns:
the date format.

setDateFormat

public void setDateFormat(DateFormat pDateFormat)
Deprecated. since 2.7, use setDatePattern(String) or setDatePattern(String, Locale) instead. Using this function might lead to undefined behavior because of a new caching mechanism based on the Locale of the DateUtil. However, there is no way to extract the Locale that has been used from a DateFormat, so it is always treated as using LocaleUtil.getDefault().

Gets the date format.

Parameters:
pDateFormat - the date format.

getDatePattern

public String getDatePattern()
Gets the date format pattern.

Returns:
the date format pattern.
See Also:
setDatePattern(String), setDatePattern(String, Locale)

setDatePattern

public void setDatePattern(String pDatePattern)
Sets the date format pattern.

This function will reset the currently set Locale to null, which means that LocaleUtil.getDefault() is used.

Parameters:
pDatePattern - the date format pattern.
See Also:
setDatePattern(String, Locale)

setDatePattern

public void setDatePattern(String pDatePattern,
                           Locale pLocale)
Sets the date format pattern.

Parameters:
pDatePattern - the date format pattern.
pLocale - the Locale to use. null to always use LocaleUtil.getDefault().
See Also:
setDatePattern(String)

isStrictFormatCheck

public boolean isStrictFormatCheck()
Strict format check defines whether the set pattern should be exactly by format, or be more flexibel in analysing the given date. This has nothing to do with lenient in SimpleDateFormat. Lenient is set to false anyway. Only correct dates are allowed. The following will be allowed without strict check, with locale de_AT and pattern: dd. MMMM.yyyy HH:mm 01.01.2016 01.Januar.2016 00:00 01.Jänner.2016 01.01 01.01.16 01.01 01 01 2016 00 00 010116 01012016 The result will always be 01.Januar.2016 00:00

Returns:
true, if format should be checked strict.

setStrictFormatCheck

public void setStrictFormatCheck(boolean pStrictFormatCheck)
Strict format check defines whether the set pattern should be exactly by format, or be more flexibel in analysing the given date. This has nothing to do with lenient in SimpleDateFormat. Lenient is set to false anyway. Only correct dates are allowed. The following will be allowed without strict check, with locale de_AT and pattern: dd. MMMM.yyyy HH:mm 01.01.2016 01.Januar.2016 00:00 01.Jänner.2016 01.01 01.01.16 01.01 01 01 2016 00 00 010116 01012016 The result will always be 01.Januar.2016 00:00

Parameters:
pStrictFormatCheck - true, if format should be checked strict.

format

public static String format(long pDate,
                            String pFormat)
Formats a time string.

Parameters:
pDate - the time (in millis since 01.01.1970 00:00) value to be formatted into a time string
pFormat - the format
Returns:
the formatted date/time string
See Also:
SimpleDateFormat

format

public static String format(Date pDate,
                            String pFormat)
Formats a Date into a date/time string.

Parameters:
pDate - the time value to be formatted into a time string
pFormat - the format
Returns:
the formatted date/time string
See Also:
SimpleDateFormat

getDate

public static Date getDate(int pDay,
                           int pMonth,
                           int pYear,
                           int pHour,
                           int pMinutes,
                           int pSeconds)
Creates a new date instance.

Parameters:
pDay - the day of month
pMonth - the month
pYear - the year
pHour - the hour of day
pMinutes - the minutes
pSeconds - the seconds
Returns:
the date

convert

public static Date convert(Date pDate,
                           String pFromTimeZone,
                           String pToTimeZone)
Converts a date from one timezone to another timezone.

Parameters:
pDate - the date
pFromTimeZone - the timezone of the date
pToTimeZone - the expected timezone
Returns:
the date converted to the expected timezone

convert

public static Date convert(Date pDate,
                           TimeZone pFromTimeZone,
                           TimeZone pToTimeZone)
Converts a date from one timezone to another timezone.

Parameters:
pDate - the date
pFromTimeZone - the timezone of the date
pToTimeZone - the expected timezone
Returns:
the date converted to the expected timezone


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.