Class WDate

java.lang.Object
eu.webtoolkit.jwt.WDate
All Implemented Interfaces:
java.lang.Comparable<WDate>

public class WDate
extends java.lang.Object
implements java.lang.Comparable<WDate>
Class which holds a date on the gregorian calendar, specified as day/month/year. A valid date may be specified by year, month, and day of month (using the WDate(int, int, int) constructor, or the setDate(int year, int month, int day) method). When attempting to specify an invalid date (with an impossible combination of year/month/date) an IllegalArgumentException will be thrown. The class provides a flexible way for converting between strings and dates. Use toString() to convert to strings, and fromString() for parsing strings. Both methods take a format string, and the same format syntax is supported by both methods. Simple operations are supported to compare dates, or to calculate with dates.
  • Constructor Summary

    Constructors 
    Constructor Description
    WDate​(int year, int month, int day)
    Specify a date by year, month (1-12), and day (1-31) When the date is invalid, an IllegalArgumentException is thrown.
    WDate​(int year, int month, int day, int hour, int minute, int second)
    Set a date by year, month (1-12), day (1-31), hour (0-23), minute (0-59), second (0 - 59) When the date is invalid, an IllegalArgumentException is thrown.
    WDate​(int year, int month, int day, int hour, int minute, int second, int millisecond)
    Set a date by year, month (1-12), day (1-31), hour (0-23), minute (0-59), second (0 - 59), millisecond (0 - 999) When the date is invalid, an IllegalArgumentException is thrown.
    WDate​(java.util.Date date)
    Specify a date by a Date object.
  • Method Summary

    Modifier and Type Method Description
    WDate addDays​(int ndays)
    Add days to a date.
    WDate addMilliseconds​(int nMilliseconds)
    Adds milliseconds.
    WDate addMonths​(int nmonths)
    Add months to a date.
    WDate addSeconds​(int nSeconds)
    Adds seconds.
    WDate addYears​(int nyears)
    Add years to a date.
    boolean after​(WDate when)
    Tests if this date is after the specified date.
    boolean before​(WDate when)
    Tests if this date is after the specified date.
    int compareTo​(WDate o)
    Compares this WDate object with the specified WDate object for order.
    boolean equals​(java.lang.Object other)
    Compares two dates for equality.
    static WDate fromJulianDay​(int jd)
    Converts a Julian Day jd to a WDate.
    static WDate fromString​(java.lang.String text)
    Parse a WString to a date using a default format.
    static WDate fromString​(java.lang.String text, java.lang.String format)
    Parse a String to a date using a specified format.
    static WDate getCurrentDate()
    Construct a date for the current client date.
    static WDate getCurrentServerDate()
    Construct a date for the current server date.
    java.util.Date getDate()
    Returns the internal Date object.
    int getDay()
    Day of month (1-31)
    int getDayOfWeek()
    Day of week (1-7)
    int getDaysTo​(WDate date)
    Returns the number of days from this date to date.
    static java.lang.String getDefaultFormat()
    Returns the default date format.
    int getHour()
    Hour (0-24)
    static java.lang.String getLongDayName​(int weekday)
    Returns the long day name.
    static java.lang.String getLongMonthName​(int month)
    Returns the long month name.
    int getMillisecond()
    Millisecond (0-999)
    long getMillisecondsTo​(WDate d)
    Returns the difference between to time values (in milliseconds).
    int getMinute()
    Minute (0-59)
    int getMonth()
    Month (1-12)
    int getSecond()
    Second (0-59)
    int getSecondsTo​(WDate d)
    Returns the difference between to time values (in seconds).
    static java.lang.String getShortDayName​(int weekday)
    Returns the short day name.
    static java.lang.String getShortMonthName​(int month)
    Returns the short month name.
    WTime getTime()
    Gets this date's time
    java.lang.String getTimeTo​(WDate other, int minValue)
    Returns the difference between two WDate values (as text).
    int getYear()
    Year
    int hashCode()
    Returns a hash code value for the object.
    void setDate​(int year, int month, int day)
    Set a date by year, month (1-12), day (1-31) When the new date is invalid, an IllegalArgumentException is thrown.
    void setDate​(int year, int month, int day, int hour, int minute, int second)
    Set a date by year, month (1-12), and day (1-31), hour (0-23), minute (0-59), second (0 - 59).
    void setDate​(int year, int month, int day, int hour, int minute, int second, int millisecond)
    Set a date by year, month (1-12), day (1-31), hour (0-23), minute (0-59), second (0 - 59), millisecond (0 - 999) When the new date is invalid, an IllegalArgumentException is thrown.
    void setTime​(int hour, int minute)
    Set this date's time by hour (0-23) and minute (0-59).
    void setTime​(int hour, int minute, int second)
    Set this date's time by hour (0-23), minute (0-59) and second (0 - 59).
    void setTime​(int hour, int minute, int second, int millisecond)
    Set this date's time by hour (0-23), minute (0-59), second (0 - 59) and millisecond (0 - 999).
    void setTime​(WTime time)
    Sets this date's time by WTime When the new date is invalid, an IllegalArgumentException is thrown.
    static void setTimezone​(java.util.TimeZone timeZone)
    Sets default timezone If null, then the default (local) timezone will be used.
    int toJulianDay()
    Converts the date to a Julian day.
    java.lang.String toString()
    Format this date to a String using a default format.
    java.lang.String toString​(java.lang.String format)
    Format this date to a WString using a specified format.
    java.lang.String toString​(java.lang.String format, boolean localized)  

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

  • Method Details

    • setTimezone

      public static void setTimezone​(java.util.TimeZone timeZone)
      Sets default timezone If null, then the default (local) timezone will be used. You may want to change this to e.g. GMT to not be affected by oddities in the local time zone, such as for example missing 'time'. (see e.g. http://stackoverflow.com/questions/6140624/java-missing-28-seconds) The default is null.
    • setDate

      public void setDate​(int year, int month, int day, int hour, int minute, int second, int millisecond)
      Set a date by year, month (1-12), day (1-31), hour (0-23), minute (0-59), second (0 - 59), millisecond (0 - 999) When the new date is invalid, an IllegalArgumentException is thrown.
      See Also:
      WDate(int year, int month, int day, int hour, int minute, int second, int millisecond), getYear(), getMonth(), getDay(), getHour(), getMinute(), getSecond(), getMillisecond()
    • setDate

      public void setDate​(int year, int month, int day)
      Set a date by year, month (1-12), day (1-31) When the new date is invalid, an IllegalArgumentException is thrown.
      See Also:
      setDate(int, int, int, int, int, int)
    • setDate

      public void setDate​(int year, int month, int day, int hour, int minute, int second)
      Set a date by year, month (1-12), and day (1-31), hour (0-23), minute (0-59), second (0 - 59). When the new date is invalid, an IllegalArgumentException is thrown.
      See Also:
      setDate(int, int, int, int, int, int, int)
    • setTime

      public void setTime​(int hour, int minute, int second, int millisecond)
      Set this date's time by hour (0-23), minute (0-59), second (0 - 59) and millisecond (0 - 999). When the new date is invalid, an IllegalArgumentException is thrown.
    • setTime

      public void setTime​(int hour, int minute, int second)
      Set this date's time by hour (0-23), minute (0-59) and second (0 - 59). When the new date is invalid, an IllegalArgumentException is thrown.
    • setTime

      public void setTime​(int hour, int minute)
      Set this date's time by hour (0-23) and minute (0-59). When the new date is invalid, an IllegalArgumentException is thrown.
    • setTime

      public void setTime​(WTime time)
      Sets this date's time by WTime When the new date is invalid, an IllegalArgumentException is thrown.
    • getTime

      public WTime getTime()
      Gets this date's time
      Returns:
      WTime object
    • addSeconds

      public WDate addSeconds​(int nSeconds)
      Adds seconds. Returns a time that is nSeconds seconds later than this time. Negative values for nSeconds will result in a time that is as many seconds earlier.
    • addMilliseconds

      public WDate addMilliseconds​(int nMilliseconds)
      Adds milliseconds. Returns a time that is nMilliseconds milliseconds later than this time. Negative values for nMilliseconds will result in a time that is as many seconds earlier.
    • addDays

      public WDate addDays​(int ndays)
      Add days to a date. Returns a date that is ndays later than this date. Negative values for ndays will result in a date that is as many days earlier.
      See Also:
      addMonths(int), addYears(int)
    • addMonths

      public WDate addMonths​(int nmonths)
      Add months to a date. Returns a date that is the same day of the month, but nmonths later than this date. Negative values for nmonths will result in a date that is as many months earlier.
      See Also:
      addDays(int), addYears(int)
    • addYears

      public WDate addYears​(int nyears)
      Add years to a date. Returns a date that is nyears later than this date. Negative values for nyears will result in a date that is as many years earlier.
      See Also:
      addDays(int), addMonths(int)
    • getYear

      public int getYear()
      Year
    • getMonth

      public int getMonth()
      Month (1-12)
    • getDay

      public int getDay()
      Day of month (1-31)
    • getHour

      public int getHour()
      Hour (0-24)
    • getMinute

      public int getMinute()
      Minute (0-59)
    • getSecond

      public int getSecond()
      Second (0-59)
    • getMillisecond

      public int getMillisecond()
      Millisecond (0-999)
    • getSecondsTo

      public int getSecondsTo​(WDate d)
      Returns the difference between to time values (in seconds).
    • getMillisecondsTo

      public long getMillisecondsTo​(WDate d)
      Returns the difference between to time values (in milliseconds).
    • getDayOfWeek

      public int getDayOfWeek()
      Day of week (1-7)
    • getDaysTo

      public int getDaysTo​(WDate date)
      Returns the number of days from this date to date.
    • getTimeTo

      public java.lang.String getTimeTo​(WDate other, int minValue)
      Returns the difference between two WDate values (as text). This returns a textual representation of the approximate difference between this time and other . The textual representation returns the difference as a number of seconds, minutes, hours, days, weeks, months, or years, using the coarsest unit that is more than \p minValue.
      See Also:
      getDaysTo(WDate), getSecondsTo(WDate)
    • before

      public boolean before​(WDate when)
      Tests if this date is after the specified date.
      Parameters:
      when - - a date.
      Returns:
      true if and only if the instant represented by this Date object is strictly earlier than the instant represented by when; false otherwise.
    • after

      public boolean after​(WDate when)
      Tests if this date is after the specified date.
      Parameters:
      when - - a date.
      Returns:
      true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.
    • equals

      public boolean equals​(java.lang.Object other)
      Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object. Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.
      Overrides:
      equals in class java.lang.Object
    • getDefaultFormat

      public static java.lang.String getDefaultFormat()
      Returns the default date format.
    • getCurrentDate

      public static WDate getCurrentDate()
      Construct a date for the current client date. This method uses browser information to retrieve the date that is configured in the client.
    • getCurrentServerDate

      public static WDate getCurrentServerDate()
      Construct a date for the current server date. This method returns the date as indicated by the system clock of the server.
    • getShortDayName

      public static java.lang.String getShortDayName​(int weekday)
      Returns the short day name. Results (for given weekDay) are (default English):
      "Mon" (1),
      "Tue" (2),
      "Wed" (3),
      "Thu" (4),
      "Fri" (5),
      "Sat" (6),
      "Sun" (7). The result is affected by localization using the "Wt.WDate.Mon" to "Wt.WDate.Sun" keys.
      See Also:
      getLongDayName(int)
    • getShortMonthName

      public static java.lang.String getShortMonthName​(int month)
      Returns the short month name. Results (for given month) are (default English):
      "Jan" (1),
      "Feb" (2),
      "Mar" (3),
      "Apr" (4),
      "May" (5),
      "Jun" (6),
      "Jul" (7),
      "Aug" (8),
      "Sep" (9),
      "Oct" (10),
      "Nov" (11),
      "Dec" (12). The result is affected by localization using the "Wt.WDate.Jan" to "Wt.WDate.Dec" keys.
      See Also:
      getLongMonthName(int)
    • getLongDayName

      public static java.lang.String getLongDayName​(int weekday)
      Returns the long day name. Results (for given weekDay) are (default English):
      "Monday" (1),
      "Tuesday" (2),
      "Wednesday" (3),
      "Thursday" (4),
      "Friday" (5),
      "Saturday" (6),
      "Sunday" (7). The result is affected by localization using the "Wt.WDate.Monday" to "Wt.WDate.Sunday" keys.
      See Also:
      getShortDayName(int)
    • getLongMonthName

      public static java.lang.String getLongMonthName​(int month)
      Returns the long month name. Results (for given month) are (default English):
      "January" (1),
      "February" (2),
      "March" (3),
      "April" (4),
      "May" (5),
      "June" (6),
      "July" (7),
      "August" (8),
      "September" (9),
      "October" (10),
      "November" (11),
      "December" (12). The result is affected by localization using the "Wt.WDate.January" to "Wt.WDate.December" keys.
      See Also:
      getShortDayName(int)
    • fromString

      public static WDate fromString​(java.lang.String text)
      Parse a WString to a date using a default format. The default format is "ddd MMM d yyyy". For example, a date specified as: "Wed Aug 29 2007" will be parsed as a date that equals a date constructed as: WDate d = new WDate(2007,8,29); When the date could not be parsed or is not valid, null is returned.
      See Also:
      fromString(String, String)
    • fromString

      public static WDate fromString​(java.lang.String text, java.lang.String format)
      Parse a String to a date using a specified format. The format follows the same syntax as used by toString(String format). When the date could not be parsed or is not valid, null is returned.
      See Also:
      toString(String format)
    • toJulianDay

      public int toJulianDay()
      Converts the date to a Julian day.
      See Also:
      fromJulianDay(int)
    • toString

      public java.lang.String toString()
      Format this date to a String using a default format. The default format is "ddd MMM d yyyy".
      Overrides:
      toString in class java.lang.Object
      See Also:
      toString(String format), fromString(String)
    • toString

      public java.lang.String toString​(java.lang.String format)
      Format this date to a WString using a specified format. The format is a string interpreted by SimpleDateFormat.
    • toString

      public java.lang.String toString​(java.lang.String format, boolean localized)
    • fromJulianDay

      public static WDate fromJulianDay​(int jd)
      Converts a Julian Day jd to a WDate.
      See Also:
      toJulianDay()
    • getDate

      public java.util.Date getDate()
      Returns the internal Date object.
    • hashCode

      public int hashCode()
      Returns a hash code value for the object.
      Overrides:
      hashCode in class java.lang.Object
    • compareTo

      public int compareTo​(WDate o)
      Compares this WDate object with the specified WDate object for order.
      Specified by:
      compareTo in interface java.lang.Comparable<WDate>