java.lang.Object
org.tentackle.common.DateHelper
Some common methods for date- and time-related types based on
Date.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic Timedaytime()Gets the current time.static Timedaytime(int secondOffset, int minuteOffset, int hourOffset) Gets the current time with an offset.static intderive4DigitYearFrom2DigitYear(int year) Derives a 4-digit from a 2-digit year referring to the current year and using a year window of 50.static intderive4DigitYearFrom2DigitYear(int year, int currentYear, int yearWindow) Derives a 4-digit from a 2-digit year.static intderiveMonthFromDay(int day) Derives the month from a day referring to the current date and a dayWindow of 15.static intderiveMonthFromDay(int day, int currentDay, int currentMonth, int dayWindow) Derives the month from a day.static intderiveYearFromMonth(int month) Derives the year from a month referring to the current date and a monthWindow of 6.static intderiveYearFromMonth(int month, int currentMonth, int currentYear, int monthWindow) Derives the year from a month.static intGets the current day.static intGets the current month.static intGets the current year.static Timestampnow()Gets the current system time.static Timestampnow(long offsetMillis) Gets the current system time plus an optional offset.static voidsetMidnight(Calendar calendar) Sets the given calendar to midnight.static CalendartoCalendar(Date date) Creates a calendar for the given date.
The calendar is created using the thread-local locale, if set, else the default locale.static CalendartoCalendar(Time time) Creates a calendar for the given time.
The calendar is created using the thread-local locale, if set, else the default locale.static CalendartoCalendar(Timestamp timestamp) Creates a calendar for the given timestamp.
The calendar is created using the thread-local locale, if set, else the default locale.static DateConverts a Calendar into a org.tentackle.misc.Date.static DateConverts ajava.util.Dateinto anorg.tentackle.misc.Date.static Calendartoday()Creates a calendar of the current date and time.
The calendar is created using the thread-local locale, if set, else the default locale.static Datetoday(int dayOffset, int monthOffset, int yearOffset) Getstoday()with an offset.static longConverts a date to the epochal time in millis with null-check.static TimeConverts a Calendar into a org.tentackle.misc.Time.static TimeConverts a java.util.Date into a org.tentackle.misc.Time.static TimestamptoTimestamp(Calendar cal) Converts a Calendar into a org.tentackle.misc.Timestamp.static TimestamptoTimestamp(Date date) Converts a java.util.Date into a org.tentackle.misc.Timestamp.
-
Field Details
-
MIN_DATE
epochal date zero: 1.1.1970 00:00:00 GMT -
MIN_TIMESTAMP
epochal timestamp zero: 1.1.1970 00:00:00.000 GMT
-
-
Method Details
-
toMillis
Converts a date to the epochal time in millis with null-check.- Parameters:
date- the date, time or timestamp, may be null- Returns:
- the epochal time in ms, 0 if date is null
-
toDate
Converts ajava.util.Dateinto anorg.tentackle.misc.Date.- Parameters:
date- the util date- Returns:
- the sql date, null if date was null
-
toDate
Converts a Calendar into a org.tentackle.misc.Date.- Parameters:
cal- the calendar- Returns:
- the date, null if cal was null
-
toTime
Converts a java.util.Date into a org.tentackle.misc.Time.- Parameters:
date- the date- Returns:
- the time of the day in date, null if date was null
-
toTime
Converts a Calendar into a org.tentackle.misc.Time.- Parameters:
cal- the calendar- Returns:
- the time of day, null if cal was null
-
toTimestamp
Converts a java.util.Date into a org.tentackle.misc.Timestamp.- Parameters:
date- the date- Returns:
- the timestamp, null if date was null
-
toTimestamp
Converts a Calendar into a org.tentackle.misc.Timestamp.- Parameters:
cal- the calendar- Returns:
- the timestamp, null if cal was null
-
today
Creates a calendar of the current date and time.
The calendar is created using the thread-local locale, if set, else the default locale.- Returns:
- the current calendar instance
-
toCalendar
Creates a calendar for the given date.
The calendar is created using the thread-local locale, if set, else the default locale.- Parameters:
date- the date- Returns:
- the calendar representing the date
-
toCalendar
Creates a calendar for the given timestamp.
The calendar is created using the thread-local locale, if set, else the default locale.Notice: org.tentackle.common.Timestamp does not extend org.tentackle.common.Date!
- Parameters:
timestamp- the timestamp- Returns:
- the calendar representing the timestamp
-
toCalendar
Creates a calendar for the given time.
The calendar is created using the thread-local locale, if set, else the default locale.Notice: org.tentackle.common.Time does not extend org.tentackle.common.Date!
- Parameters:
time- the time- Returns:
- the calendar representing the time
-
today
Getstoday()with an offset.- Parameters:
dayOffset- the offset in daysmonthOffset- the offset in monthsyearOffset- the offset in years- Returns:
- the date
-
daytime
Gets the current time.- Returns:
- current Time
-
daytime
Gets the current time with an offset.- Parameters:
secondOffset- the offset in secondsminuteOffset- the offset in minuteshourOffset- the offset in hours- Returns:
- the time
-
now
Gets the current system time plus an optional offset.- Parameters:
offsetMillis- the offset to the current system time in milliseconds- Returns:
- current Timestamp
-
now
Gets the current system time.- Returns:
- current Timestamp
-
setMidnight
Sets the given calendar to midnight.- Parameters:
calendar- the calendar
-
getCurrentYear
public static int getCurrentYear()Gets the current year.- Returns:
- the current 4-digit year
-
getCurrentMonth
public static int getCurrentMonth()Gets the current month.- Returns:
- the current month (0 - 11)
-
getCurrentDay
public static int getCurrentDay()Gets the current day.- Returns:
- the current day (1 - 31)
-
derive4DigitYearFrom2DigitYear
public static int derive4DigitYearFrom2DigitYear(int year, int currentYear, int yearWindow) Derives a 4-digit from a 2-digit year.Finds the closest distance to the current, past or next century according to a given current year.
- Parameters:
year- the year, will be cut to 2 digits via modulo 100currentYear- the current 4 digit yearyearWindow- the year window in number of years in the future (0-100, any other value always returns the year in the current century)- Returns:
- the closest 4-digit year
-
derive4DigitYearFrom2DigitYear
public static int derive4DigitYearFrom2DigitYear(int year) Derives a 4-digit from a 2-digit year referring to the current year and using a year window of 50.Finds the closest distance to the current, past or next century according to the current year.
- Parameters:
year- the year, will be cut to 2 digits via modulo 100- Returns:
- the closest 4-digit year
-
deriveYearFromMonth
public static int deriveYearFromMonth(int month, int currentMonth, int currentYear, int monthWindow) Derives the year from a month.- Parameters:
month- the month (0-11)currentMonth- the current month (0-11)currentYear- the current year (4 digits)monthWindow- the month window, (0-11, any other value returns the current year)- Returns:
- the closest year (4 digits)
-
deriveYearFromMonth
public static int deriveYearFromMonth(int month) Derives the year from a month referring to the current date and a monthWindow of 6.- Parameters:
month- the month (0-11)- Returns:
- the closest year (4 digits)
-
deriveMonthFromDay
public static int deriveMonthFromDay(int day, int currentDay, int currentMonth, int dayWindow) Derives the month from a day.- Parameters:
day- the day (1-31)currentDay- the current day of the month (1-31)currentMonth- the current month (0-11)dayWindow- the day window (0-30)- Returns:
- the closest month (0-11 for current year, -1 for december of last year, 12 for january of next year)
-
deriveMonthFromDay
public static int deriveMonthFromDay(int day) Derives the month from a day referring to the current date and a dayWindow of 15.- Parameters:
day- the day (1-31)- Returns:
- the closest month (0-11 for current year, -1 for december of last year, 12 for january of next year)
-