public class DateHelper
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
DateHelper.ThreadSafeDateFormat
Thread save DateFormat
|
| Modifier and Type | Field and Description |
|---|---|
static DateHelper.ThreadSafeDateFormat |
ISO_DATE
a Thread safe implementation of DateFormat for an ISO date
|
| Modifier and Type | Method and Description |
|---|---|
static int |
betweenDate(java.util.Calendar c1,
java.util.Calendar c2,
int field,
boolean round)
calculate the difference between two
Calendars in intervals defined by
field. |
static int |
betweenDate(java.util.Date d1,
java.util.Date d2,
int field,
boolean round)
calculate the difference between two
Dates in intervals defined by
field. |
static java.lang.String |
fromDate(java.util.Date dt)
gets a date formatted as String with pattern "dd-MMM-yyyy"
|
static java.lang.String |
isoFromDate(java.util.Date d)
Writes a date to an ISO-string without time
|
static java.lang.String |
isoFromDate(java.util.Date d,
boolean includeTime)
Writes a date to an ISO-string, either with or without time
|
static java.util.Date |
isoToDate(java.lang.String s)
Convert an ISO-date String into a Date
|
static java.util.Date |
max(java.util.Date left,
java.util.Date right)
Return the last Date for two compared Dates
|
static java.util.Date |
min(java.util.Date left,
java.util.Date right)
Return the first Date for two compared Dates
|
static java.util.Date |
toDate(java.lang.String txt)
returns a Date from a String representing a Date
|
static java.util.Date |
toDate(java.lang.String txt,
java.util.Locale locale)
Attempts to convert a String to a Date in the most flexible manor using the specific locale to determine names and order
|
public static final DateHelper.ThreadSafeDateFormat ISO_DATE
public static final java.lang.String fromDate(java.util.Date dt)
dt - java.util.Datepublic static final java.util.Date toDate(java.lang.String txt)
throws java.lang.NumberFormatException
This toDate function is basically a rule based system recognizing a Date in the following patterns:
ddMMyy
day-month-year
day can be either 1 or 2 digits, with or without a
leading 0
month can be either 1 or 2 digits, with or without a
leading 0, a String representing the long name or a short name of the
month. The name of the month must be in the same language at which the VM
it is operating is running in Locale.getDefault() is used to determine
this.
year can be 1 or 2 digits, with or without a leading
0. This is interpreted as a year without a century, so the current
century is added. It can also be four digits and will then be intrepeted
as year including a century.
txt - String that should be interpreted as Datejava.lang.NumberFormatException - thrown if at an expected number was not found at the Expected
position.public static final java.util.Date toDate(java.lang.String txt,
java.util.Locale locale)
throws java.lang.NumberFormatException
txt - Stringlocale - Localejava.lang.NumberFormatException - thrown when String cannot be coverted to a datepublic static final java.lang.String isoFromDate(java.util.Date d,
boolean includeTime)
d - Date to be convertedincludeTime - boolean true is to include timepublic static final java.lang.String isoFromDate(java.util.Date d)
d - Date to be convertedisoFromDate(Date, boolean)public static final java.util.Date isoToDate(java.lang.String s)
Will work for ISO-date Strings with or without time
s - ISO formatted Stringpublic static final int betweenDate(java.util.Date d1,
java.util.Date d2,
int field,
boolean round)
Dates in intervals defined by
field.
The compare of the calendars follows normal conventions, so if c1 > c2 you will get a negative number. If rounding is on the level below is measured and normal rounding rules apply.
The field value should be one of Calendar.YEAR,Calendar.MONTH,Calendar.DATE
d1 - Date one (required)d2 - Date two (required)field - int, should be in (Calendar.YEAR|Calendar.MONTH|Calendar.DATE)round - boolean, true if the should difference be rounded according to the normal rounding rules.False will floor the valueDates in the defined measurepublic static final int betweenDate(java.util.Calendar c1,
java.util.Calendar c2,
int field,
boolean round)
Calendars in intervals defined by
field.
The compare of the calendars follows normal conventions, so if c1 > c2 you will get a negative number. If rounding is on the level below is measured and normal rounding rules apply.
The field value should be one of Calendar.YEAR,Calendar.MONTH,Calendar.DATE
c1 - Calendar one (required)c2 - Calendar two (required)field - int, should be in (Calendar.YEAR|Calendar.MONTH|Calendar.DATE)round - boolean, true if the should difference be rounded according to the normal rounding rules.False will floor the valueCalendars in the defined measurepublic static final java.util.Date min(java.util.Date left,
java.util.Date right)
If one of the dates is null the other one will be returned
left - Dateright - Datepublic static final java.util.Date max(java.util.Date left,
java.util.Date right)
If one of the dates is null the other one will be returned
left - Dateright - Date