Package com.kosherjava.zmanim.util
Class ZmanimFormatter
java.lang.Object
com.kosherjava.zmanim.util.ZmanimFormatter
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 - 2024
-
Field Summary
Modifier and TypeFieldDescriptionprivate SimpleDateFormat
static final int
Format using standard decimal format with 5 positions after the decimal.static final long
constant for milliseconds in an hour (3,600,000)private DecimalFormat
the formatter for hours.private static DecimalFormat
the formatter for minutes as milliseconds.(package private) static final long
constant for milliseconds in a minute (60,000)private static DecimalFormat
the formatter for minutes as seconds.private boolean
Setting to prepent a zero to single digit hours.static final int
Format using hours and minutes.static final int
Format using hours, minutes, seconds and milliseconds.static final int
Format using hours, minutes and seconds.static final int
Format using hours, minutes, seconds and milliseconds using the xsd:time format.private int
Defaults toSEXAGESIMAL_XSD_FORMAT
.private TimeZone
private boolean
private boolean
static final int
Format using the XSD Duration format. -
Constructor Summary
ConstructorDescriptionZmanimFormatter
(int format, SimpleDateFormat dateFormat, TimeZone timeZone) ZmanimFormatter constructor using a formatterZmanimFormatter
(TimeZone timeZone) constructor that defaults to this will use the format "h:mm:ss" for dates and 00.00.00.0 forTime
. -
Method Summary
Modifier and TypeMethodDescriptionformat
(double milliseconds) A method that formats milliseconds into a time format.format
(int millis) A method that formats milliseconds into a time format.A method that formatsTime
objects.formatDateTime
(Date dateTime, Calendar calendar) Formats a date using this classe'sdate format
.private static String
formatDigits
(int digits) Represent the hours and minutes with two-digit strings.formatXSDDurationTime
(long millis) This returns the xml representation of an xsd:duration object.formatXSDDurationTime
(Time time) This returns the xml representation of an xsd:duration object.returns the SimpleDateFormat ObjectgetXSDateTime
(Date date) getXSDateTime
(Date date, Calendar calendar) Deprecated.This method will be removed in v3.0private static boolean
includeMethod
(Method method) Determines if a method should be output by thetoXML(AstronomicalCalendar)
void
setDateFormat
(SimpleDateFormat simpleDateFormat) Sets the SimpleDateFormat Objectprivate void
setSettings
(boolean prependZeroHours, boolean useSeconds, boolean useMillis) Sets various format settings.void
setTimeFormat
(int format) Sets the format to use for formatting.void
setTimeZone
(TimeZone timeZone) static String
toJSON
(AstronomicalCalendar astronomicalCalendar) A method that returns a JSON formattedString
representing the serializedObject
.static String
toXML
(AstronomicalCalendar astronomicalCalendar) A method that returns an XML formattedString
representing the serializedObject
.
-
Field Details
-
prependZeroHours
Setting to prepent a zero to single digit hours.- See Also:
-
useSeconds
- See Also:
-
useMillis
- See Also:
-
minuteSecondNF
the formatter for minutes as seconds. -
hourNF
the formatter for hours. -
milliNF
the formatter for minutes as milliseconds. -
dateFormat
- See Also:
-
timeZone
- See Also:
-
SEXAGESIMAL_XSD_FORMAT
Format using hours, minutes, seconds and milliseconds using the xsd:time format. This format will return 00.00.00.0 when formatting 0.- See Also:
-
timeFormat
Defaults toSEXAGESIMAL_XSD_FORMAT
.- See Also:
-
DECIMAL_FORMAT
Format using standard decimal format with 5 positions after the decimal.- See Also:
-
SEXAGESIMAL_FORMAT
Format using hours and minutes.- See Also:
-
SEXAGESIMAL_SECONDS_FORMAT
Format using hours, minutes and seconds.- See Also:
-
SEXAGESIMAL_MILLIS_FORMAT
Format using hours, minutes, seconds and milliseconds.- See Also:
-
MINUTE_MILLIS
constant for milliseconds in a minute (60,000)- See Also:
-
HOUR_MILLIS
constant for milliseconds in an hour (3,600,000)- See Also:
-
XSD_DURATION_FORMAT
Format using the XSD Duration format. This is in the format of PT1H6M7.869S (P for period (duration), T for time, H, M and S indicate hours, minutes and seconds.- See Also:
-
-
Constructor Details
-
ZmanimFormatter
constructor that defaults to this will use the format "h:mm:ss" for dates and 00.00.00.0 forTime
.- Parameters:
timeZone
- the TimeZone Object
-
ZmanimFormatter
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:00dateFormat
- the SimpleDateFormat ObjecttimeZone
- the TimeZone Object
-
-
Method Details
-
getTimeZone
- Returns:
- the timeZone
-
setTimeZone
- Parameters:
timeZone
- the timeZone to set
-
setTimeFormat
Sets the format to use for formatting.- Parameters:
format
- int the format constant to use.
-
setDateFormat
Sets the SimpleDateFormat Object- Parameters:
simpleDateFormat
- the SimpleDateFormat Object to set
-
getDateFormat
returns the SimpleDateFormat Object- Returns:
- the SimpleDateFormat Object
-
setSettings
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 formatuseMillis
- should milliseconds be used informatting time.
-
format
A method that formats milliseconds into a time format.- Parameters:
milliseconds
- The time in milliseconds.- Returns:
- String The formatted
String
-
format
A method that formats milliseconds into a time format.- Parameters:
millis
- The time in milliseconds.- Returns:
- String The formatted
String
-
format
A method that formatsTime
objects.- Parameters:
time
- The timeObject
to be formatted.- Returns:
- String The formatted
String
-
formatDateTime
Formats a date using this classe'sdate format
.- Parameters:
dateTime
- the date to formatcalendar
- theCalendar
used to help format based on the Calendar's DST and other settings.- Returns:
- the formatted String
-
getXSDateTime
Deprecated.This method will be removed in v3.0The 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 Objectcalendar
- Calendar Object that is now ignored.- Returns:
- the XSD dateTime
-
getXSDateTime
-
formatDigits
Represent the hours and minutes with two-digit strings.- Parameters:
digits
- hours or minutes.- Returns:
- two-digit String representation of hrs or minutes.
-
formatXSDDurationTime
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
A method that returns an XML formattedString
representing the serializedObject
. 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 atemporal hour
. The output of this method is returned by thetoString
.- 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
A method that returns a JSON formattedString
representing the serializedObject
. 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 atemporal 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
Determines if a method should be output by thetoXML(AstronomicalCalendar)
- Parameters:
method
- the method in question- Returns:
- if the method should be included in serialization
-