Class Dates

java.lang.Object
org.zkoss.util.Dates

public class Dates extends Object
Utilities for java.util.Date
Author:
henrichen, tomyeh
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final Date
    add(Date when, TimeZone tz, int field, int amount)
    Date Arithmetic function.
    static final Date
    Given a date, a proper TimeZone, return the beginning date of the specified date and TimeZone.
    static final Date
    Return the beginning date of this month.
    static final Date
    Given a date, a proper TimeZone, return the beginning date of the month of the specified date and TimeZone.
    static final Date
    Return the beginning date of this year.
    static final Date
    Given a date, a proper TimeZone, return the beginning date of the month of the specified date and TimeZone.
    static final int
    Get the day of month of a date.
    static final Date
    endOfDate(Date when, TimeZone tz)
    Given a date, a proper TimeZone, return the last millisecond date of the specified date and TimeZone.
    static final Date
    Return the ending date of this month.
    static final Date
    Given a date, a proper TimeZone, return the ending date of the month of the specified date and TimeZone.
    static final Date
    Return the ending date of this year.
    static final Date
    endOfYear(Date when, TimeZone tz)
    Given a date, a proper TimeZone, return the ending date of the month of the specified date and TimeZone.
    static final long
    getTimezoneOffset(TimeZone timezone, Date date)
    Returns the timezone offset.
    static final boolean
    Whether the given date in the specified TimeZone is the first day of that month.
    static final boolean
    Whether the given date in the specified TimeZone is the last day of that month.
    static final boolean
    isRounded(Date date, int precision)
    Tests whether a date is rounded.
    static final int
    Get the year of a date in the specified locale.
    static final Date
    merge(Date datePart, Date timePart, TimeZone tz)
    merge the date part and time part of two specified dates into a date.
    static final int
    Get the month of a date.
    static final int
    Get the month of a date.
    static final Date
    now()
    Returns the current time without rounding.
    static final Date
    now(int precision)
    Returns the current time but rounding to the specified precision of milliseconds.
    static final Date
    Given a date, return the previous date of the given date (24 hrs before).
    static final long
    round(long time, int precision)
    Rounds a date represented in long to the specified precision of milliseconds.
    static final Date
    round(Date date, int precision)
    Truncates date to the nearest precision milliseconds.
    static final long
    subtract(Date date2, TimeZone tz, int field, Date date1)
    Date Arithmetic function (date2 - date1). subtract a date from another date, return the difference as the required fields.
    static final Date
    Returns today by setting time to 0:0:0.
    static final short
    Return the ending date of this year.
    static final short
    Given a date, a proper TimeZone, return the two month int. e.g. 1, 3, 5, 7, 9, 11.
    static final int
    Get the year of a date.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Dates

      public Dates()
  • Method Details

    • round

      public static final Date round(Date date, int precision)
      Truncates date to the nearest precision milliseconds. MS SQLServer2000 with only the maximum accuracy of 1/300 seconds would not be able to store up to one millisecond accuracy. That is, User must round the millisecond to some less precisions; or the data in that db would be inconsistent with that in memory. It is useful to store a Date object in a database. Without rounding, if you want to get it back and compare with the one in the memory. See now(int) for details.
      Parameters:
      precision - the divider of the precision(e.g. 10 for precision of 10 milliseconds;i.e. all millisecond less than 10 would be truncated)
      See Also:
    • round

      public static final long round(long time, int precision)
      Rounds a date represented in long to the specified precision of milliseconds.
      Parameters:
      time - the date represented in long.
      precision - the divider of the precision(e.g. 10 for precision of 10 milliseconds;i.e. all millisecond less than 10 would be truncated)
      See Also:
    • isRounded

      public static final boolean isRounded(Date date, int precision)
      Tests whether a date is rounded. It is mainly used for debugging.
    • now

      public static final Date now(int precision)
      Returns the current time but rounding to the specified precision of milliseconds. It is useful if you want to create the current time which will be stored in the database and want to compare it with something with what you store in the database. Otherwise, that you get back the one you store might be different, because the resolution of database timestamp is usually less than one millisecond, e.g., MS SQL: 0.003 second.

      If you don't cache it in the memory (remember entity beans always cache by the container), you don't need to round. If you are not sure, round it.

      See Also:
    • now

      public static final Date now()
      Returns the current time without rounding.
    • today

      public static final Date today()
      Returns today by setting time to 0:0:0.
    • previousDate

      public static final Date previousDate(Date when)
      Given a date, return the previous date of the given date (24 hrs before).
    • beginOfMonth

      public static final Date beginOfMonth()
      Return the beginning date of this month.
    • beginOfMonth

      public static final Date beginOfMonth(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the beginning date of the month of the specified date and TimeZone. If TimeZone is null, meaning use default TimeZone of the JVM.
    • endOfMonth

      public static final Date endOfMonth()
      Return the ending date of this month.
    • endOfMonth

      public static final Date endOfMonth(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the ending date of the month of the specified date and TimeZone. If TimeZone is null, meaning use default TimeZone of the JVM.
    • isEndOfMonth

      public static final boolean isEndOfMonth(Date when, TimeZone tz)
      Whether the given date in the specified TimeZone is the last day of that month. If TimeZone is null, meaning use default TimeZone of the JVM.
    • isBeginOfMonth

      public static final boolean isBeginOfMonth(Date when, TimeZone tz)
      Whether the given date in the specified TimeZone is the first day of that month. If TimeZone is null, meaning use default TimeZone of the JVM.
    • beginOfDate

      public static final Date beginOfDate(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the beginning date of the specified date and TimeZone. If TimeZone is null, meaning use Default TimeZone of the JVM.
    • endOfDate

      public static final Date endOfDate(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the last millisecond date of the specified date and TimeZone. If TimeZone is null, meaning use Default TimeZone of the JVM.
    • beginOfYear

      public static final Date beginOfYear()
      Return the beginning date of this year.
    • beginOfYear

      public static final Date beginOfYear(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the beginning date of the month of the specified date and TimeZone. If TimeZone is null, meaning use default TimeZone of the JVM.
    • endOfYear

      public static final Date endOfYear()
      Return the ending date of this year.
    • endOfYear

      public static final Date endOfYear(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the ending date of the month of the specified date and TimeZone. If TimeZone is null, meaning use default TimeZone of the JVM.
    • twoMonthShort

      public static final short twoMonthShort()
      Return the ending date of this year.
    • twoMonthShort

      public static final short twoMonthShort(Date when, TimeZone tz)
      Given a date, a proper TimeZone, return the two month int. e.g. 1, 3, 5, 7, 9, 11. If TimeZone is null, meaning use default TimeZone of the JVM.
    • yearOfDate

      public static final int yearOfDate(Date when, TimeZone tz)
      Get the year of a date.
      Parameters:
      when - The date.
      tz - The time zone; if null, the current time zone is assumed.
      See Also:
    • localizedYearOfDate

      public static final int localizedYearOfDate(Date when, Locale locale, TimeZone tz)
      Get the year of a date in the specified locale.

      Currently, only Locale.ZH_TW is supported, i.e., "year - 1911" and it's may be less than 0. Otherwise, it is the same as yearOfDate(java.util.Date, java.util.TimeZone).

      Parameters:
      when - The date.
      locale - the locale; if null, the current locale is assumed.
      tz - The time zone; if null, the current time zone is assumed.
      See Also:
    • monthOfDate

      public static final int monthOfDate(Date when, TimeZone tz)
      Get the month of a date. The first month of the year is JANUARY which is 0.
      Parameters:
      when - The date.
      tz - The time zone; if null, the current time zone is assumed.
    • monthOfDatePlus1

      public static final int monthOfDatePlus1(Date when, TimeZone tz)
      Get the month of a date. The first month of the year is JANUARY which is 1.
      Parameters:
      when - The date.
      tz - The time zone; if null, the current time zone is assumed.
    • dayMonthOfDate

      public static final int dayMonthOfDate(Date when, TimeZone tz)
      Get the day of month of a date. The first day of the month has value 1.
      Parameters:
      when - The date.
      tz - The time zone; if null, the current time zone is assumed.
    • add

      public static final Date add(Date when, TimeZone tz, int field, int amount)
      Date Arithmetic function. Adds the specified (signed) amount of time to the given date, based on the calendar's rules.
      Parameters:
      when - The based date.
      tz - The time zone; if null, the current time zone is assumed.
      field - The time field.
      amount - The amount of date or time to be added to the field.
    • subtract

      public static final long subtract(Date date2, TimeZone tz, int field, Date date1)
      Date Arithmetic function (date2 - date1). subtract a date from another date, return the difference as the required fields. E.g. if specified Calendar.Date, the smaller range of fields is ignored and this method return the difference of days.
      Parameters:
      date2 - The date2.
      tz - The time zone.
      field - The time field; e.g., Calendar.DATE, Calendar.YEAR, it's default value is Calendar.DATE
      date1 - The date1.
    • merge

      public static final Date merge(Date datePart, Date timePart, TimeZone tz)
      merge the date part and time part of two specified dates into a date.
      Parameters:
      datePart - The date part date.
      timePart - The time part date.
      tz - The time zone.
    • getTimezoneOffset

      public static final long getTimezoneOffset(TimeZone timezone, Date date)
      Returns the timezone offset.
      Parameters:
      timezone - The time zone.
      date -
      Returns:
      long