Module platform

Class Hour

All Implemented Interfaces:
Serializable, Comparable, MonthConstants, TimePeriod

public class Hour extends RegularTimePeriod implements Serializable
Represents an hour in a specific day. This class is immutable, which is a requirement for all RegularTimePeriod subclasses.
See Also:
  • Field Details

    • FIRST_HOUR_IN_DAY

      public static final int FIRST_HOUR_IN_DAY
      Useful constant for the first hour in the day.
      See Also:
    • LAST_HOUR_IN_DAY

      public static final int LAST_HOUR_IN_DAY
      Useful constant for the last hour in the day.
      See Also:
  • Constructor Details

    • Hour

      public Hour()
      Constructs a new Hour, based on the system date/time. The time zone and locale are determined by the calendar returned by RegularTimePeriod.getCalendarInstance().
    • Hour

      public Hour(int hour, Day day)
      Constructs a new Hour. The time zone and locale are determined by the calendar returned by RegularTimePeriod.getCalendarInstance().
      Parameters:
      hour - the hour (in the range 0 to 23).
      day - the day (null not permitted).
    • Hour

      public Hour(int hour, int day, int month, int year)
      Creates a new hour. The time zone and locale are determined by the calendar returned by RegularTimePeriod.getCalendarInstance().
      Parameters:
      hour - the hour (0-23).
      day - the day (1-31).
      month - the month (1-12).
      year - the year (1900-9999).
    • Hour

      public Hour(Date time)
      Constructs a new instance, based on the supplied date/time. The time zone and locale are determined by the calendar returned by RegularTimePeriod.getCalendarInstance().
      Parameters:
      time - the date-time (null not permitted).
      See Also:
    • Hour

      public Hour(Date time, TimeZone zone, Locale locale)
      Constructs a new instance, based on the supplied date/time evaluated in the specified time zone.
      Parameters:
      time - the date-time (null not permitted).
      zone - the time zone (null not permitted).
      locale - the locale (null not permitted).
    • Hour

      public Hour(Date time, Calendar calendar)
      Constructs a new instance, based on a particular date/time. The time zone and locale are determined by the calendar parameter.
      Parameters:
      time - the date/time (null not permitted).
      calendar - the calendar to use for calculations (null not permitted).
  • Method Details

    • getHour

      public int getHour()
      Returns the hour.
      Returns:
      The hour (0 <= hour <= 23).
    • getDay

      public Day getDay()
      Returns the day in which this hour falls.
      Returns:
      The day.
    • getYear

      public int getYear()
      Returns the year in which this hour falls.
      Returns:
      The year.
    • getMonth

      public int getMonth()
      Returns the month in which this hour falls.
      Returns:
      The month.
    • getDayOfMonth

      public int getDayOfMonth()
      Returns the day-of-the-month in which this hour falls.
      Returns:
      The day-of-the-month.
    • getFirstMillisecond

      public long getFirstMillisecond()
      Returns the first millisecond of the hour. This will be determined relative to the time zone specified in the constructor, or in the calendar instance passed in the most recent call to the peg(Calendar) method.
      Specified by:
      getFirstMillisecond in class RegularTimePeriod
      Returns:
      The first millisecond of the hour.
      See Also:
    • getLastMillisecond

      public long getLastMillisecond()
      Returns the last millisecond of the hour. This will be determined relative to the time zone specified in the constructor, or in the calendar instance passed in the most recent call to the peg(Calendar) method.
      Specified by:
      getLastMillisecond in class RegularTimePeriod
      Returns:
      The last millisecond of the hour.
      See Also:
    • peg

      public void peg(Calendar calendar)
      Recalculates the start date/time and end date/time for this time period relative to the supplied calendar (which incorporates a time zone).
      Specified by:
      peg in class RegularTimePeriod
      Parameters:
      calendar - the calendar (null not permitted).
    • previous

      public RegularTimePeriod previous()
      Returns the hour preceding this one. No matter what time zone and locale this instance was created with, the returned instance will use the default calendar for time calculations, obtained with RegularTimePeriod.getCalendarInstance().
      Specified by:
      previous in class RegularTimePeriod
      Returns:
      The hour preceding this one.
    • next

      public RegularTimePeriod next()
      Returns the hour following this one. No matter what time zone and locale this instance was created with, the returned instance will use the default calendar for time calculations, obtained with RegularTimePeriod.getCalendarInstance().
      Specified by:
      next in class RegularTimePeriod
      Returns:
      The hour following this one.
    • getSerialIndex

      public long getSerialIndex()
      Returns a serial index number for the hour.
      Specified by:
      getSerialIndex in class RegularTimePeriod
      Returns:
      The serial index number.
    • getFirstMillisecond

      public long getFirstMillisecond(Calendar calendar)
      Returns the first millisecond of the hour.
      Specified by:
      getFirstMillisecond in class RegularTimePeriod
      Parameters:
      calendar - the calendar/timezone (null not permitted).
      Returns:
      The first millisecond.
      Throws:
      NullPointerException - if calendar is null.
      See Also:
    • getLastMillisecond

      public long getLastMillisecond(Calendar calendar)
      Returns the last millisecond of the hour.
      Specified by:
      getLastMillisecond in class RegularTimePeriod
      Parameters:
      calendar - the calendar/timezone (null not permitted).
      Returns:
      The last millisecond.
      Throws:
      NullPointerException - if calendar is null.
      See Also:
    • equals

      public boolean equals(Object obj)
      Tests the equality of this object against an arbitrary Object.

      This method will return true ONLY if the object is an Hour object representing the same hour as this instance.

      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare (null permitted).
      Returns:
      true if the hour and day value of the object is the same as this.
    • toString

      public String toString()
      Returns a string representation of this instance, for debugging purposes.
      Overrides:
      toString in class RegularTimePeriod
      Returns:
      A string.
    • hashCode

      public int hashCode()
      Returns a hash code for this object instance. The approach described by Joshua Bloch in "Effective Java" has been used here:

      http://developer.java.sun.com/developer/Books/effectivejava /Chapter3.pdf

      Overrides:
      hashCode in class Object
      Returns:
      A hash code.
    • compareTo

      public int compareTo(Object o1)
      Returns an integer indicating the order of this Hour object relative to the specified object: negative == before, zero == same, positive == after.
      Specified by:
      compareTo in interface Comparable
      Parameters:
      o1 - the object to compare.
      Returns:
      negative == before, zero == same, positive == after.
    • parseHour

      public static Hour parseHour(String s)
      Creates an Hour instance by parsing a string. The string is assumed to be in the format "YYYY-MM-DD HH", perhaps with leading or trailing whitespace.
      Parameters:
      s - the hour string to parse.
      Returns:
      null if the string is not parseable, the hour otherwise.