Package com.groiss.cal
Class CalUtil
java.lang.Object
com.groiss.cal.CalUtil
This class provides some utilities for date and time handling.
It uses two properties: 'DateFormat' and 'DateTimeFormat' which must
be set in the default configuration object.
The integer constants in this class can be used with the method getDateFormatter
to get a thread save DateFormat instance for this format.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
ANSI C's asctime() format: EEE MMM d HH:mm:ss yyyystatic final int
Date format as set in the configurationstatic final int
Date and time format as set in the configurationstatic final int
date format : yyyy-MM-dd HH:mm:ssstatic final int
date format : yyyyMMddHHmmssstatic final int
DATE_TIME
without zone infostatic final int
static final int
static final int
Date format ISO 8601: yyyy-MM-dd'T'HH:mm:ss'Z'static final int
Date format for log output: yyyy-MM-dd HH:mm:ss.SSSstatic final long
Number of milliseconds per daystatic final int
static final Pattern
static final int
Date format RFC1036: EEEEEEEEE, dd-MMM-yy HH:mm:ss zstatic final int
Date format RFC1123: EEE, dd MMM yyyy HH:mm:ss zstatic final int
TimeInterval Reporing & Time Managmentstatic final String[]
short and normal names for TimeUnitsstatic final long[]
the Unit Factorysstatic final String[]
static final int
static final int
xml date format: yyyy-MM-ddstatic final int
xml date format: yyyy-MM-dd'Z'static final long
the time value of the date object of date January 1, 1000 -
Method Summary
Modifier and TypeMethodDescriptionstatic Date
addWorkdays
(Date d1, int days) adds the specified amount of workdays to the given datestatic boolean
static int
daysbetween
(Calendar start, Date endDate, boolean removeEarlyEnd) Days between start and end date, including start.static void
deleteTimeInfoUntil
(Calendar cal, int field) Set the time information to 0 starting at the given field, for example hourstatic int
easterDay
(int year) calculates the day of the easter-sunday for the given yearstatic Calendar
static Calendar
getCalendar
(Locale loc) static Calendar
getCalendar
(Locale loc, TimeZone tz) Returns a calendar instance.static Calendar
getCalendar
(TimeZone tz) static DateFormatSymbols
Returns aDateFormatSymbols
instance for the currentThreadContext.getThreadLocale()
static DateFormatSymbols
getDateFormatSymbols
(Calendar calendar, Locale loc) Returns aDateFormatSymbols
instance for the specifiedLocale
andCalendar
ifCalendar
isnull
getCalendar()
is used
ifLocale
isnull
Locale.getDefault()
is usedstatic DateFormatSymbols
static SimpleDateFormat
getDateFormatter
(int type) Returns a thread save DateFormat object for the given format.static SimpleDateFormat
getDateFormatter
(int type, Locale loc) Get local specific instance of predefined formatter typesstatic SimpleDateFormat
getDateFormatter
(int type, Locale locale, TimeZone timezone) get the localizedSimpleDateFormat
with the pattern specified bytype
if the giventype
has a fixedlocale
/timezone
the givenlocale
/timezone
is ignoredstatic SimpleDateFormat
getDateFormatter
(int type, Locale locale, TimeZone timezone, Calendar cal) static SimpleDateFormat
getDateFormatter
(int type, TimeZone tz) static SimpleDateFormat
getDateFormatter
(String pattern) static SimpleDateFormat
getDateFormatter
(String pattern, Locale loc) static SimpleDateFormat
getDateFormatter
(String pattern, Locale locale, TimeZone timezone) get aSimpleDateFormat
instance for the pattern/locale/timezonestatic SimpleDateFormat
getDateFormatter
(String pattern, TimeZone tz) static String
getIntervalInUnits
(long interval) Format a time interval.static String
getIntervalInUnits
(long interval, int maxunit) static String
getIntervalInUnits
(long interval, int maxunit, int minunit) static String
getIntervalInUnits
(long interval, int maxunit, int minunit, String[] unitNames) static String
getIntervalInUnits
(long interval, int maxunit, String[] unitNames) Returns a String representation of a TimeIntervallstatic String
getIntervalInUnits
(long interval, String maxunit) Returns a String representation of a TimeIntervall using the short names of timeunitsstatic String
getIntervalInUnits
(long interval, String maxunit, String minunit) static Calendar
getJavaCalendar
(Calendar icuCalendar) static int
getUnitIntFromString
(String unit) static boolean
returns true if the passed date is not before start and not after end.static String
Returns null or the name of the holiday for the given daystatic boolean
isNonWorkingDay
(int day_of_week) this method checky configured workind days of week only.static boolean
checks if a given date is defined as working daystatic Date
parse a date from a string with the specified formatstatic Date
Parse a date from a string.static Date
Parse a date from a string using the specified format.static Date
Parses the date sets the time to 23:59:59:999.static Duration
parseDuration
(String duration, String key) Parses a time string to a java.time.Duration Objectstatic TimeZone
static void
static void
setDateFormatter
(int type, SimpleDateFormat df) static String
static String
static String
Show the date using the default formatter.static String
static String
static String
Show the date and time using the default formatter.static String
Show the date and time in the form yyyy-MM-dd HH:mm:ss.static String
showDuration
(Object obj) Show the duration in format *d*h*m*sstatic int
thisYear()
Return the current year.static Date
toBeginDay
(Date date) returns the date set to 00:00:00:00static Date
returns a date set to 23:59:59:999static int
workDaysBetween
(Date d1, Date d2) Compute the working days between the two argument date
-
Field Details
-
DATE
public static final int DATEDate format as set in the configuration- See Also:
-
DATE_TIME
public static final int DATE_TIMEDate and time format as set in the configuration- See Also:
-
RFC1123
public static final int RFC1123Date format RFC1123: EEE, dd MMM yyyy HH:mm:ss z- See Also:
-
RFC1036
public static final int RFC1036Date format RFC1036: EEEEEEEEE, dd-MMM-yy HH:mm:ss z- See Also:
-
ANSI_C_ASCTIME
public static final int ANSI_C_ASCTIMEANSI C's asctime() format: EEE MMM d HH:mm:ss yyyy- See Also:
-
LOG_DATE
public static final int LOG_DATEDate format for log output: yyyy-MM-dd HH:mm:ss.SSS- See Also:
-
ISO
public static final int ISODate format ISO 8601: yyyy-MM-dd'T'HH:mm:ss'Z'- See Also:
-
XML_DATE
public static final int XML_DATExml date format: yyyy-MM-dd- See Also:
-
XML_DATE_UTC
public static final int XML_DATE_UTCxml date format: yyyy-MM-dd'Z'- See Also:
-
DATE_TIME_SEC
public static final int DATE_TIME_SECdate format : yyyy-MM-dd HH:mm:ss- See Also:
-
DATE_TIME_SEC_DIGITS
public static final int DATE_TIME_SEC_DIGITSdate format : yyyyMMddHHmmss- See Also:
-
DATE_TIME_WITHOUT_ZONE
public static final int DATE_TIME_WITHOUT_ZONEDATE_TIME
without zone info- See Also:
-
YEAR_THOUSAND
public static final long YEAR_THOUSANDthe time value of the date object of date January 1, 1000- See Also:
-
MILLISPERDAY
public static final long MILLISPERDAYNumber of milliseconds per day- See Also:
-
NUMBER_CHECK_PATTERN
-
SECONDS
public static final int SECONDSTimeInterval Reporing & Time Managment- See Also:
-
MINUTES
public static final int MINUTES- See Also:
-
HOURS
public static final int HOURS- See Also:
-
DAYS
public static final int DAYS- See Also:
-
WEEK
public static final int WEEK- See Also:
-
SHORT_UNIT_NAMES
short and normal names for TimeUnits -
UNIT_NAMES
-
UNIT_FACTORS
public static final long[] UNIT_FACTORSthe Unit Factorys
-
-
Method Details
-
addWorkdays
adds the specified amount of workdays to the given date- Parameters:
d1
-days
-- Returns:
- the new date
-
areHolidaysConfigured
public static boolean areHolidaysConfigured()- Returns:
true
if the special class was configured for holidays; otherwise returnsfalse
-
deleteTimeInfoUntil
Set the time information to 0 starting at the given field, for example hour- Parameters:
cal
-field
-
-
easterDay
public static int easterDay(int year) calculates the day of the easter-sunday for the given year- Parameters:
year
-- Returns:
- the easter-sunday for the given year
-
getCalendar
- Returns:
- a calendar instance with
ThreadContext.getThreadLocale()
andThreadContext.getThreadTimeZone()
- See Also:
-
getCalendar
- Returns:
- a calendar instance
- See Also:
-
getCalendar
- Returns:
- a calendar instance
- See Also:
-
getCalendar
Returns a calendar instance. The calendar class can be set using the configuration parameter "calendar.class". Default is the GregorianCalendar.- Parameters:
loc
- the locale if locale isnull
,Locale.getDefault()
is usedtz
- the TimeZone if timezone isnull
,TimeZone.getDefault()
is used- Returns:
- a calendar instance
-
getJavaCalendar
-
getDateFormatSymbols
Returns aDateFormatSymbols
instance for the currentThreadContext.getThreadLocale()
- Returns:
- a DateFormatSymbols instance
- See Also:
-
getDateFormatSymbols
Returns aDateFormatSymbols
instance for the specifiedLocale
andCalendar
ifCalendar
isnull
getCalendar()
is used
ifLocale
isnull
Locale.getDefault()
is used- Parameters:
calendar
- the calendarloc
- the locale- Returns:
- a DateFormatSymbols instance
-
getDateFormatSymbols
- Parameters:
loc
- the locale- Returns:
- a DateFormatSymbols instance
- See Also:
-
getDateFormatter
Returns a thread save DateFormat object for the given format.- Parameters:
type
- is one of the integer constants of this class (DATE, RFC1123, ...)- Returns:
- a DateFormat instance
-
getDateFormatter
Get local specific instance of predefined formatter types- Parameters:
type
-loc
-- Returns:
- local specific instance of predefined formatter types
-
getDateFormatter
get the localizedSimpleDateFormat
with the pattern specified bytype
if the giventype
has a fixedlocale
/timezone
the givenlocale
/timezone
is ignored- Parameters:
type
- one ofDATE
,DATE_TIME
,DATE_TIME_SEC
,DATE_TIME_WITHOUT_ZONE
,DATE_TIME_SEC_DIGITS
locale
- the localetimezone
- the timezone- Returns:
- the
DateFormat
-
getDateFormatter
public static SimpleDateFormat getDateFormatter(int type, Locale locale, TimeZone timezone, Calendar cal) - Parameters:
type
- one ofDATE
,DATE_TIME
,DATE_TIME_SEC
,DATE_TIME_WITHOUT_ZONE
,DATE_TIME_SEC_DIGITS
locale
- the localetimezone
- the timezonecal
- the calendar to be used- Returns:
- the
DateFormat
-
getDateFormatter
- Parameters:
type
-tz
-- Returns:
- the date formatter
- See Also:
-
getDateFormatter
- Parameters:
pattern
-- Returns:
- the date formatter
- See Also:
-
getDateFormatter
- Parameters:
pattern
-loc
-- Returns:
- the date formatter
- See Also:
-
getDateFormatter
get aSimpleDateFormat
instance for the pattern/locale/timezone- Parameters:
pattern
-locale
-timezone
-- Returns:
- the date formatter
-
getDateFormatter
- Parameters:
pattern
-tz
-- Returns:
- the date formatter
- See Also:
-
getIntervalInUnits
Format a time interval.- Parameters:
interval
- duration in milliseconds- Returns:
-
getIntervalInUnits
-
getIntervalInUnits
-
getIntervalInUnits
-
getIntervalInUnits
Returns a String representation of a TimeIntervall- Parameters:
interval
- the intervall in millisecondesmaxunit
-unitNames
- - The UntiNames to use for the string representation- Returns:
- a String representation of this Intervals, embedded in a DIV block with textalign right.
-
getIntervalInUnits
Returns a String representation of a TimeIntervall using the short names of timeunits- Parameters:
interval
- the intervall in millisecondesmaxunit
- - the unis as string (maybe short or normal name)- Returns:
- a String representation of this Intervals, embedded in a DIV block with textalign right.
-
getIntervalInUnits
-
getUnitIntFromString
-
isHoliday
Returns null or the name of the holiday for the given day- Returns:
- a holiday name or null
-
isNonWorkingDay
public static boolean isNonWorkingDay(int day_of_week) this method checky configured workind days of week only. It doesnt check local holydays -
isWorkingDay
checks if a given date is defined as working day- Parameters:
c
- the given date- Returns:
- true if date is a configured working day
-
parseDate
parse a date from a string with the specified format- Parameters:
type
- the specified format to apply on the passed stringdateString
- the string representing a date which shall be parsed to a date object- Returns:
Date
instance
-
parseDate
Parse a date from a string using the specified format.- Parameters:
dateString
- the string representing a date which shall be parsed to a date objecttype
- the specified formats to apply on the passed string- Returns:
Date
instance
-
parseDate
Parse a date from a string.- Parameters:
s
- a string representing a date or date + time- Returns:
- a date object
-
parseDateEndDay
Parses the date sets the time to 23:59:59:999. This is useful for intervals. if the date is passed as date+time, the second/minute is also rounded to the next full second/minute- Returns:
- a date object
-
parseTimeZone
-
reconfigure
public static void reconfigure() -
setDateFormatter
-
showDate
-
showDate
-
showDate
Show the date using the default formatter.- Parameters:
d
- the date object
-
showDate
-
showDate
-
showDateTime
Show the date and time using the default formatter.- Parameters:
d
- the date object
-
showDateTimeSeconds
Show the date and time in the form yyyy-MM-dd HH:mm:ss.- Parameters:
d
- the date object
-
parseDuration
Parses a time string to a java.time.Duration Object- Parameters:
duration
- Can be a time string in format [dD][hH][mM][s[.f]S] or just secondskey
- The key of the property- Returns:
- A Duration in ISO-8601 format
-
showDuration
Show the duration in format *d*h*m*s -
thisYear
public static int thisYear()Return the current year.- Returns:
- the current year as integer.
-
toBeginDay
returns the date set to 00:00:00:00- Parameters:
date
-- Returns:
- the date set to 00:00:00:00
-
toEndDay
returns a date set to 23:59:59:999- Parameters:
date
-- Returns:
- the date set to 23:59:59:999
-
workDaysBetween
Compute the working days between the two argument date- Parameters:
d1
- the start dated2
- the end date- Returns:
- the days between, excluding holidays, saturdays, and sundays, including the start and the end day.
-
isBetween
returns true if the passed date is not before start and not after end. If start is null the passed date is considered as not before start. If end is null the passed date is considered as not after end. -
daysbetween
Days between start and end date, including start.- Parameters:
start
- the FROM dateendDate
- the TO dateremoveEarlyEnd
- end date is only included if hour>0- Returns:
- the days
-