Skip to content

Commit 8a643bc

Browse files
authored
Replace StringBuffer with StringBuilder and add toXML()
1 parent 7fba454 commit 8a643bc

File tree

1 file changed

+30
-3
lines changed
  • src/main/java/com/kosherjava/zmanim/util

1 file changed

+30
-3
lines changed

src/main/java/com/kosherjava/zmanim/util/Zman.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Zmanim Java API
3-
* Copyright (C) 2004-2020 Eliyahu Hershfeld
3+
* Copyright (C) 2004-2024 Eliyahu Hershfeld
44
*
55
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
66
* Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
@@ -54,7 +54,7 @@
5454
* // will sort shaah 1.6, shaah GRA, sunrise, sunset
5555
* </pre>
5656
*
57-
* @author &copy; Eliyahu Hershfeld 2007-2020
57+
* @author &copy; Eliyahu Hershfeld 2007-2024
5858
* @todo Add secondary sorting. As of now the {@code Comparator}s in this class do not sort by secondary order. This means that when sorting a
5959
* {@link java.util.Collection} of <em>zmanim</em> and using the {@link #DATE_ORDER} {@code Comparator} will have the duration based <em>zmanim</em>
6060
* at the end, but they will not be sorted by duration. This should be N/A for label based sorting.
@@ -230,12 +230,39 @@ public int compare(Zman zman1, Zman zman2) {
230230
return firstDuration == secondDuration ? 0 : firstDuration > secondDuration ? 1 : -1;
231231
}
232232
};
233+
234+
/**
235+
* A method that returns an XML formatted <code>String</code> representing the serialized <code>Object</code>. Very
236+
* similar to the toString method but the return value is in an xml format. The format currently used (subject to
237+
* change) is:
238+
*
239+
* <pre>
240+
* &lt;Zman&gt;
241+
* &lt;Label&gt;Sof Zman Krias Shema GRA&lt;/Label&gt;
242+
* &lt;Zman&gt;1969-02-08T09:37:56.820&lt;/Zman&gt;
243+
* &lt;Duration&gt;0&lt;/Duration&gt;
244+
* &lt;Description&gt;Sof Zman Krias Shema GRA is 3 sha'os zmaniyos calculated from sunrise to sunset.&lt;/Description&gt;
245+
* &lt;/Zman&gt;
246+
* </pre>
247+
* @return The XML formatted <code>String</code>.
248+
*/
249+
public String toXML() {
250+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
251+
StringBuilder sb = new StringBuilder();
252+
sb.append("<Zman>\n");
253+
sb.append("\t<Label>").append(getLabel()).append("</Label>\n");
254+
sb.append("\t<Zman>").append(getZman() == null ? "": formatter.format(getZman())).append("</Zman>\n");
255+
sb.append("\n\t<Duration>").append(getDuration()).append("</Duration>\n");
256+
sb.append("\t<Description>").append(getDescription()).append("</Description>\n");
257+
sb.append("</Zman>");
258+
return sb.toString();
259+
}
233260

234261
/**
235262
* @see java.lang.Object#toString()
236263
*/
237264
public String toString() {
238-
StringBuffer sb = new StringBuffer();
265+
StringBuilder sb = new StringBuilder();
239266
sb.append("\nLabel:\t\t\t").append(this.getLabel());
240267
sb.append("\nZman:\t\t\t").append(getZman());
241268
sb.append("\nDuration:\t\t\t").append(getDuration());

0 commit comments

Comments
 (0)