Class ZmanimFormatter

java.lang.Object
com.kosherjava.zmanim.util.ZmanimFormatter

public class ZmanimFormatter extends Object
A class used to format both Instant times generated by the Zmanim package as well as Duration used for temporal hours / sha'os zmaniyos. For example the AstronomicalCalendar.getTemporalHour() returns the length of the hour as a Duration.
Author:
© Eliyahu Hershfeld 2004 - 2026
  • Field Details

  • Constructor Details

    • ZmanimFormatter

      public ZmanimFormatter(ZoneId zoneId)
      Default constructor that uses the format "h:mm:ss" for dates and "0:00:00" for Durations.
      Parameters:
      zoneId - the ZoneId Object.
    • ZmanimFormatter

      public ZmanimFormatter(int timeFormat, DateTimeFormatter dateTimeFormatter, ZoneId zoneId)
      ZmanimFormatter constructor using various formatting options.
      Parameters:
      timeFormat - The formatting style to use for Duration used to represent shaos zmaniyos. The formatting will be as follows.
      dateTimeFormatter - the DateTimeFormatter Object.
      zoneId - the ZoneId Object.
  • Method Details

    • getZoneId

      public ZoneId getZoneId()
      Method to return the ZoneId.
      Returns:
      the ZoneId
    • setZoneId

      public void setZoneId(ZoneId zoneId)
      Method to set the ZoneId.
      Parameters:
      zoneId - the ZoneId to set
    • setTimeFormat

      public void setTimeFormat(int format)
      Sets the time format to use for formatting Durations.
      Parameters:
      format - the format constant to use. See ZmanimFormatter(int, DateTimeFormatter, ZoneId) for documentation on the formats.
    • setDateTimeFormatter

      public void setDateTimeFormatter(DateTimeFormatter dateTimeFormatter)
      Sets the DateTimeFormatter Object.
      Parameters:
      dateTimeFormatter - the DateTimeFormatter Object to set.
    • getDateTimeFormatter

      returns the DateTimeFormatter Object.
      Returns:
      the DateTimeFormatter Object.
    • setSettings

      private void setSettings(boolean prependZeroHours, boolean useSeconds, boolean useMillis)
      Sets various format settings.
      Parameters:
      prependZeroHours - if to prepend a zero for single digit hours (so that 1 o'clock is displayed as 01).
      useSeconds - should seconds be used in the time format.
      useMillis - should milliseconds be used in formatting time.
    • format

      public String format(Duration duration)
      A method that formats a Duration into a String format of "00:00", "00:00:00" or "00:00:00.000" depending on the settings of the formatter. See the documentation on ZmanimFormatter(int, DateTimeFormatter, ZoneId) for details.
      Parameters:
      duration - The Duration to format.
      Returns:
      String The formatted String.
    • formatInstant

      public String formatInstant(Instant instant, ZoneId zoneId)
      Formats an Instant using this class's getDateTimeFormatter().
      Parameters:
      instant - the Instant to format.
      zoneId - the ZoneId used to help format based on the Instant's DST and other settings.
      Returns:
      the formatted String
      See Also:
    • formatXSDateTime

      public String formatXSDateTime(Instant instant)
      Format the Instant using the format "yyyy-MM-dd'T'HH:mm:ssXXX".
      Parameters:
      instant - the Instant to format.
      Returns:
      the Instant formatted using the format "yyyy-MM-dd'T'HH:mm:ssXXX".
    • formatXSDDurationTime

      This returns the xml representation of an xsd:duration object. This simply uses Duration.toString() that formats it in the standard XSD ISO-8601 (e.g., "PT1H30M")
      Parameters:
      duration - the duration to format
      Returns:
      the xsd:duration ISO-8601 formatted String
    • toXML

      public static String toXML(AstronomicalCalendar astronomicalCalendar)
      A method that returns an XML formatted String representing the serialized Object. The format used is:
       <AstronomicalTimes date="1969-02-08" type="com.kosherjava.zmanim.AstronomicalCalendar"
              algorithm="US Naval Almanac Algorithm" location="Montreal, Quebec" latitude="45.497" longitude="-73.63"
              elevation="85.0" timeZoneName="Eastern Standard Time" timeZoneID="America/New_York" timeZoneOffset="-5">
          <SeaLevelSunset>1969-02-08T17:11:26-05:00</SeaLevelSunset>
          <TemporalHour>PT50M23.259S</TemporalHour>
          ...
        </AstronomicalTimes>
      
      If a zman does not occur, the value "N/A" will be returned. Note that the output uses the xsd:dateTime format for times such as sunrise, and xsd:duration format for times that are a duration such as the length of a temporal hour. The output of this method is returned by the toString.
      Parameters:
      astronomicalCalendar - the AstronomicalCalendar Object
      Returns:
      The XML String formatted as described above.
      TODO:
      Add proper schema, and support for nulls. XSD duration (for solar hours), should probably return nil and not P.
    • toJSON

      public static String toJSON(AstronomicalCalendar astronomicalCalendar)
      A method that returns a JSON formatted String representing the serialized Object. The format used is:
      {
         "metadata":{
           "date":"1969-02-08",
           "type":"com.kosherjava.zmanim.AstronomicalCalendar",
           "algorithm":"US Naval Almanac Algorithm",
           "location":"Montreal, Quebec",
           "latitude":"45.497",
           "longitude":"-73.63",
           "elevation":"85.0",
           "timeZoneName":"Eastern Standard Time",
           "timeZoneID":"America/New_York",
           "timeZoneOffset":"-5"},
         "AstronomicalTimes":{
          "SeaLevelSunset":"1969-02-08T17:11:26-05:00",
          "TemporalHour":"PT50M23.259S"
          ...
          }
      }
      
      Note that the output uses the xsd:dateTime format for times such as sunrise, and xsd:duration format for times that are a duration such as the length of a temporal hour. If a zman does not occur, the value "N/A" will be returned.
      Parameters:
      astronomicalCalendar - the AstronomicalCalendar Object
      Returns:
      The JSON String formatted as described above.
    • includeMethod

      private static boolean includeMethod(Method method)
      Determines if a method should be output by the toXML(AstronomicalCalendar)
      Parameters:
      method - the method in question
      Returns:
      if the method should be included in serialization