Package org.zkoss.util
Class Dates
java.lang.Object
org.zkoss.util.Dates
Utilities for java.util.Date
- Author:
- henrichen, tomyeh
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final DateDate Arithmetic function.static final DatebeginOfDate(Date when, TimeZone tz) Given a date, a proper TimeZone, return the beginning date of the specified date and TimeZone.static final DateReturn the beginning date of this month.static final DatebeginOfMonth(Date when, TimeZone tz) Given a date, a proper TimeZone, return the beginning date of the month of the specified date and TimeZone.static final DateReturn the beginning date of this year.static final DatebeginOfYear(Date when, TimeZone tz) Given a date, a proper TimeZone, return the beginning date of the month of the specified date and TimeZone.static final intdayMonthOfDate(Date when, TimeZone tz) Get the day of month of a date.static final DateGiven a date, a proper TimeZone, return the last millisecond date of the specified date and TimeZone.static final DateReturn the ending date of this month.static final DateendOfMonth(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 DateReturn the ending date of this year.static final DateGiven a date, a proper TimeZone, return the ending date of the month of the specified date and TimeZone.static final longgetTimezoneOffset(TimeZone timezone, Date date) Returns the timezone offset.static final booleanisBeginOfMonth(Date when, TimeZone tz) Whether the given date in the specified TimeZone is the first day of that month.static final booleanisEndOfMonth(Date when, TimeZone tz) Whether the given date in the specified TimeZone is the last day of that month.static final booleanTests whether a date is rounded.static final intlocalizedYearOfDate(Date when, Locale locale, TimeZone tz) Get the year of a date in the specified locale.static final Datemerge the date part and time part of two specified dates into a date.static final intmonthOfDate(Date when, TimeZone tz) Get the month of a date.static final intmonthOfDatePlus1(Date when, TimeZone tz) Get the month of a date.static final Datenow()Returns the current time without rounding.static final Datenow(int precision) Returns the current time but rounding to the specified precision of milliseconds.static final DatepreviousDate(Date when) Given a date, return the previous date of the given date (24 hrs before).static final longround(long time, int precision) Rounds a date represented in long to the specified precision of milliseconds.static final DateTruncates date to the nearest precision milliseconds.static final longDate Arithmetic function (date2 - date1). subtract a date from another date, return the difference as the required fields.static final Datetoday()Returns today by setting time to 0:0:0.static final shortReturn the ending date of this year.static final shorttwoMonthShort(Date when, TimeZone tz) Given a date, a proper TimeZone, return the two month int. e.g. 1, 3, 5, 7, 9, 11.static final intyearOfDate(Date when, TimeZone tz) Get the year of a date.
-
Constructor Details
-
Dates
public Dates()
-
-
Method Details
-
round
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. Seenow(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
Tests whether a date is rounded. It is mainly used for debugging. -
now
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
Returns the current time without rounding. -
today
Returns today by setting time to 0:0:0. -
previousDate
Given a date, return the previous date of the given date (24 hrs before). -
beginOfMonth
Return the beginning date of this month. -
beginOfMonth
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
Return the ending date of this month. -
endOfMonth
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
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
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
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
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
Return the beginning date of this year. -
beginOfYear
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
Return the ending date of this year. -
endOfYear
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
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
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
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
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
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
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
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
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.DATEdate1- The date1.
-
merge
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
Returns the timezone offset.- Parameters:
timezone- The time zone.date-- Returns:
- long
-