Package org.cyclos.server.utils
Class DateHelper
- java.lang.Object
-
- org.cyclos.server.utils.DateHelper
-
public class DateHelper extends Object
Helper class for dates
-
-
Field Summary
Fields Modifier and Type Field Description static intSECONDS_PER_DAYstatic intSECONDS_PER_HOURstatic intSECONDS_PER_MINUTE
-
Constructor Summary
Constructors Constructor Description DateHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Dateadd(Date date, ITimeInterval interval)Adds the given interval to a date.static Dateadd(Date date, TimeField field, int amount)Adds the given amount of time to the given date.static DateaddDecimal(Date date, TimeField field, double amount)As {@link add(Date, TimeField, int}, but accepts a double as amount param.static DateapplyOffset(Date date, TimeZone clientTZ, boolean toServer)Applies an offset shift to the given date, according to the difference between the client and the server time zones.static doubleconvertField(TimeField toField, int amount, TimeField fromField)Converts the given time interval into another time fieldstatic Pair<Date,Date>createPeriod(PredefinedPeriodData input, TimeZone timeZone)Creates a period from the parameters.static List<PredefinedPeriodData>createPeriodRange(PredefinedPeriodData input, int number)creates a list of periods as a range.static doubledaysBetween(Date date1, Date date2)Returns the number of days between 2 dates.static Dateearliest(Date... dates)a null proof method returning the earliest of any number of Datesstatic StringformatDateTime(DateTime dateTime, String format)Formats the given date time on the given formatstatic StringformatDuration(Long time)Formats a duration (expressed in milliseconds) in a human-readable format.static intgetCalendarField(TimeField field)static StringgetFormattedGMT(int offset)Returns the GMT formatted string for the given time zone offsetstatic StringgetFormattedGMT(TimeZone timezone, Date date)Returns the GMT formatted string for the given time zone and date.static PredefinedPeriodDatagetLastCompletedPeriod(PeriodType periodType, TimeZone timeZone)gets the last completed period as a PredefinedPeriodData object, also with the year specified.static PredefinedPeriodDatagetLastMonthsRange(int months, TimeZone timeZone)Gets a month range as a PredefinedPeriodData objectstatic intgetOffset(Date time, TimeZone timeZone)Returns the time zone offset for the given time zone, at the given date, in relation to the server default time zonestatic PredefinedPeriodDatagetPresentMonth(TimeZone timeZone)gets the present year as a PredefinedPeriodData objectstatic PredefinedPeriodDatagetPresentYear(TimeZone timeZone)gets the present year as a PredefinedPeriodData objectstatic TemporalUnitgetTemporalUnit(TimeField field)static PredefinedPeriodDatagetThisWeek(TimeZone timeZone)gets the present week as a PredefinedPeriodData objectstatic longgetTime(TimeIntervalDTO timeInterval)static PredefinedPeriodDatagetToday(TimeZone timeZone)gets the present day as a PredefinedPeriodData objectstatic booleanincludes(Date begin, Date end, Date date, boolean beginAndEndNullMeansInfinite)Returns whether a period from begin to end includes the given date.static booleanisLessThanOneDayGap(Date date1, Date date2)Returns true if the difference between the two dates is less than 24 hours.static Datelatest(Date... dates)a null proof method returning the latest of any number of Datesstatic longmillisIn(TimeField field, int amount)static Datenow()Returns a Date representing the current timestatic DatePeriodDTOperiod(DateTime begin, DateTime end)Creates a new period between the given datesstatic DatePeriodDTOperiodBeginningAt(DateTime date)Creates a new period beginning at the given datestatic Pair<Date,Date>periodEndingAt(ITimeInterval interval, Date date)Returns a period ending at the given date (not including it), with this sizestatic DatePeriodDTOperiodEndingAt(DateTime date)Creates a new period ending at the given datestatic Pair<Date,Date>periodIncluding(ITimeInterval interval, Date date)Returns a full period with length according to the given interval, which includes the given date.static Pair<Date,Date>periodStartingAt(ITimeInterval interval, Date date)Returns a period starting at the given date, with this sizestatic Pair<Date,Date>previousPeriod(ITimeInterval interval, Date date)Returns the previous full period that does not include the given date.static booleansameDay(TimeZone clientTZ, Date date1, Date date2)returns true if on the same day, false if otherwise.static doublesecondsSince(long startTime)Returns the number of seconds elapsed since the given timestatic DateshiftToBegin(Date serverDate, TimeZone clientTZ)Shifts the given date to the day begin, according to the given client time zone (assumes the server time zone if null).static DateshiftToEnd(Date serverDate, TimeZone clientTZ)Shifts the given date to the day end, according to the given client time zone (assumes the server time zone if null).static DateshiftToNextDay(Date serverDate, TimeZone clientTZ)Equivalent toshiftToEnd(Date, TimeZone)+ 1 MILLISECONDstatic Datesubtract(Date date, ITimeInterval interval)Subtracts the given interval from a date.static Datesubtract(Date date, TimeField field, int amount)Subtracts the given amount of time from a date.static inttoCalendarField(TimeField field)static ZonedDateTimetoZonedDateTime(Date date, TimeZone tz)Returns theZonedDateTimefor the specified date on the given time zone
-
-
-
Field Detail
-
SECONDS_PER_MINUTE
public static final int SECONDS_PER_MINUTE
- See Also:
- Constant Field Values
-
SECONDS_PER_HOUR
public static final int SECONDS_PER_HOUR
- See Also:
- Constant Field Values
-
SECONDS_PER_DAY
public static final int SECONDS_PER_DAY
- See Also:
- Constant Field Values
-
-
Method Detail
-
add
public static Date add(Date date, ITimeInterval interval)
Adds the given interval to a date. If the interval (or either it's amount or field) is null, returns the date as is.
Note that the result may be inaccurate for times - seeadd(Date, TimeField, int).
-
add
public static Date add(Date date, TimeField field, int amount)
Adds the given amount of time to the given date.Note that this method is NOT accurate. It simply adds the timefield via Calendar. For example, in case of a Daylight savings time shift, if the original date is in summertime and the end date in winter time, the one hour time shift is NOT applied if you add a certain number of days.
For exact calculations, useaddDecimal(java.util.Date, org.cyclos.model.utils.TimeField, double).
-
addDecimal
public static Date addDecimal(Date date, TimeField field, double amount)
As {@link add(Date, TimeField, int}, but accepts a double as amount param. Is also more precise, as this method does handle daylight saving time shifts correctly.
-
applyOffset
public static Date applyOffset(Date date, TimeZone clientTZ, boolean toServer)
Applies an offset shift to the given date, according to the difference between the client and the server time zones.- Parameters:
date- The date for calculation. When null, returns null.clientTZ- The client time zone. When null, returns the date itselftoServer- If true, considers the date to be a client-side date to be transformed to server date. If false, is the other way arround.
-
convertField
public static double convertField(TimeField toField, int amount, TimeField fromField)
Converts the given time interval into another time field
-
createPeriod
public static Pair<Date,Date> createPeriod(PredefinedPeriodData input, TimeZone timeZone)
Creates a period from the parameters. For example:createPeriod(2012, PeriodType.Quarter, 1)creates a period for the first quarter of 2012.- Parameters:
input- a PredefinedPeriodData which is a wrapper around the needed input parameters. The following must be set on this object:- year the year in which the period is.
- type the type of the period according to PeriodType.
- ordinal the ordinal number of that periodType in the year. The following cases for PeriodType apply:
- YEAR: the ordinal number is ignored, and a period for the complete year will be created.
- QUARTER: the ordinal numbers 1 to 4 will create a period for the quarters 1, 2, 3 or 4 for the given year
- MONTH: analogous, but with months. Ranges from 0 to 11, so 0 corresponds with January
- WEEK: the ordinal number ranges from 1 to 53, and it returns a period around this weeknumber in that year. The weeknumbers are defined according to the local Java.util.Calendar-rules concerning Java.util.Calendar.WEEK_OF_YEAR
timeZone- the timeZone of the client.- Returns:
- null if the input is null, or year or type are null. Otherwise, a
Pairofjava.util.Dates is returned, where the first element is the begin, and the second the end of the period.
-
createPeriodRange
public static List<PredefinedPeriodData> createPeriodRange(PredefinedPeriodData input, int number)
creates a list of periods as a range. The first element in the list is the period created with#createPeriod(PredefinedPeriodData)followed bynumberconsecutive elements. Example:createPeriodRange(input, 3)with input havingyear = 2012,type = PeriodType.MONTHandordinal = 0, creates a list with three periods, being january, february and march 2012.- Parameters:
input-number- the number of elements created. A negative number creates a range going back in time; a positive number creates a range going forth in time.- Returns:
- a
ListwithPredefinedPeriodData. - See Also:
#createPeriod(PredefinedPeriodData)
-
daysBetween
public static double daysBetween(Date date1, Date date2)
Returns the number of days between 2 dates. Will be negative if date1 > date2, 0 if both are the same day or positive otherwise
-
earliest
public static Date earliest(Date... dates)
a null proof method returning the earliest of any number of Dates- Parameters:
dates- any null arguments are ignored. If all arguments are null just returns null.- Returns:
- the earliest date of the arguments.
-
formatDateTime
public static String formatDateTime(DateTime dateTime, String format)
Formats the given date time on the given format
-
formatDuration
public static String formatDuration(Long time)
Formats a duration (expressed in milliseconds) in a human-readable format. If less than a second, returns 0ms. If less than a minute, 0s. If less than an hour, returns 0:00. If larger, 0:00:00. For nulls, returns an empty string.
-
getCalendarField
public static int getCalendarField(TimeField field)
-
getFormattedGMT
public static String getFormattedGMT(int offset)
Returns the GMT formatted string for the given time zone offset
-
getFormattedGMT
public static String getFormattedGMT(TimeZone timezone, Date date)
Returns the GMT formatted string for the given time zone and date.
-
getLastCompletedPeriod
public static PredefinedPeriodData getLastCompletedPeriod(PeriodType periodType, TimeZone timeZone)
gets the last completed period as a PredefinedPeriodData object, also with the year specified. For example:getLastCompletePeriod(PeriodType.QUARTER)gives2012 quarter IIwhen the present time is in the third quarter of 2012
-
getLastMonthsRange
public static PredefinedPeriodData getLastMonthsRange(int months, TimeZone timeZone)
Gets a month range as a PredefinedPeriodData object
-
getOffset
public static int getOffset(Date time, TimeZone timeZone)
Returns the time zone offset for the given time zone, at the given date, in relation to the server default time zone
-
getPresentMonth
public static PredefinedPeriodData getPresentMonth(TimeZone timeZone)
gets the present year as a PredefinedPeriodData object
-
getPresentYear
public static PredefinedPeriodData getPresentYear(TimeZone timeZone)
gets the present year as a PredefinedPeriodData object
-
getTemporalUnit
public static TemporalUnit getTemporalUnit(TimeField field)
-
getThisWeek
public static PredefinedPeriodData getThisWeek(TimeZone timeZone)
gets the present week as a PredefinedPeriodData object
-
getTime
public static long getTime(TimeIntervalDTO timeInterval)
-
getToday
public static PredefinedPeriodData getToday(TimeZone timeZone)
gets the present day as a PredefinedPeriodData object
-
includes
public static boolean includes(Date begin, Date end, Date date, boolean beginAndEndNullMeansInfinite)
Returns whether a period from begin to end includes the given date. IfbeginAndEndNullMeansInfiniteis true, the result is always true. Otherwise, if both begin and end are null, will return false. In either case, if begin is null and end is not null, checks only the end. The same for begin.
-
isLessThanOneDayGap
public static boolean isLessThanOneDayGap(Date date1, Date date2)
Returns true if the difference between the two dates is less than 24 hours.
-
latest
public static Date latest(Date... dates)
a null proof method returning the latest of any number of Dates- Parameters:
dates- any null arguments are ignored. If all arguments are null just returns null.- Returns:
- the latest date of the arguments.
-
millisIn
public static long millisIn(TimeField field, int amount)
-
now
public static Date now()
Returns a Date representing the current time
-
period
public static DatePeriodDTO period(DateTime begin, DateTime end)
Creates a new period between the given dates
-
periodBeginningAt
public static DatePeriodDTO periodBeginningAt(DateTime date)
Creates a new period beginning at the given date
-
periodEndingAt
public static DatePeriodDTO periodEndingAt(DateTime date)
Creates a new period ending at the given date
-
periodEndingAt
public static Pair<Date,Date> periodEndingAt(ITimeInterval interval, Date date)
Returns a period ending at the given date (not including it), with this size
-
periodIncluding
public static Pair<Date,Date> periodIncluding(ITimeInterval interval, Date date)
Returns a full period with length according to the given interval, which includes the given date. For example, if the interval is 2 months, and the date is 2014-01-17, the return will be 2014-01-01 to 2014-02-28. All dates are expected to be on the server default time zone
-
periodStartingAt
public static Pair<Date,Date> periodStartingAt(ITimeInterval interval, Date date)
Returns a period starting at the given date, with this size
-
previousPeriod
public static Pair<Date,Date> previousPeriod(ITimeInterval interval, Date date)
Returns the previous full period that does not include the given date. Example: date = 2014-02-15, field = month, number = 2 returns a period of 2 months from 2013-12-01 to 2014-01-31. All dates are expected to be on the server default time zone
-
sameDay
public static boolean sameDay(TimeZone clientTZ, Date date1, Date date2)
returns true if on the same day, false if otherwise.
-
secondsSince
public static double secondsSince(long startTime)
Returns the number of seconds elapsed since the given time
-
shiftToBegin
public static Date shiftToBegin(Date serverDate, TimeZone clientTZ)
Shifts the given date to the day begin, according to the given client time zone (assumes the server time zone if null). The date is assumed to represent the server time zone. For example: If the server is +3h from the client, and the given date is 2000-01-01 01:30:00, it will shifted to 1999-12-31 22:30:00 (per time zone offset), as that's time point for the client, and represents a day ago. Then, it will be shifted to 1999-12-31 00:00:00, representing the beginning of that day for the client. Finally, the date will be shifted again to the server offset, and the result will be 1999-12-31 03:00:00 (using the server time zone). Handles null, returning null.
-
shiftToEnd
public static Date shiftToEnd(Date serverDate, TimeZone clientTZ)
Shifts the given date to the day end, according to the given client time zone (assumes the server time zone if null). The date is assumed to represent the server time zone. For example: If the server is +3h from the client, and the given date is 2000-01-01 12:30:00, it will shifted to 2000-01-01 09:30:00 (per time zone offset), as that's time point for the client. Then, it will be shifted to 2000-01-01 23:59:59, representing the end of that day for the client. Finally, the date will be shifted again to the server offset, and the result will be 2000-01-02 02:59:59 (using the server time zone). Handles null, returning null.
-
shiftToNextDay
public static Date shiftToNextDay(Date serverDate, TimeZone clientTZ)
Equivalent toshiftToEnd(Date, TimeZone)+ 1 MILLISECOND
-
subtract
public static Date subtract(Date date, ITimeInterval interval)
Subtracts the given interval from a date. If the interval (or either it's amount or field) is null, returns the date as is.Note that the result may be inaccurate for times - see
add(Date, TimeField, int).
-
subtract
public static Date subtract(Date date, TimeField field, int amount)
Subtracts the given amount of time from a date.Note that the result may be inaccurate for times - see
add(Date, TimeField, int).
-
toCalendarField
public static int toCalendarField(TimeField field)
-
toZonedDateTime
public static ZonedDateTime toZonedDateTime(Date date, TimeZone tz)
Returns theZonedDateTimefor the specified date on the given time zone
-
-