Package net.time4j

Class DayPeriod

java.lang.Object
net.time4j.DayPeriod

public final class DayPeriod extends Object

Represents a period or part of a day usually in minute precision as formattable extension to PlainTime.

The i18n-module is necessary to exploit the full functionality otherwise this class will just fall back to AM/PM only. Most non-English speaking countries completely ignore the notation of AM/PM but know day periods with finer granularity such as "in the morning", "evening" etc. Users can get extensive format support for day periods via the CLDR-pattern symbols b or B. The combination of half-day-hours with a day period is especially useful. Example of usage:

     ChronoFormatter<PlainTime> f =
       ChronoFormatter.ofTimePattern("h:mm BBBB", PatternType.CLDR, Locale.ENGLISH);
     System.out.println(f.format(PlainTime.of(17, 15))); // output => 5:15 in the afternoon
 
Since:
3.13/4.10
Author:
Meno Hochschild
  • Method Details

    • of

      public static DayPeriod of(Locale locale)

      Creates an instance based on locale-specific predefined data.

      If given locale does not point to any predefined data then Time4J will fall back to AM/PM.

      Parameters:
      locale - contains the language setting
      Returns:
      locale-specific instance
      Since:
      3.13/4.10
    • of

      public static DayPeriod of(Map<PlainTime,​String> timeToLabels)

      Creates an instance based on user-defined data.

      Parameters:
      timeToLabels - map containing the day-periods where the keys represent starting points and the values represent the associated labels intended for representation
      Returns:
      user-specific instance
      Throws:
      IllegalArgumentException - if given map is empty or contains empty values
      Since:
      3.13/4.10
    • fixed

      public ChronoFunction<ChronoDisplay,​String> fixed()

      Equivalent to fixed(TextWidth.WIDE, OutputContext.FORMAT).

      Returns:
      fixed textual representation of day period as function applicable on PlainTime etc.
      Since:
      3.13/4.10
      See Also:
      fixed(TextWidth, OutputContext)
    • fixed

      public ChronoFunction<ChronoDisplay,​String> fixed(TextWidth width, OutputContext outputContext)

      Represents a fixed day period (am / pm / midnight / noon).

      The function returned can be applied on either PlainTime or PlainTimestamp. Otherwise it throws a ChronoException if an instance of PlainTime cannot be found. If this day period was not created for a locale then the function will just return one of the literals "am", "pm", "midnight" or "noon".

      Parameters:
      width - determines the text width
      outputContext - determines in which context to format
      Returns:
      fixed textual representation of day period as function applicable on PlainTime etc.
      Since:
      3.13/4.10
    • approximate

      public ChronoFunction<ChronoDisplay,​String> approximate()

      Equivalent to approximate(TextWidth.WIDE, OutputContext.FORMAT).

      Returns:
      approximate textual representation of day period as function applicable on PlainTime etc.
      Since:
      3.13/4.10
      See Also:
      approximate(TextWidth, OutputContext)
    • approximate

      public ChronoFunction<ChronoDisplay,​String> approximate(TextWidth width, OutputContext outputContext)

      Represents a flexible day period (in the afternoon, at night etc).

      The function returned can be applied on either PlainTime or PlainTimestamp. Otherwise it throws a ChronoException if an instance of PlainTime cannot be found. If no suitable text can be determined then the function falls back to AM/PM.

      Parameters:
      width - determines the text width
      outputContext - determines in which context to format
      Returns:
      approximate textual representation of day period as function applicable on PlainTime etc.
      Since:
      3.13/4.10
    • getStart

      public PlainTime getStart(PlainTime context)

      Determines the start of the day period which covers given clock time.

      Parameters:
      context - the clock time a day period is searched for
      Returns:
      start of day period around given clock time, inclusive
      Since:
      3.13/4.10
      See Also:
      getEnd(PlainTime)
    • getEnd

      public PlainTime getEnd(PlainTime context)

      Determines the end of the day period which covers given clock time.

      Parameters:
      context - the clock time a day period is searched for
      Returns:
      end of day period around given clock time, exclusive
      Since:
      3.13/4.10
      See Also:
      getStart(PlainTime)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      For debugging purposes.
      Overrides:
      toString in class Object
      Returns:
      String