Package io.higson.runtime.rhino
Class RhinoDate
java.lang.Object
io.higson.runtime.rhino.RhinoDate
This is utility class for working with dates that are supported in Higson.
- Author:
- przemek hertel
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic DateAdds amount to 'day' field that is based on given date.static DateAdds amount to 'day' field that is based on given date.static DateAdds amount to 'day' field that is based on given date.static DateAdds amount to 'hour' field that is based on given date.static DateAdds amount to 'hour' field that is based on given date.static DateAdds amount to 'hour' field that is based on given date.static DateAdds amount to 'minute' field that is based on given date.
1) If amount> 0, it will add minutes,
2) If amount< 0, it will subtract minutes from date.
When given date is: String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats, instance of DateHolder - it will try to retrieve date from holder, instance of other type - it will try to convert date to proper ValueHolder and then retrieve date. This method doesn't modify input date, but creates new instance of date type.static DateAdds amount to 'minute' field that is based on given date.static DateAdds amount to 'minute' field that is based on given date.
1) If amount> 0, it will add minutes,
2) If amount< 0, it will subtract minutes from date.
This method doesn't modify input date, but creates new instance of date type.static DateAdds amount to 'month' field that is based on given date.static DateAdds amount to 'month' field that is based on given date.static DateAdds amount to 'month' field that is based on given date.static DateaddMonthOracle(Object date, int amount) Adds amount to 'month' field that is based on given date, but if day is last day of a given month, then moves day to the last possible day of a calculated month.
1) If amount> 0, it will add months,
2) If amount< 0, it will subtract months from date.
When given date is: String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats, instance of DateHolder od NumberHolder - it will try to retrieve date from holder, instance of other type - it will try to convert date to proper ValueHolder and then retrieve date. This method doesn't modify input date, but creates new instance of date type.static DateaddMonthOracle(Object date, Object amount) Adds amount to 'month' field that is based on given date, but if day is last day of a given month, then moves day to the last possible day of a calculated month.
1) If amount> 0, it will add months,
2) If amount< 0, it will subtract months from date.
When given date is: String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats, instance of DateHolder od NumberHolder - it will try to retrieve date from holder, instance of other type - it will try to convert date to proper ValueHolder and then retrieve date. When given amount is: int type - If amount is positive it will add months, but if negative it will subtract months from date, instance of Number - it will try to retrieve int from number, instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer. This method doesn't modify input date, but creates new instance of date type.static DateaddMonthOracle(Date date, int amount) Adds amount to 'month' field that is based on given date, but if day is last day of a given month, then moves day to the last possible day of a calculated month.
1) If amount> 0, it will add months,
2) If amount< 0, it will subtract months from date.
This method doesn't modify input date, but creates new instance of date type.static DateAdds amount to 'second' field that is based on given date.
1) If amount> 0, it will add seconds,
2) If amount< 0, it will subtract seconds from date.
When given date is: String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats, instance of DateHolder - it will try to retrieve date from holder, instance of other type - it will try to convert date to proper ValueHolder and then retrieve date. This method doesn't modify input date, but creates new instance of date type.static DateAdds amount to 'second' field that is based on given date.
1) If amount> 0, it will add seconds,
2) If amount< 0, it will subtract seconds from date.
When given date is: String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats, instance of DateHolder - it will try to retrieve date from holder, instance of other type - it will try to convert date to proper ValueHolder and then retrieve date. When given amount is: int type - If amount is positive it will add seconds, but if negative it will subtract seconds from date, instance of Number - it will try to retrieve int from number, instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer. This method doesn't modify input date, but creates new instance of date type.static DateAdds amount to 'second' field that is based on given date.
1) If amount> 0, it will add seconds,
2) If amount< 0, it will subtract seconds from date.
This method doesn't modify input date, but creates new instance of date type.static DateAdds amount to 'year' field that is based on given date.static DateAdds amount to 'year' field that is based on given date.static DateAdds amount to 'year' field that is based on given date.static intcompareYMD(Date date1, Date date2) Compares two dates by year, month, date.static DateCreates copy of given date.static Datecurrent()Deprecated.static DateReturn current date with time set to 00:00:00.static DateReturn current date with time.static StringReturns String from date using yyyy-MM-dd format.static StringReturns String in given pattern from date.static StringReturns String in dd-MM-yyyy pattern from date.static StringformatTimestamp(long timestamp) Returns String from timestamp as long using yyyy-MM-dd HH:mm:ss.SSS format.static StringformatTimestamp(Date date) Returns String from date using yyyy-MM-dd HH:mm:ss.SSS format.static DatefromInt(int date) It will try to convert given date as int to proper date instance.static intgetAbsoluteYearDiff(Object date1, Object date2) Calculates full years intervals between date1 and date2.static DategetDate(int year, int month, int day) Creates new date from given parameters without time.static DategetDate(int year, int month, int day, int hour, int minute) Creates new date from given parameters.static DategetDate(int year, int month, int day, int hour, int minute, int second) Creates new date from given parameters.static intGet day from date, but date must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.static intgetDayDiff(Object date1, Object date2) Returns difference of days between givens dates.static intgetDayDiff(Object date1, Object date2, String pattern) Returns difference of days between givens dates.static intgetDayDiffDeprecated(Object date1, Object date2) Deprecated.will be removed in future, usegetDayDiff(java.lang.Object, java.lang.Object)static intgetDaysBetween(Date date1, Date date2) Calculates full days intervals between date1 and date2.static intgetDaysInYear(int year) Number of days in a given year.static DategetFirstDayOfMonth(Date date) Create new date based on given date, but set to first day of a month.static DategetFirstDayOfYear(int year) Create date with a beginning for a given year.static DategetFirstDayOfYear(Date date) Create new date based on given date, but set to first day of a year.static DategetFirstWorkingDayOfYear(int year) Create date, that is set to the first work day of beginning of given year.static intgetFullYearDiff(Date date1, Date date2) The same asgetAbsoluteYearDiff(Object, Object), but it is not null safe.static intGet hour from date, but date must be in yyyy-MM-dd HH:mm:ss format.static intgetHourDiff(Object date1, Object date2) Deprecated.Will be removed in the futurestatic DategetLastDayOfMonth(Date date) Create new date based on given date, but set to last day of a month.static DategetLastDayOfYear(Date date) Create new date based on given date, but set to last day of a year.static intGet minutes from date, but date must be in yyyy-MM-dd HH:mm:ss format.static intgetMinuteDiff(Object date1, Object date2) Deprecated.Will be removed in the futurestatic intGet month from date, but date must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.static intgetMonthDiff(Object date1, Object date2) Returns difference of months between givens dates.static intgetMonthDiffDeprecated(Object date1, Object date2) Deprecated.will be removed in future, usegetMonthDiff(java.lang.Object, java.lang.Object)static intgetMonthsBetween(Date date1, Date date2) Calculates full months intervals between date1 and date2.static DategetNearestWorkDay(Date date) Get closets work day from given date, but if given date is work date, it will return same instance.static DateGet next work day based on given date, but also verify polish holidays, and consider them during verification.static DategetNextWorkDay(Date date) Get next working date from given date.getPolishHolidays(int year) Returns set of polish holidays for given year, except these years that happen to occur during weekend.static intGet seconds from date, but date must be in yyyy-MM-dd HH:mm:ss format.static SimpleDateFormatgetSimpleDateFormat(String pattern) Returns instance of SimpleDateFormat that is currently used in active Thread.static DateDeprecated.Will be removed in the futurestatic intGet year from date, but date must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.static intgetYearDiff(Object date1, Object date2) Returns difference of years between givens dates.
Possible outcomes: negative result if date1> date2, positive result if date1< date2, zero if there is no difference or any of given dates are null.static intgetYearsBetween(Date date1, Date date2) Calculates full years intervals between date1 and date2.static booleanisFirstWorkDayOfMonth(Date date) Checks if given date is both a first day of a month and a work day.static booleanisInPeriod(long d, long d1, long d2) Works the same asisInPeriod(Date, Date, Date), but for longs.static booleanisInPeriod(Date dateToCheck, Date minDateInclusive, Date maxDateExclusive) Checks if given dateToCheck is within boundaries.static booleanisInPeriodInclusive(long d, long d1, long d2) Works the same asisInPeriodInclusive(Date, Date, Date), but for longs.static booleanisInPeriodInclusive(Date dateToCheck, Date minDateInclusive, Date maxDateInclusive) Checks if given dateToCheck is within boundaries.static booleanisLastDayOfMonth(Date date) Checks if given date is last date of a month.static booleanisLeapYear(int year) Verifies if given year is a leap year.static booleanChecks if given date is work date.static intjulianDay(int year, int month, int day) Read more about here: http://en.wikipedia.org/wiki/Julian_daystatic intRead more about here: http://en.wikipedia.org/wiki/Julian_daystatic intjulianDayDiff(Date date1, Date date2) Read more about here: http://en.wikipedia.org/wiki/Julian_day Difference between to julian days.static DateChecks which one of given dates is greater then the other.static DateChecks which one of given dates is greater then the others.static DateChecks which one of given dates is smaller then the other.static DateChecks which one of given dates is the earliest.static DateTries to parse given date with default pattern yyyy-MM-dd.static DateTries to parse given date for given object pattern.static DateTries to parse String for multiple supported formats and return date.static DateTries to parse String for given format, then return date.static DateTries to parse given String in dd-MM-yyyy format.static DateTries to parse given object in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.static DateTries to parse given date with default pattern yyyy-MM-dd.static DateTries to parse given date for given pattern String.static DateTries to parse given String in yyyy-MM-dd format.static DateAdds amount to the 'day' field without changing other fields.static DateAdds amount to the 'hour' field without changing other fields.static DaterollMinute(Object date, Object amount) Adds amount to the 'minute' field without changing other fields.static DateAdds amount to the 'month' field without changing other fields.static DaterollSecond(Object date, Object amount) Adds amount to the 'second' field without changing other fields.static DateAdds amount to the 'year' field without changing other fields.static DateSets day for given date.static DateSets hour for given date.static DateSets minute for given date.static DateSets month for given date.static DateSets second for given date.static DateSets year for given date.static DatesubtractYear(Date date) Deprecated.will be removed in the future, useaddYear(Date, int)with -1 amount.static intIt will try to convert given date to int representation.static DateClearing time part of a given date.
-
Field Details
-
INSTANCE
-
YMD
- See Also:
-
YMDHMS
- See Also:
-
YMDHMSMS
- See Also:
-
DMY
- See Also:
-
DMYHMS
- See Also:
-
DMYHMSMS
- See Also:
-
CAL_MONTHS_IN_YEAR
public static final int CAL_MONTHS_IN_YEAR- See Also:
-
CAL_MILLIS_IN_DAY
public static final int CAL_MILLIS_IN_DAY- See Also:
-
CAL_MILLIS_IN_HOUR
public static final int CAL_MILLIS_IN_HOUR- See Also:
-
CAL_MILLIS_IN_MINUTE
public static final int CAL_MILLIS_IN_MINUTE- See Also:
-
CAL_LEAP_YEAR_DAYS
public static final int CAL_LEAP_YEAR_DAYS- See Also:
-
-
Method Details
-
parse
Tries to parse String for multiple supported formats and return date. supported formats:- yyyy-MM-dd
- yyyy.MM.dd
- yyyy/MM/dd
- dd-MM-yyyy
- dd.MM.yyyy
- dd/MM/yyyy
Example:
DateType type = higson.getDateType(); assertThat(RhinoDate.parse("2018-10-29"), is(type.getDate("2018-10-29"));- Parameters:
dateStr- 10-characters date String- Returns:
- Date parsed from String
- Throws:
IllegalArgumentException- if Higson was not able to parse given String
-
parse
Tries to parse String for given format, then return date.Example:
DateType type = higson.getDateType(); assertThat(RhinoDate.parse("2018-10-29", "yyyy-MM-dd"), is(type.getDate("2018-10-29"));- Parameters:
dateStr- date as Stringpattern- that matches given dateStr- Returns:
- Date parsed from String
- Throws:
IllegalArgumentException- if Higson was not able to parse given String
-
current
Deprecated.usecurrentDatetime()methodReturn current date with time.Example:
Date date = RhinoDate.current();- Returns:
- current date with time
-
currentDate
Return current date with time set to 00:00:00.Example:
Date currentDate = RhinoDate.currentDate();- Returns:
- current date with time set to 00:00:00
-
currentDatetime
Return current date with time.Example:
Date date = RhinoDate.currentDatetime();- Returns:
- current date with time
-
getSimpleDateFormat
Returns instance of SimpleDateFormat that is currently used in active Thread. If there was no such pattern used, then new instance is created and stored in memory.Example:
SimpleDateFormat sdf = RhinoDate.getSimpleDateFormat("yyyy-MM-dd");- Parameters:
pattern- to be matched when looking for SimpleDateFormat instance- Returns:
- same instance of SimpleDateFormat if exists, or fetches new instance
- Throws:
IllegalArgumentException- if given pattern is invalid
-
parseDMY
Tries to parse given String in dd-MM-yyyy format.Example:
Date date = RhinoDate.parseDMY("29-10-2018");- Parameters:
dateString- date as String- Returns:
- current date with time
- Throws:
IllegalArgumentException- if Higson was not able to parse given String
-
parseYMD
Tries to parse given String in yyyy-MM-dd format.Example:
Date date = RhinoDate.parseDMY("2018-10-29");- Parameters:
dateString- date as String- Returns:
- current date with time
- Throws:
IllegalArgumentException- if Higson was not able to parse given String
-
parseLong
Tries to parse given object in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.Example:
Date date = RhinoDate.parseLong("2018-10-29"); // valid Date date = RhinoDate.parseLong("2018-10-29 12:45:66"); // valid Date input = RhinoDate.currentDate(); Date date = RhinoDate.parseLong(input); // valid- Parameters:
date- object to be parsed as date- Returns:
- parsed date or null if parsing was not successful
-
parse
Tries to parse given date with default pattern yyyy-MM-dd. If given date is different type than String, then toString() on this object is invoked.Example:
Date date = RhinoDate.parse("2018-10-29"); // valid Date input = RhinoDate.currentDate(); Date date = RhinoDate.parse(input); // valid- Parameters:
date- object to be parsed as date- Returns:
- parsed date or null if parsing was not successful
-
parse
Tries to parse given date for given object pattern. Object pattern might be type of DateFormat or simple String format. If given date is different type than String, then toString() on this object is invoked.Example:
Date date = RhinoDate.parse("2018-10-29", "yyyy-MM-dd"); // valid Date input = RhinoDate.currentDate(); Date date = date.parse(input, "yyyy-MM-dd"); // valid Date input = RhinoDate.currentDate(); CustomDateFormat cdf = ...; // custom date format implementation Date date = RhinoDate.parse(input, cdf); // valid- Parameters:
date- object to be parsed as datepattern- object to be used for parsing date- Returns:
- parsed date or null if parsing was not successful
- Throws:
IllegalArgumentException- if given pattern is invalid
-
parseNoEx
Tries to parse given date with default pattern yyyy-MM-dd.Example:
Date date = RhinoDate.parseNoEx("2018-10-29"); // valid Date date = RhinoDate.parseNoEx("29-10-2018"); // valid, but return null- Parameters:
date- as String to be parsed- Returns:
- parsed date or null if parsing was not successful
-
parseNoEx
Tries to parse given date for given pattern String.Example:
Date date = RhinoDate.parseNoEx("2018-10-29", "yyyy-MM-dd"); // valid Date date = RhinoDate.parseNoEx("2018-10-29", "dd-MM-yyyy"); // null- Parameters:
date- as String to be parsedpattern- used for parsing date String- Returns:
- parsed date or null if parsing was not successful
- Throws:
IllegalArgumentException- if given pattern is invalid
-
format
Returns String from date using yyyy-MM-dd format.Example:
Date date = higson.getDateType().decode("2018-10-29").getDate(); String dateAsString = RhinoDate.format(date); assertThat(dateAsString, is("2018-10-29"));- Parameters:
date- to be formatted as String- Returns:
- String from date using proper format
-
formatTimestamp
Returns String from date using yyyy-MM-dd HH:mm:ss.SSS format.Example:
Date date = ctx.getDatetimeType().decode("2017-01-03 18:11:31.840").getValue(); String dateAsString = RhinoDate.format(date); assertThat(dateAsString, is("2017-01-03 18:11:31.840"));- Parameters:
date- to be formatted as String- Returns:
- String from date using proper format
-
formatTimestamp
Returns String from timestamp as long using yyyy-MM-dd HH:mm:ss.SSS format.Example:
long timestamp = 1432894454862L; assertThat(RhinoDate.formatTimestamp(timestamp), is("2015-05-29 12:14:14.862"));- Parameters:
timestamp- to be formatted as String- Returns:
- String from date using proper format
-
format
Returns String in given pattern from date.Example:
Date date = higson.getDatetimeType().decode("25-11-2018").getValue(); String dateAsString = RhinoDate.format(date, "dd-MM-yyyy"); assertThat(dateAsString, is("25-11-2018"));- Parameters:
date- to be formatted as Stringpattern- that date should be formatted against- Returns:
- String from date using proper format
-
formatDMY
Returns String in dd-MM-yyyy pattern from date.Example:
Date date = higson.getDatetimeType().decode("25-11-2018").getValue(); String dateAsString = RhinoDate.formatDMY(date); assertThat(dateAsString, is("25-11-2018"));- Parameters:
date- to be formatted as String- Returns:
- String from date using format
-
getYear
Get year from date, but date must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.Example:
int year = RhinoDate.getYear("2018-10-29"); // valid int year = RhinoDate.getYear("2018-10-29 18:11:31"); // valid int yearAsZero = RhinoDate.getYear("29-10-2018"); // not valid format assertThat(year, is(2018)); assertThat(yearAsZero, is(0));- Parameters:
date- from which year is extracted- Returns:
- 0 if date is not properly parsed, otherwise year as int
-
getMonth
Get month from date, but date must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.Example:
int month = RhinoDate.getMonth("2018-10-29"); // valid int month = RhinoDate.getMonth("2018-10-29 18:11:31"); // valid int monthAsZero = RhinoDate.getMonth("29-10-2018"); // not valid format assertThat(month, is(10)); assertThat(monthAsZero, is(0));- Parameters:
date- from which month is extracted- Returns:
- 0 if date is not properly parsed, otherwise month as int
-
getDay
Get day from date, but date must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats.Example:
int day = RhinoDate.getDay("2018-10-29"); // valid int day = RhinoDate.getDay("2018-10-29 18:11:31"); // valid int dayAsZero = RhinoDate.getDay("29-10-2018"); // not valid format assertThat(day, is(29)); assertThat(dayAsZero, is(0));- Parameters:
date- from which day is extracted- Returns:
- 0 if date is not properly parsed, otherwise day as int
-
getHour
Get hour from date, but date must be in yyyy-MM-dd HH:mm:ss format.Example:
int hour = RhinoDate.getHour("2018-10-29 18:11:31"); // valid int hourAsZero = RhinoDate.getHour("29-10-2018"); // not valid format assertThat(hour, is(18)); assertThat(hourAsZero, is(0));- Parameters:
date- from which hour is extracted- Returns:
- 0 if hour is not properly parsed, otherwise hour as int
-
getMinute
Get minutes from date, but date must be in yyyy-MM-dd HH:mm:ss format.Example:
int minutes = RhinoDate.getMinute("2018-10-29 18:11:31"); // valid int minutesAsZero = RhinoDate.getMinute("29-10-2018"); // not valid format assertThat(minutes, is(11)); assertThat(minutesAsZero, is(0));- Parameters:
date- from which minutes are extracted- Returns:
- 0 if minutes are not properly parsed, otherwise minutes as int
-
getSecond
Get seconds from date, but date must be in yyyy-MM-dd HH:mm:ss format.Example:
int seconds = RhinoDate.getSecond("2018-10-29 18:11:31"); // valid int secondsAsZero = RhinoDate.getSecond("29-10-2018"); // not valid format assertThat(seconds, is(31)); assertThat(secondsAsZero, is(0));- Parameters:
date- from which seconds are extracted- Returns:
- 0 if seconds are not properly parsed, otherwise seconds as int
-
addYear
Adds amount to 'year' field that is based on given date.
When given date is:1) If amount > 0, it will add years,<br> 2) If amount < 0, it will subtract years from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder od NumberHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add years, but if negative it will subtract years from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = addYear("2018-11-13", 2); Date resultForNegativeAmount = addYear("2018-11-13", -2); assertThat(date.format(result), is("2020-11-13")); assertThat(date.format(resultForNegativeAmount), is("2016-11-13"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addYear
Adds amount to 'year' field that is based on given date.
When given date is:1) If amount > 0, it will add years, 2) If amount < 0, it will subtract years from date.- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = addYear("2018-11-13", 2); Date resultForNegativeAmount = addYear("2018-11-13", -2); assertThat(RhinoDate.format(result), is("2020-11-13")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2016-11-13"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addYear
Adds amount to 'year' field that is based on given date.
This method doesn't modify input date, but creates new instance of date type.1) If amount > 0, it will add years,<br> 2) If amount < 0, it will subtract years from date.<br>Example:
Date input = date.parse("2018-11-13"); Date result = RhinoDate.addYear(input, 2); Date resultForNegativeAmount = RhinoDate.addYear(input, -2); assertThat(RhinoDate.format(result), is("2020-11-13")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2016-11-13"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
addMonth
Adds amount to 'month' field that is based on given date.
When given date is:1) If amount > 0, it will add months,<br> 2) If amount < 0, it will subtract months from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add months, but if negative it will subtract months from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = RhinoDate.addMonth("2018-11-13", 2); Date resultForNegativeAmount = RhinoDate.addMonth("2018-11-13", -2); assertThat(RhinoDate.format(result), is("2019-01-13")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-09-13"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addMonth
Adds amount to 'month' field that is based on given date.
When given date is:1) If amount > 0, it will add months,<br> 2) If amount < 0, it will subtract months from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = RhinoDate.addMonth("2018-11-13", 2); Date resultForNegativeAmount = RhinoDate.addMonth("2018-11-13", -2); assertThat(RhinoDate.format(result), is("2019-01-13")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-09-13"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addMonth
Adds amount to 'month' field that is based on given date.
This method doesn't modify input date, but creates new instance of date type.1) If amount > 0, it will add months,<br> 2) If amount < 0, it will subtract months from date.<br>Example:
Date inputDate = new Date() Date result = RhinoDate.addMonth(inputDate, 2); Date resultForNegativeAmount = RhinoDate.addMonth(inputDate, -2);- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
addDay
Adds amount to 'day' field that is based on given date.
When given date is:1) If amount > 0, it will add days,<br> 2) If amount < 0, it will subtract days from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add days, but if negative it will subtract days from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = RhinoDate.addDay("2018-11-13", 2); Date resultForNegativeAmount = RhinoDate.addDay("2018-11-13", -2); assertThat(RhinoDate.format(result), is("2018-11-15")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-11-11"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addDay
Adds amount to 'day' field that is based on given date.
When given date is:1) If amount > 0, it will add days,<br> 2) If amount < 0, it will subtract days from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = RhinoDate.addDay("2018-11-13", 2); Date resultForNegativeAmount = RhinoDate.addDay("2018-11-13", -2); assertThat(RhinoDate.format(result), is("2018-11-15")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-11-11"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addDay
Adds amount to 'day' field that is based on given date.
This method doesn't modify input date, but creates new instance of date type.1) If amount > 0, it will add days,<br> 2) If amount < 0, it will subtract days from date.<br>Example:
Date inputDate = new Date() Date result = RhinoDate.addDay(inputDate, 2); Date resultForNegativeAmount = RhinoDate.addDay(inputDate, -2);- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
addHour
Adds amount to 'hour' field that is based on given date.
When given date is:1) If amount > 0, it will add hours,<br> 2) If amount < 0, it will subtract hours from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add days, but if negative it will subtract days from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = RhinoDate.addHour("2018-01-03 18:11:31", 2); Date resultForNegativeAmount = RhinoDate.addHour("2018-01-03 18:11:31.840", -2); assertThat(RhinoDate.format(result), is("2018-01-03 20:11:31")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-01-03 16:11:31.840"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addHour
Adds amount to 'hour' field that is based on given date.
When given date is:1) If amount > 0, it will add hours,<br> 2) If amount < 0, it will subtract hours from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = RhinoDate.addHour("2018-01-03 18:11:31", 2); Date resultForNegativeAmount = RhinoDate.addHour("2018-01-03 18:11:31.840", -2); assertThat(RhinoDate.format(result), is("2018-01-03 20:11:31")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-01-03 16:11:31.840"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addHour
Adds amount to 'hour' field that is based on given date.
This method doesn't modify input date, but creates new instance of date type.1) If amount > 0, it will add hours, 2) If amount < 0, it will subtract hours from date.Example:
Date inputDate = new Date(); Date result = RhinoDate.addHour(inputDate, 2); Date resultForNegativeAmount = RhinoDate.addHour(inputDate, -2);- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
addMinute
Adds amount to 'minute' field that is based on given date.
When given date is:1) If amount > 0, it will add minutes,<br> 2) If amount < 0, it will subtract minutes from date.<br>- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add minutes, but if negative it will subtract minutes from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = RhinoDate.addMinute("2018-01-03 18:11:31", 20); Date resultForNegativeAmount = RhinoDate.addMinute("2018-01-03 18:11:31.840", -20); assertThat(RhinoDate.format(result), is("2018-01-03 18:31:31")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-01-03 17:51:31.840"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addMinute
Adds amount to 'minute' field that is based on given date.
1) If amount> 0, it will add minutes,
2) If amount< 0, it will subtract minutes from date.
When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = RhinoDate.addMinute("2018-01-03 18:11:31", 20); Date resultForNegativeAmount = RhinoDate.addMinute("2018-01-03 18:11:31.840", -20); assertThat(RhinoDate.format(result), is("2018-01-03 18:31:31")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-01-03 17:51:31.840"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addMinute
Adds amount to 'minute' field that is based on given date.
1) If amount> 0, it will add minutes,
2) If amount< 0, it will subtract minutes from date.
This method doesn't modify input date, but creates new instance of date type.Example:
Date inputDate = new Date(); Date result = RhinoDate.addMinute(inputDate, 20); Date resultForNegativeAmount = RhinoDate.addMinute(inputDate, -20);- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
addSecond
Adds amount to 'second' field that is based on given date.
1) If amount> 0, it will add seconds,
2) If amount< 0, it will subtract seconds from date.
When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add seconds, but if negative it will subtract seconds from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = RhinoDate.addSecond("2018-01-03 18:11:31", 20); Date resultForNegativeAmount = RhinoDate.addSecond("2018-01-03 18:11:31.840", -20); assertThat(RhinoDate.format(result), is("2018-01-03 18:31:51")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-01-03 18:11:11.840"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addSecond
Adds amount to 'second' field that is based on given date.
1) If amount> 0, it will add seconds,
2) If amount< 0, it will subtract seconds from date.
When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd HH:mm:ss.SSS formats,
- instance of DateHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = RhinoDate.addSecond("2018-01-03 18:11:31", 20); Date resultForNegativeAmount = RhinoDate.addSecond("2018-01-03 18:11:31.840", -20); assertThat(RhinoDate.format(result), is("2018-01-03 18:31:51")); assertThat(RhinoDate.format(resultForNegativeAmount), is("2018-01-03 18:11:11.840"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addSecond
Adds amount to 'second' field that is based on given date.
1) If amount> 0, it will add seconds,
2) If amount< 0, it will subtract seconds from date.
This method doesn't modify input date, but creates new instance of date type.Example:
Date inputDate = new Date(); Date result = RhinoDate.addMinute(inputDate, 20); Date resultForNegativeAmount = RhinoDate.addMinute(inputDate, -20);- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
setYear
Sets year for given date. Supported formats are yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
This method doesn't modify input date, but creates new instance of date type.Example:
Date result = RhinoDate.setYear("2018-01-03 18:11:31", 2019); assertThat(RhinoDate.format(result), is("2019-01-03 18:11:31"));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
setMonth
Sets month for given date. Supported formats are yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
This method doesn't modify input date, but creates new instance of date type.Example:
Date result = RhinoDate.setMonth("2018-01-03 18:11:31", 5); assertThat(RhinoDate.format(result), is("2018-05-03 18:11:31"));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
setDay
Sets day for given date. Supported formats are yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
This method doesn't modify input date, but creates new instance of date type.Example:
Date result = RhinoDate.setDay("2018-01-03 18:11:31", 5); assertThat(RhinoDate.format(result), is("2018-01-05 18:11:31"));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
setHour
Sets hour for given date. Supported formats are yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
This method doesn't modify input date, but creates new instance of date type.Example:
Date result = RhinoDate.setHour("2018-01-03 18:11:31", 12); assertThat(RhinoDate.format(result), is("2018-01-03 12:11:31"));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
setMinute
Sets minute for given date. Supported formats are yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
This method doesn't modify input date, but creates new instance of date type.Example:
Date result = RhinoDate.setMinute("2018-01-03 18:11:31", 12); assertThat(RhinoDate.format(result), is("2018-01-03 18:12:31"));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
setSecond
Sets second for given date. Supported formats are yyyy-MM-dd HH:mm:ss or yyyy-MM-dd.
This method doesn't modify input date, but creates new instance of date type.Example:
Date result = RhinoDate.setSecond("2018-01-03 18:11:31", 12); assertThat(RhinoDate.format(result), is("2018-01-03 18:11:12"));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
rollYear
Adds amount to the 'year' field without changing other fields. A negative roll amount means to subtract from year without changing other fields. If the specified amount is 0, this method performs nothing.
When given date is:- String type - must be in yyyy-MM-dd or yyyy-MM-dd HH:mm:ss formats.
- Date type - no need for parsing
- int type - If amount is positive it will add years, but if negative it will subtract years from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date date = parse("2018-01-02"); assertThat(rollYear(date, 2), is(parse("2020-01-02")));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
rollMonth
Adds amount to the 'month' field without changing other fields. A negative roll amount means to subtract from month without changing other fields. If the specified amount is 0, this method performs nothing.
When given date is:- String type - must be in yyyy-MM-dd or yyyy-MM-dd HH:mm:ss formats.
- Date type - no need for parsing
- int type - If amount is positive it will add months, but if negative it will subtract months from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date date = parse("2018-01-02"); assertThat(rollMonth(date, 5), is(parse("2018-06-02"))); assertThat(rollMonth(date, 12), is(parse("2018-01-02")));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
rollDay
Adds amount to the 'day' field without changing other fields. A negative roll amount means to subtract from day without changing other fields. If the specified amount is 0, this method performs nothing. When given date is:- String type - must be in yyyy-MM-dd or yyyy-MM-dd HH:mm:ss formats.
- Date type - no need for parsing
- int type - If amount is positive it will add days, but if negative it will subtract days from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
This method doesn't modify input date, but creates new instance of date type.Date date = parse("2018-01-02"); assertThat(rollDay(date, 10), is(parse("2018-01-12")));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
rollHour
Adds amount to the 'hour' field without changing other fields. A negative roll amount means to subtract from hour without changing other fields. If the specified amount is 0, this method performs nothing. When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss formats.
- Date type - no need for parsing
- int type - If amount is positive it will add hours, but if negative it will subtract hours from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date datetime = type.getDatetime("2018-01-02 12:21:22.000"); assertThat(rollHour(datetime, 10), is(type.getDatetime("2018-01-02 22:21:22.000")));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
rollMinute
Adds amount to the 'minute' field without changing other fields. A negative roll amount means to subtract from minute without changing other fields. If the specified amount is 0, this method performs nothing. When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss formats.
- Date type - no need for parsing
- int type - If amount is positive it will add minutes, but if negative it will subtract minutes from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date datetime = type.getDatetime("2018-01-02 12:21:22.000"); assertThat(rollMinute(datetime, 20), is(type.getDatetime("2018-01-02 12:41:22.000"))); assertThat(rollMinute(datetime, 60), is(type.getDatetime("2018-01-02 12:21:22.000")));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
rollSecond
Adds amount to the 'second' field without changing other fields. A negative roll amount means to subtract from second without changing other fields. If the specified amount is 0, this method performs nothing. When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss formats.
- Date type - no need for parsing
- int type - If amount is positive it will add seconds, but if negative it will subtract seconds from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
String datetimeAsString = "2018-01-02 12:21:22.000"; assertThat(rollSecond(datetimeAsString, 10), is(type.getDatetime("2018-01-02 12:21:32.000"))); assertThat(rollSecond(datetimeAsString, 60), is(type.getDatetime("2018-01-02 12:21:22.000")));- Parameters:
date- based on which new date will be created with changeamount- int value, that is used for setting field- Returns:
- new instance, that is combination of given date and amount
-
getYearDiff
Returns difference of years between givens dates.
Possible outcomes:- negative result if date1
> date2, - positive result if date1
< date2, - zero if there is no difference or any of given dates are null.
Example:
TypeConverter type = new TypeConverter(); Date d1 = type.getDate("2001-06-15"); Date d2 = type.getDate("2003-01-01"); assertThat(getYearDiff(d1, d2), is(2)); assertThat(getYearDiff(d2, d1), is(-2));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment
- negative result if date1
-
getFullYearDiff
The same asgetAbsoluteYearDiff(Object, Object), but it is not null safe. If either date1 or date2 is null, HigsonException will be thrown.- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment
- Throws:
HigsonRuntimeException- if date1 or date2 is null- See Also:
-
getMonthDiff
Returns difference of months between givens dates. Possible outcomes:- negative result if date1
> date2, - positive result if date1
< date2, - zero if there is no difference or any of given dates are null.
Example:
assertThat(getMonthDiff("2010-01-15", "2010-01-15"), is(0)); assertThat(getMonthDiff("2010-01-01", "2010-02-28"), is(1)); assertThat(getMonthDiff("2010-02-01", "2010-01-30"), is(-1));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment
- See Also:
- negative result if date1
-
getMonthDiffDeprecated
Deprecated.will be removed in future, usegetMonthDiff(java.lang.Object, java.lang.Object)- Parameters:
date1- first datedate2- second date- Returns:
- int value with difference of months between givens dates.
-
getDayDiffDeprecated
Deprecated.will be removed in future, usegetDayDiff(java.lang.Object, java.lang.Object)- Parameters:
date1- first datedate2- second date- Returns:
- int value with difference of days between givens dates.
-
getDayDiff
Returns difference of days between givens dates. Possible outcomes:- negative result if date1
> date2, - positive result if date1
< date2, - zero if there is no difference or any of given dates are null.
Example:
assertThat(getDayDiff("2010-01-15", "2010-01-15"), is(0)); assertThat(getDayDiff("2010-01-01", "2010-01-28"), is(27)); assertThat(getDayDiff("2010-02-05", "2010-02-03"), is(-2));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment
- See Also:
- negative result if date1
-
getDayDiff
Returns difference of days between givens dates. Possible outcomes:- positive result if date1 < date2 or date1
> date2, - zero if there is no difference or any of given dates are null.
Example:
assertThat(getDayDiff("20-02-2017", "15-02-2017", "MM-DD-YYYY"), is(5)); assertThat(getDayDiff("15-02-2017", "20-02-2017", "MM-DD-YYYY"), is(5)); // WATCH OUT!- Parameters:
date1- first datedate2- second datepattern- used for parsing date, if they are Strings- Returns:
- int value as defined in comment
- Throws:
HigsonRuntimeException- if date1 or date2 was null or not properly parsed- See Also:
- positive result if date1 < date2 or date1
-
getHourDiff
Deprecated.Will be removed in the future- Parameters:
date1- datedate2- date- Returns:
- hour diff
-
getMinuteDiff
Deprecated.Will be removed in the future- Parameters:
date1- datedate2- date- Returns:
- minute diff
-
getTime
Deprecated.Will be removed in the future- Parameters:
cal- Calendar object- Returns:
- Date object
-
addMonthOracle
Adds amount to 'month' field that is based on given date, but if day is last day of a given month, then moves day to the last possible day of a calculated month.
1) If amount> 0, it will add months,
2) If amount< 0, it will subtract months from date.
When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder od NumberHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
- int type - If amount is positive it will add months, but if negative it will subtract months from date,
- instance of Number - it will try to retrieve int from number,
- instance of other Object types - it will try to retrieve int from instance, by parsing toString() as Integer.
Example:
Date result = RhinoDate.addMonthOracle("2018-02-28", 1); Date secondResult = RhinoDate.addMonthOracle("2018-02-28", 2); Date notChangedDay = RhinoDate.addMonthOracle("2018-02-27", 2); assertThat(RhinoDate.format(result), is("2018-03-31")); assertThat(RhinoDate.format(secondResult), is("2018-04-30")); assertThat(RhinoDate.format(notChangedDay), is("2018-04-27"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addMonthOracle
Adds amount to 'month' field that is based on given date, but if day is last day of a given month, then moves day to the last possible day of a calculated month.
1) If amount> 0, it will add months,
2) If amount< 0, it will subtract months from date.
When given date is:- String type - must be in yyyy-MM-dd HH:mm:ss or yyyy-MM-dd formats,
- instance of DateHolder od NumberHolder - it will try to retrieve date from holder,
- instance of other type - it will try to convert date to proper ValueHolder and then retrieve date.
Example:
Date result = RhinoDate.addMonthOracle("2018-02-28", 1); Date secondResult = RhinoDate.addMonthOracle("2018-02-28", 2); Date notChangedDay = RhinoDate.addMonthOracle("2018-02-27", 2); assertThat(RhinoDate.format(result), is("2018-03-31")); assertThat(RhinoDate.format(secondResult), is("2018-04-30")); assertThat(RhinoDate.format(notChangedDay), is("2018-04-27"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
- Throws:
IllegalArgumentException- if there are any problems with date conversions
-
addMonthOracle
Adds amount to 'month' field that is based on given date, but if day is last day of a given month, then moves day to the last possible day of a calculated month.
1) If amount> 0, it will add months,
2) If amount< 0, it will subtract months from date.
This method doesn't modify input date, but creates new instance of date type.Example:
Date lastDay = RhinoDate.parse("2018-02-28"); Date notLastDay = RhinoDate.parse("2018-02-27"); Date result = RhinoDate.addMonthOracle(lastDay, 1); Date secondResult = RhinoDate.addMonthOracle(lastDay, 2); Date notChangedDay = RhinoDate.addMonthOracle(notLastDay, 2); assertThat(RhinoDate.format(result), is("2018-03-31")); assertThat(RhinoDate.format(secondResult), is("2018-04-30")); assertThat(RhinoDate.format(notChangedDay), is("2018-04-27"));- Parameters:
date- based on which new date will be created with changeamount- to be added if positive or subtract if negative- Returns:
- new instance, that is combination of given date and amount
-
trim
Clearing time part of a given date. This method doesn't modify input date, but creates new instance of date type.Example:
assertThat(trim(type.getDatetime("2018-02-28")), is(type.getDatetime("2018-02-28"))); assertThat(trim(type.getDatetime("2018-02-28 18:11:31.840")), is(type.getDatetime("2018-02-28 00:00:00.000")));- Parameters:
date- to be trimmed- Returns:
- trimmed date if not null, or null otherwise
-
getDate
Creates new date from given parameters.- Parameters:
year- yearmonth- monthday- dayhour- hourminute- minutesecond- second- Returns:
- new date
-
getDate
Creates new date from given parameters.- Parameters:
year- yearmonth- monthday- dayhour- hourminute- minute- Returns:
- new date
-
getDate
Creates new date from given parameters without time.- Parameters:
year- yearmonth- monthday- day- Returns:
- new date
-
getFirstDayOfMonth
Create new date based on given date, but set to first day of a month.- Parameters:
date- that is based on- Returns:
- new date
-
getFirstDayOfYear
Create new date based on given date, but set to first day of a year.- Parameters:
date- that is based on- Returns:
- new date
-
getLastDayOfMonth
Create new date based on given date, but set to last day of a month.- Parameters:
date- that is based on- Returns:
- new date
-
getLastDayOfYear
Create new date based on given date, but set to last day of a year.- Parameters:
date- that is based on- Returns:
- new date
-
getNextWorkDay
Get next working date from given date. Short examples:- Monday -
>Tuesday - Tuesday -
>Wednesday - Friday -
>Monday - Saturday -
>Monday - Sunday -
>Monday
Example:
assertThat(RhinoDate.getNextWorkDay(type.getDate("2018-02-28")), is(type.getDate("2018-03-01")));- Parameters:
date- to be used as starting point- Returns:
- next work date
- Monday -
-
getNearestWorkDay
Get closets work day from given date, but if given date is work date, it will return same instance.Example:
assertThat(RhinoDate.getNearestWorkDay(type.getDate("2018-02-28")), is(type.getDate("2018-02-28"))); assertThat(RhinoDate.getNearestWorkDay(type.getDate("2018-12-01")), is(type.getDate("2018-12-03")));- Parameters:
date- to be checked- Returns:
- same instance of date if it is working date, or next work day
-
isWorkDay
Checks if given date is work date.- Parameters:
date- to be checked- Returns:
- true if it is work date, false otherwise
-
isLastDayOfMonth
Checks if given date is last date of a month.Example:
Date date1 = type.getDate("2018-10-31"); Date date2 = type.getDate("2018-12-31"); Date date3 = type.getDate("2018-12-28"); assertThat(RhinoDate.isLastDayOfMonth(date1), is(true)); assertThat(RhinoDate.isLastDayOfMonth(date2), is(true)); assertThat(RhinoDate.isLastDayOfMonth(date3), is(false));- Parameters:
date- to be checked- Returns:
- true if given date is set to last day of a month, false otherwise (event if date is null)
-
isFirstWorkDayOfMonth
Checks if given date is both a first day of a month and a work day.Example:
Date date1 = type.getDate("2018-10-01"); Date date2 = type.getDate("2018-10-02"); Date date3 = type.getDate("2018-12-01"); Date date4 = type.getDate("2018-12-03"); assertThat(RhinoDate.isFirstWorkDayOfMonth(date1), is(true)); assertThat(RhinoDate.isFirstWorkDayOfMonth(date2), is(false)); assertThat(RhinoDate.isFirstWorkDayOfMonth(date3), is(false)); assertThat(RhinoDate.isFirstWorkDayOfMonth(date4), is(true));- Parameters:
date- to be checked- Returns:
- true if given date pass the conditions, false otherwise
-
subtractYear
Deprecated.will be removed in the future, useaddYear(Date, int)with -1 amount.- Parameters:
date- date- Returns:
- Date object
-
copy
Creates copy of given date.- Parameters:
date- to be copied- Returns:
- null if given date was null, otherwise proper copy
-
getFirstDayOfYear
Create date with a beginning for a given year.Example:
Date startDate = getFirstDayOfYear(1998) // "1998-01-01 00:00:00"- Parameters:
year- used for date creation- Returns:
- new date instance
-
getFirstWorkingDayOfYear
Create date, that is set to the first work day of beginning of given year.Example:
Date date1 = type.getDate("2018-01-02"); assertThat(RhinoDate.getFirstWorkingDayOfYear(2018), is(date1));- Parameters:
year- used for date creation- Returns:
- new date instance
-
isInPeriod
Checks if given dateToCheck is within boundaries. The simplified check looks like this:
1) If minDateInclusive is null, then it acts like Long.MIN_VALUE,minDateInclusive <= dateToCheck < maxDateExclusive.
2) If maxDateExclusive is null, then it acts like Long.MAX_VALUE,
3) Parameter dateToCheck can't be null.Example:
Date minDate = type.getDate("2018-01-02"); Date maxDate = type.getDate("2018-01-05"); assertThat(RhinoDate.isInPeriod(type.getDate("2018-01-01"), minDate, maxDate), is(false)); assertThat(RhinoDate.isInPeriod(type.getDate("2018-01-02"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriod(type.getDate("2018-01-03"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriod(type.getDate("2018-01-04"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriod(type.getDate("2018-01-05"), minDate, maxDate), is(false)); assertThat(RhinoDate.isInPeriod(type.getDate("2018-01-06"), minDate, maxDate), is(false));- Parameters:
dateToCheck- date to be checked if it is withing boundariesminDateInclusive- minimal date(inclusive)maxDateExclusive- maximal date(exclusive)- Returns:
- true if described conditions are met, false otherwise
-
isInPeriod
public static boolean isInPeriod(long d, long d1, long d2) Works the same asisInPeriod(Date, Date, Date), but for longs.- Parameters:
d- lower dated1- upper dated2- date to be checked in period- Returns:
- true if d2 is in period, or false
-
isInPeriodInclusive
public static boolean isInPeriodInclusive(Date dateToCheck, Date minDateInclusive, Date maxDateInclusive) Checks if given dateToCheck is within boundaries. The simplified check looks like this:
1) If minDateInclusive is null, then it acts like Long.MIN_VALUE,minDateInclusive <= dateToCheck <= maxDateInclusive.
2) If maxDateInclusive is null, then it acts like Long.MAX_VALUE,
3) Parameter dateToCheck can't be null.Example:
Date minDate = type.getDate("2018-01-02"); Date maxDate = type.getDate("2018-01-05"); assertThat(RhinoDate.isInPeriodInclusive(type.getDate("2018-01-01"), minDate, maxDate), is(false)); assertThat(RhinoDate.isInPeriodInclusive(type.getDate("2018-01-02"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriodInclusive(type.getDate("2018-01-03"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriodInclusive(type.getDate("2018-01-04"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriodInclusive(type.getDate("2018-01-05"), minDate, maxDate), is(true)); assertThat(RhinoDate.isInPeriodInclusive(type.getDate("2018-01-06"), minDate, maxDate), is(false));- Parameters:
dateToCheck- date to be checked if it is withing boundariesminDateInclusive- minimal date(inclusive)maxDateInclusive- maximal date(inclusive)- Returns:
- true if described conditions are met, false otherwise
-
isInPeriodInclusive
public static boolean isInPeriodInclusive(long d, long d1, long d2) Works the same asisInPeriodInclusive(Date, Date, Date), but for longs.- Parameters:
d- lower dated1- upper dated2- date to be checked in period- Returns:
- true if d2 is in period, or false
-
max
Checks which one of given dates is greater then the other. It doesn't create new instances, but returns the same instance from parameters. If d1 is null, then d2 is return. If d2 is null, then d1 is return. Example:Date date1 = type.getDate("2017-01-03"); Date date2 = type.getDate("2017-01-04"); assertThat(max(date1, date2), is(date2));- Parameters:
d1- first dated2- second date- Returns:
- either d1 if it is greater or d2
-
max
Checks which one of given dates is greater then the others. It doesn't create new instances, but returns the same instance from parameters. Example:Date date1 = type.getDate("2017-01-03"); Date date2 = type.getDate("2017-01-05"); Date date3 = type.getDate("2017-01-04"); assertThat(max(date1, date2, date3), is(date2));- Parameters:
d1- first dated2- second dated3- third date- Returns:
- maximal date from d1, d2 or d3
-
min
Checks which one of given dates is smaller then the other. It doesn't create new instances, but returns the same instance from parameters. If d1 or d2 is null, then null is return. Example:Date date1 = type.getDate("2017-01-03"); Date date2 = type.getDate("2017-01-04"); assertThat(min(date1, date2), is(date1));- Parameters:
d1- first dated2- second date- Returns:
- null if any dates is null, or smaller date
-
min
Checks which one of given dates is the earliest. It doesn't create new instances, but returns the same instance from parameters. If d1 or d2 or d3 is null, then null is return. Example:Date date1 = type.getDate("2017-01-03"); Date date2 = type.getDate("2017-01-05"); Date date3 = type.getDate("2017-01-04"); assertThat(min(date1, date2, date3), is(date1));- Parameters:
d1- first dated2- second dated3- third date- Returns:
- null if any dates is null, or the earliest date
-
getPolishHolidays
Returns set of polish holidays for given year, except these years that happen to occur during weekend. Supported polish holidays, where is just an example: 1.1.2018, 6.1.2018, 1.5.2018, 3.5.2018, 15.8.2018, 1.11.2018, 11.11.2018, 25.12.2018, 26.12.2018, also two other holidays: Corpus Christi and Easter , that are calculated based on year.- Parameters:
year- for it, holidays will be checked- Returns:
- set of polish holidays
-
getNearestWorkDayExcludingPolishHolidays
Get next work day based on given date, but also verify polish holidays, and consider them during verification. If given date is not work day, because it is a weekend, then return given date(same instance). Example:Date date1 = RhinoDate.getNearestWorkDayExcludingPolishHolidays(RhinoDate.parse("2018-10-31")); Date date2 = RhinoDate.getNearestWorkDayExcludingPolishHolidays(RhinoDate.parse("2018-11-01")); Date date3 = RhinoDate.getNearestWorkDayExcludingPolishHolidays(RhinoDate.parse("2018-11-02")); assertThat(date1, is(type.getDate("2018-10-31"))); assertThat(date2, is(type.getDate("2018-11-02"))); assertThat(date3, is(type.getDate("2018-11-02")));- Parameters:
date- to be used as starting date- Returns:
- same date instance if given date is not polish holiday, or work date. Otherwise it will return next work day.
-
compareYMD
Compares two dates by year, month, date. Possible outcomes:
Supported formats are the same as-1 : if date1 < date2 0 : if date1 = date2 1 : if date1 > date2parse(String)Example:assertThat(RhinoDate.compareYMD(parse("2018-01-01"), parse("2018-01-05")), is(-1)); assertThat(RhinoDate.compareYMD(parse("2018-01-05"), parse("2018-01-01")), is(1)); assertThat(RhinoDate.compareYMD(parse("2018-01-01"), parse("2018-01-01")), is(0)); assertThat(RhinoDate.compareYMD(parse("01-01-2018"), parse("2018-01-01")), is(0)); assertThat(RhinoDate.compareYMD(parse("05-01-2018"), parse("2018-01-01")), is(1)); assertThat(RhinoDate.compareYMD(parse("01-01-2018"), parse("05-01-2018")), is(-1));- Parameters:
date1- first datedate2- second date- Returns:
- -1, 0 or 1 based on described conditions
-
isLeapYear
public static boolean isLeapYear(int year) Verifies if given year is a leap year.- Parameters:
year- to be checked- Returns:
- true if leap year, false otherwise
-
getDaysInYear
public static int getDaysInYear(int year) Number of days in a given year. It does consider leap year.- Parameters:
year- to be checked- Returns:
- number of days
-
toInt
It will try to convert given date to int representation. Example:assertThat(RhinoDate.toInt(parse("2018-01-01")), is(20180101));- Parameters:
date- to be converted- Returns:
- int representation of a date, that will mimic format yyyyMMdd
-
fromInt
It will try to convert given date as int to proper date instance. Given int value must mimic date format, like: yyyyMMddExample:
assertThat(RhinoDate.fromInt(20180101), is(parse("2018-01-01")));- Parameters:
date- to be converted- Returns:
- date instance or null, if parsing int was not successful
-
getMonthsBetween
Calculates full months intervals between date1 and date2. The result is always positive value, but parameters can't be null or HigsonRuntimeException will be thrown. It also considers: 1. last day of a month (last day of April - last day of Match = 1m) 2. leap yearExample:
// date1 == date2 assertThat(getMonthsBetween(parse("2010-01-15"), parse("2010-01-15")), is(0)); // date1 < date2 assertThat(getMonthsBetween("2010-01-01", "2011-03-30"), is(2 + 12)); assertThat(getMonthsBetween("2010-01-01", "2012-03-30"), is(2 + 24)); assertThat(getMonthsBetween("2010-01-31", "2010-02-01"), is(0)); assertThat(getMonthsBetween("2010-01-01", "2010-02-01"), is(1)); // date1 > date2 assertThat(getMonthsBetween("2010-01-22", "2010-01-20"), is(0)); assertThat(getMonthsBetween("2010-02-01", "2010-01-01"), is(1));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment, but never negative value.
- See Also:
-
getDaysBetween
Calculates full days intervals between date1 and date2. The result is always positive value, but parameters can't be null or HigsonRuntimeException will be thrown. It also considers HH:MM:SS.sss part.Example:
Date d1 = type.getDatetime("2017-01-01 12:00:00"); Date d2 = type.getDatetime("2017-01-02 10:00:00"); Date d3 = type.getDatetime("2017-01-02 12:01:00"); Date d4 = type.getDatetime("2017-01-04 05:01:00"); assertThat(getDaysBetween(d1, d1), is(0)); assertThat(getDaysBetween(d1, d2), is(0)); assertThat(getDaysBetween(d1, d3), is(1)); assertThat(getDaysBetween(d1, d4), is(2));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment, but never negative value.
- See Also:
-
getAbsoluteYearDiff
Calculates full years intervals between date1 and date2. Possible outcomes:- positive value of difference in years
- zero if there is no difference or any of given dates are null
Example:
TypeConverter type = new TypeConverter(); Date d1 = type.getDate("2001-06-15"); Date d2 = type.getDate("2003-01-01"); assertThat(getAbsoluteYearDiff(d1, d2), is(1)); assertThat(getAbsoluteYearDiff(d2, d1), is(1));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment
- See Also:
-
getYearsBetween
Calculates full years intervals between date1 and date2. The result is always positive value, but parameters can't be null or HigsonRuntimeException will be thrown.Example:
TypeConverter type = new TypeConverter(); Date d1 = type.getDate("2001-06-15"); Date d2 = type.getDate("2003-01-01"); assertThat(getYearsBetween(d1, d2), is(1)); assertThat(getYearsBetween(d2, d1), is(1));- Parameters:
date1- first datedate2- second date- Returns:
- int value as defined in comment, but never negative value.
- Throws:
HigsonRuntimeException- if date1 or date2 is null- See Also:
-
julianDay
public static int julianDay(int year, int month, int day) Read more about here: http://en.wikipedia.org/wiki/Julian_day- Parameters:
year- yearmonth- monthday- day- Returns:
- int value with Julian Day
-
julianDay
Read more about here: http://en.wikipedia.org/wiki/Julian_day- Parameters:
date- date- Returns:
- int value with Julian Day
-
julianDayDiff
Read more about here: http://en.wikipedia.org/wiki/Julian_day Difference between to julian days.Example:
Date d1 = type.getDatetime("2017-01-01 12:00:00"); Date d2 = type.getDatetime("2017-01-02 10:00:00"); Date d3 = type.getDatetime("2017-01-02 12:01:00"); Date d4 = type.getDatetime("2017-01-04 05:01:00"); assertThat(julianDayDiff(d1, d2), is(1)); assertThat(julianDayDiff(d1, d3), is(1)); assertThat(julianDayDiff(d1, d4), is(3));- Parameters:
date1- first datedate2- second date- Returns:
- difference in days as int value }
-
currentDatetime()method