Class ZmanimFormatter

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

public class ZmanimFormatter extends Object
A class used to format both non Date times generated by the Zmanim package as well as Dates. For example the AstronomicalCalendar.getTemporalHour() returns the length of the hour in milliseconds. This class can format this time.
Author:
© Eliyahu Hershfeld 2004 - 2022
  • Field Details

  • Constructor Details

    • ZmanimFormatter

      public ZmanimFormatter(TimeZone timeZone)
      constructor that defaults to this will use the format "h:mm:ss" for dates and 00.00.00.0 for Time.
      Parameters:
      timeZone - the TimeZone Object
    • ZmanimFormatter

      public ZmanimFormatter(int format, SimpleDateFormat dateFormat, TimeZone timeZone)
      ZmanimFormatter constructor using a formatter
      Parameters:
      format - int The formatting style to use. Using ZmanimFormatter.SEXAGESIMAL_SECONDS_FORMAT will format the time time of 90*60*1000 + 1 as 1:30:00
      dateFormat - the SimpleDateFormat Object
      timeZone - the TimeZone Object
  • Method Details

    • getTimeZone

      Returns:
      the timeZone
    • setTimeZone

      public void setTimeZone(TimeZone timeZone)
      Parameters:
      timeZone - the timeZone to set
    • setTimeFormat

      public void setTimeFormat(int format)
      Sets the format to use for formatting.
      Parameters:
      format - int the format constant to use.
    • setDateFormat

      public void setDateFormat(SimpleDateFormat simpleDateFormat)
      Sets the SimpleDateFormat Object
      Parameters:
      simpleDateFormat - the SimpleDateFormat Object to set
    • getDateFormat

      returns the SimpleDateFormat Object
      Returns:
      the SimpleDateFormat 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 'oclock is displayed as 01)
      useSeconds - should seconds be used in the time format
      useMillis - should milliseconds be used informatting time.
    • format

      public String format(double milliseconds)
      A method that formats milliseconds into a time format.
      Parameters:
      milliseconds - The time in milliseconds.
      Returns:
      String The formatted String
    • format

      public String format(int millis)
      A method that formats milliseconds into a time format.
      Parameters:
      millis - The time in milliseconds.
      Returns:
      String The formatted String
    • format

      public String format(Time time)
      A method that formats Timeobjects.
      Parameters:
      time - The time Object to be formatted.
      Returns:
      String The formatted String
    • formatDateTime

      public String formatDateTime(Date dateTime, Calendar calendar)
      Formats a date using this classe's date format.
      Parameters:
      dateTime - the date to format
      calendar - the Calendar used to help format based on the Calendar's DST and other settings.
      Returns:
      the formatted String
    • getXSDateTime

      public String getXSDateTime(Date dateTime, Calendar calendar)
      The date:date-time function returns the current date and time as a date/time string. The date/time string that's returned must be a string in the format defined as the lexical representation of xs:dateTime in [3.3.8 dateTime] of [XML Schema 1.1 Part 2: Datatypes]. The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult [XML Schema 1.1 Part 2: Datatypes] and [ISO 8601] for details. The date/time string format must include a time zone, either a Z to indicate Coordinated Universal Time or a + or - followed by the difference between the difference from UTC represented as hh:mm.
      Parameters:
      dateTime - the Date Object
      calendar - Calendar Object
      Returns:
      the XSD dateTime
    • formatDigits

      private static String formatDigits(int digits)
      Represent the hours and minutes with two-digit strings.
      Parameters:
      digits - hours or minutes.
      Returns:
      two-digit String representation of hrs or minutes.
    • formatXSDDurationTime

      public String formatXSDDurationTime(long millis)
      This returns the xml representation of an xsd:duration object.
      Parameters:
      millis - the duration in milliseconds
      Returns:
      the xsd:duration formatted String
    • formatXSDDurationTime

      This returns the xml representation of an xsd:duration object.
      Parameters:
      time - the duration as a Time object
      Returns:
      the xsd:duration 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="Lakewood, NJ" latitude="40.095965" longitude="-74.22213" elevation="31.0" timeZoneName="Eastern Standard Time" timeZoneID="America/New_York" timeZoneOffset="-5">
           <Sunrise>2007-02-18T06:45:27-05:00</Sunrise>
           <TemporalHour>PT54M17.529S</TemporalHour>
           ...
         </AstronomicalTimes>
       
      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 formatted String. The format will be:
        <AstronomicalTimes date="1969-02-08" type="com.kosherjava.zmanim.AstronomicalCalendar algorithm="US Naval Almanac Algorithm" location="Lakewood, NJ" latitude="40.095965" longitude="-74.22213" elevation="31.0" timeZoneName="Eastern Standard Time" timeZoneID="America/New_York" timeZoneOffset="-5">
           <Sunrise>2007-02-18T06:45:27-05:00</Sunrise>
           <TemporalHour>PT54M17.529S</TemporalHour>
           ...
        </AstronomicalTimes>
       
      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":"Lakewood, NJ",
            "latitude":"40.095965",
            "longitude":"-74.22213",
            "elevation:"31.0",
            "timeZoneName":"Eastern Standard Time",
            "timeZoneID":"America/New_York",
            "timeZoneOffset":"-5"},
          "AstronomicalTimes":{
           "Sunrise":"2007-02-18T06:45:27-05:00",
           "TemporalHour":"PT54M17.529S"
           ...
           }
       }
       
      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.
      Parameters:
      astronomicalCalendar - the AstronomicalCalendar Object
      Returns:
      The JSON formatted String. The format will be:
       {
          "metadata":{
            "date":"1969-02-08",
            "type":"com.kosherjava.zmanim.AstronomicalCalendar",
            "algorithm":"US Naval Almanac Algorithm",
            "location":"Lakewood, NJ",
            "latitude":"40.095965",
            "longitude":"-74.22213",
            "elevation:"31.0",
            "timeZoneName":"Eastern Standard Time",
            "timeZoneID":"America/New_York",
            "timeZoneOffset":"-5"},
          "AstronomicalTimes":{
           "Sunrise":"2007-02-18T06:45:27-05:00",
           "TemporalHour":"PT54M17.529S"
           ...
           }
       }
       
    • 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