- java.lang.Object
-
- org.tentackle.common.DateHelper
-
public final class DateHelper extends java.lang.ObjectSome common methods for date and time.- Author:
- harald
-
-
Field Summary
Fields Modifier and Type Field Description static DateMIN_DATEepochal date zero: 1.1.1970 00:00:00 UTCstatic java.time.LocalDateMIN_LOCALDATEepochal date zero: 1.1.1970 00:00:00 UTCstatic java.time.LocalDateTimeMIN_LOCALDATETIMEepochal timestamp zero: 1.1.1970 00:00:00.000 UTCstatic TimestampMIN_TIMESTAMPepochal timestamp zero: 1.1.1970 00:00:00.000 UTC
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intconvert2DigitYearTo4DigitYear(int year2)Converts a 2-digit year to a 4-digit year.static intconvert2DigitYearTo4DigitYear(int year2, int currentYear4)Converts a 2-digit year to a 4-digit year.static Timedaytime()Gets the current time.static Timedaytime(int secondOffset, int minuteOffset, int hourOffset)Gets the current time with an offset.static Timestampnow()Gets the current system time.static Timestampnow(long offsetMillis)Gets the current system time plus an optional offset.static voidsetMidnight(java.util.GregorianCalendar greg)Sets the given calendar to midnight.static DatetoDate(java.util.Calendar cal)Converts a Calendar into a org.tentackle.misc.Date.static DatetoDate(java.util.Date date)Converts ajava.util.Dateinto anorg.tentackle.misc.Date.static Datetoday()Gets the current date.static Datetoday(int dayOffset, int monthOffset, int yearOffset)Gets the current date with an offset.static longtoMillis(java.util.Date date)Converts a date to the epochal time in millis with null-check.static TimetoTime(java.util.Calendar cal)Converts a Calendar into a org.tentackle.misc.Time.static TimetoTime(java.util.Date date)Converts a java.util.Date into a org.tentackle.misc.Time.static TimestamptoTimestamp(java.util.Calendar cal)Converts a Calendar into a org.tentackle.misc.Timestamp.static TimestamptoTimestamp(java.util.Date date)Converts a java.util.Date into a org.tentackle.misc.Timestamp.
-
-
-
Field Detail
-
MIN_DATE
public static final Date MIN_DATE
epochal date zero: 1.1.1970 00:00:00 UTC
-
MIN_LOCALDATE
public static final java.time.LocalDate MIN_LOCALDATE
epochal date zero: 1.1.1970 00:00:00 UTC
-
MIN_TIMESTAMP
public static final Timestamp MIN_TIMESTAMP
epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
-
MIN_LOCALDATETIME
public static final java.time.LocalDateTime MIN_LOCALDATETIME
epochal timestamp zero: 1.1.1970 00:00:00.000 UTC
-
-
Method Detail
-
toMillis
public static long toMillis(java.util.Date date)
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
public static Date toDate(java.util.Date date)
Converts ajava.util.Dateinto anorg.tentackle.misc.Date.- Parameters:
date- the util date- Returns:
- the sql date, null if date was null
-
toDate
public static Date toDate(java.util.Calendar cal)
Converts a Calendar into a org.tentackle.misc.Date.- Parameters:
cal- the calendar- Returns:
- the date, null if cal was null
-
toTime
public static Time toTime(java.util.Date date)
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
public static Time toTime(java.util.Calendar cal)
Converts a Calendar into a org.tentackle.misc.Time.- Parameters:
cal- the calendar- Returns:
- the time of day, null if cal was null
-
toTimestamp
public static Timestamp toTimestamp(java.util.Date date)
Converts a java.util.Date into a org.tentackle.misc.Timestamp.- Parameters:
date- the date- Returns:
- the timestamp, null if date was null
-
toTimestamp
public static Timestamp toTimestamp(java.util.Calendar cal)
Converts a Calendar into a org.tentackle.misc.Timestamp.- Parameters:
cal- the calendar- Returns:
- the timestamp, null if cal was null
-
today
public static Date today()
Gets the current date.- Returns:
- current Date
-
today
public static Date today(int dayOffset, int monthOffset, int yearOffset)
Gets the current date with an offset.- Parameters:
dayOffset- the offset in daysmonthOffset- the offset in monthsyearOffset- the offset in years- Returns:
- the date
-
daytime
public static Time daytime()
Gets the current time.- Returns:
- current Time
-
daytime
public static Time daytime(int secondOffset, int minuteOffset, int hourOffset)
Gets the current time with an offset.- Parameters:
hourOffset- the offset in hoursminuteOffset- the offset in minutessecondOffset- the offset in seconds- Returns:
- the time
-
now
public static Timestamp now(long offsetMillis)
Gets the current system time plus an optional offset.- Parameters:
offsetMillis- the offset to the current system time in milliseconds- Returns:
- current Timestamp
-
now
public static Timestamp now()
Gets the current system time.- Returns:
- current Timestamp
-
setMidnight
public static void setMidnight(java.util.GregorianCalendar greg)
Sets the given calendar to midnight.- Parameters:
greg- the calendar
-
convert2DigitYearTo4DigitYear
public static int convert2DigitYearTo4DigitYear(int year2, int currentYear4)Converts a 2-digit year to a 4-digit year.Finds the closest distance to the current, past or next century according to a given current year.
- Parameters:
year2- the 2-digit yearcurrentYear4- the current 4 digit year- Returns:
- the closest 4-digit year
-
convert2DigitYearTo4DigitYear
public static int convert2DigitYearTo4DigitYear(int year2)
Converts a 2-digit year to a 4-digit year.Finds the closest distance to the current, past or next century according to the current year.
- Parameters:
year2- the 2-digit year- Returns:
- the closest 4-digit year
-
-