001/*
002 * Zmanim Java API
003 * Copyright (C) 2011 - 2026 Eliyahu Hershfeld
004 *
005 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
006 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option)
007 * any later version.
008 *
009 * This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied
010 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
011 * details.
012 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
013 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA,
014 * or connect to: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
015 */
016package com.kosherjava.zmanim.hebrewcalendar;
017
018import java.time.format.DateTimeFormatter;
019import java.util.EnumMap;
020
021/**
022 * The HebrewDateFormatter class formats a {@link JewishDate}. The class formats Jewish dates, numbers, <em>Daf Yomi</em>
023 * (<em>Bavli</em> and <em>Yerushalmi</em>), the <em>Omer</em>, <em>Parshas Hashavua</em> (including the special <em>parshiyos</em>
024 * of <em>Shekalim</em>, <em>Zachor</em>, <em>Parah</em> and <em>Hachodesh</em>), <em>Yomim Tovim</em> in Hebrew or Latin chars, and
025 * has various settings. Sample full date output includes (using various options):
026 * <ul>
027 * <li>21 Shevat, 5729</li>
028 * <li>כא שבט תשכט</li>
029 * <li>כ״א שבט ה׳תשכ״ט</li>
030 * <li>כ״א שבט תש״פ or כ״א שבט תש״ף</li>
031 * <li>כ׳ שבט ו׳ אלפים</li>
032 * </ul>
033 * 
034 * @see JewishDate
035 * @see JewishCalendar
036 * @author &copy; Eliyahu Hershfeld 2011 - 2026
037 */
038public class HebrewDateFormatter {
039        
040        /**
041         * See {@link #isHebrewFormat()} and {@link #setHebrewFormat(boolean)}.
042         */
043        private boolean hebrewFormat = false;
044        
045        /**
046         * See {@link #isUseLongHebrewYears()} and {@link #setUseLongHebrewYears(boolean)}.
047         */
048        private boolean useLonghebrewYears = false;
049        
050        /**
051         * See {@link #isUseGershGershayim()} and {@link #setUseGershGershayim(boolean)}.
052         */
053        private boolean useGershGershayim = true;
054        
055        /**
056         * See {@link #isLongWeekFormat()} and {@link #setLongWeekFormat(boolean)}.
057         */
058        private boolean longWeekFormat = true;
059        
060        /**
061         * See {@link #isUseFinalFormLetters()} and {@link #setUseFinalFormLetters(boolean)}.
062         */
063        private boolean useFinalFormLetters = false;
064        
065        /**
066         * The internal DateFormat. See {@link #isLongWeekFormat()} and {@link #setLongWeekFormat(boolean)}.
067         */
068        private DateTimeFormatter weekFormat;
069        
070        /**
071         * List of transliterated parshiyos using the default <em>Ashkenazi</em> pronunciation. For information on the format, see
072         * {@link #getTransliteratedParshiosList()}.
073         * 
074         * @see #getTransliteratedParshiosList()
075         * @see #setTransliteratedParshiosList(EnumMap)
076         * @see #HebrewDateFormatter() where the map is initially set.
077         */
078        private EnumMap<JewishCalendar.Parsha, String> transliteratedParshaMap;
079        
080        /**
081         * An {@link EnumMap} of Hebrew <em>parshiyos</em>. The list includes double and special <em>parshiyos</em> and contains<br>
082         * <code>&rlm;בראשית, נח, לך לך,וירא, חיי שרה,תולדות, ויצא, וישלח,וישב, מקץ, ויגש, ויחי,שמות, וארא, בא, בשלח,יתרו, משפטים, תרומה,תצוה, כי תשא, ויקהל,פקודי, 
083         * ויקרא, צו,שמיני, תזריע, מצרע,אחרי מות, קדושים,אמור, בהר, בחקתי,במדבר, נשא, בהעלתך,שלח לך, קרח, חוקת, בלק,פינחס, מטות, מסעי,דברים, ואתחנן, עקב,ראה, שופטים, כי תצא,כי
084         * תבוא, נצבים, וילך,האזינו, וזאת הברכה,ויקהל פקודי, תזריעמצרע, אחרי מותקדושים, בהר בחקתי,חוקת בלק, מטות מסעי,נצבים וילך,
085         * שקלים,זכור, פרה, החדש,שובה,שירה,הגדול,חזון,נחמו</code>
086         */
087        private final EnumMap<JewishCalendar.Parsha, String> hebrewParshaMap;
088        
089        /**
090         * Default constructor sets the {@link EnumMap}s of Hebrew and default transliterated parshiyos.
091         */
092        public HebrewDateFormatter() {
093                weekFormat = DateTimeFormatter.ofPattern("EEEE");
094                transliteratedParshaMap = new EnumMap<>(JewishCalendar.Parsha.class);
095                transliteratedParshaMap.put(JewishCalendar.Parsha.NONE, "");
096                transliteratedParshaMap.put(JewishCalendar.Parsha.BERESHIS, "Bereshis");
097                transliteratedParshaMap.put(JewishCalendar.Parsha.NOACH, "Noach");
098                transliteratedParshaMap.put(JewishCalendar.Parsha.LECH_LECHA, "Lech Lecha");
099                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYERA, "Vayera");
100                transliteratedParshaMap.put(JewishCalendar.Parsha.CHAYEI_SARA, "Chayei Sara");
101                transliteratedParshaMap.put(JewishCalendar.Parsha.TOLDOS, "Toldos");
102                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYETZEI, "Vayetzei");
103                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYISHLACH, "Vayishlach");
104                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYESHEV, "Vayeshev");
105                transliteratedParshaMap.put(JewishCalendar.Parsha.MIKETZ, "Miketz");
106                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYIGASH, "Vayigash");
107                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYECHI, "Vayechi");
108                transliteratedParshaMap.put(JewishCalendar.Parsha.SHEMOS, "Shemos");
109                transliteratedParshaMap.put(JewishCalendar.Parsha.VAERA, "Vaera");
110                transliteratedParshaMap.put(JewishCalendar.Parsha.BO, "Bo");
111                transliteratedParshaMap.put(JewishCalendar.Parsha.BESHALACH, "Beshalach");
112                transliteratedParshaMap.put(JewishCalendar.Parsha.YISRO, "Yisro");
113                transliteratedParshaMap.put(JewishCalendar.Parsha.MISHPATIM, "Mishpatim");
114                transliteratedParshaMap.put(JewishCalendar.Parsha.TERUMAH, "Terumah");
115                transliteratedParshaMap.put(JewishCalendar.Parsha.TETZAVEH, "Tetzaveh");
116                transliteratedParshaMap.put(JewishCalendar.Parsha.KI_SISA, "Ki Sisa");
117                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYAKHEL, "Vayakhel");
118                transliteratedParshaMap.put(JewishCalendar.Parsha.PEKUDEI, "Pekudei");
119                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYIKRA, "Vayikra");
120                transliteratedParshaMap.put(JewishCalendar.Parsha.TZAV, "Tzav");
121                transliteratedParshaMap.put(JewishCalendar.Parsha.SHMINI, "Shmini");
122                transliteratedParshaMap.put(JewishCalendar.Parsha.TAZRIA, "Tazria");
123                transliteratedParshaMap.put(JewishCalendar.Parsha.METZORA, "Metzora");
124                transliteratedParshaMap.put(JewishCalendar.Parsha.ACHREI_MOS, "Achrei Mos");
125                transliteratedParshaMap.put(JewishCalendar.Parsha.KEDOSHIM, "Kedoshim");
126                transliteratedParshaMap.put(JewishCalendar.Parsha.EMOR, "Emor");
127                transliteratedParshaMap.put(JewishCalendar.Parsha.BEHAR, "Behar");
128                transliteratedParshaMap.put(JewishCalendar.Parsha.BECHUKOSAI, "Bechukosai");
129                transliteratedParshaMap.put(JewishCalendar.Parsha.BAMIDBAR, "Bamidbar");
130                transliteratedParshaMap.put(JewishCalendar.Parsha.NASSO, "Nasso");
131                transliteratedParshaMap.put(JewishCalendar.Parsha.BEHAALOSCHA, "Beha'aloscha");
132                transliteratedParshaMap.put(JewishCalendar.Parsha.SHLACH, "Sh'lach");
133                transliteratedParshaMap.put(JewishCalendar.Parsha.KORACH, "Korach");
134                transliteratedParshaMap.put(JewishCalendar.Parsha.CHUKAS, "Chukas");
135                transliteratedParshaMap.put(JewishCalendar.Parsha.BALAK, "Balak");
136                transliteratedParshaMap.put(JewishCalendar.Parsha.PINCHAS, "Pinchas");
137                transliteratedParshaMap.put(JewishCalendar.Parsha.MATOS, "Matos");
138                transliteratedParshaMap.put(JewishCalendar.Parsha.MASEI, "Masei");
139                transliteratedParshaMap.put(JewishCalendar.Parsha.DEVARIM, "Devarim");
140                transliteratedParshaMap.put(JewishCalendar.Parsha.VAESCHANAN, "Vaeschanan");
141                transliteratedParshaMap.put(JewishCalendar.Parsha.EIKEV, "Eikev");
142                transliteratedParshaMap.put(JewishCalendar.Parsha.REEH, "Re'eh");
143                transliteratedParshaMap.put(JewishCalendar.Parsha.SHOFTIM, "Shoftim");
144                transliteratedParshaMap.put(JewishCalendar.Parsha.KI_SEITZEI, "Ki Seitzei");
145                transliteratedParshaMap.put(JewishCalendar.Parsha.KI_SAVO, "Ki Savo");
146                transliteratedParshaMap.put(JewishCalendar.Parsha.NITZAVIM, "Nitzavim");
147                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYEILECH, "Vayeilech");
148                transliteratedParshaMap.put(JewishCalendar.Parsha.HAAZINU, "Ha'Azinu");
149                transliteratedParshaMap.put(JewishCalendar.Parsha.VZOS_HABERACHA, "Vezos Habracha");
150                transliteratedParshaMap.put(JewishCalendar.Parsha.VAYAKHEL_PEKUDEI, "Vayakhel Pekudei");
151                transliteratedParshaMap.put(JewishCalendar.Parsha.TAZRIA_METZORA, "Tazria Metzora");
152                transliteratedParshaMap.put(JewishCalendar.Parsha.ACHREI_MOS_KEDOSHIM, "Achrei Mos Kedoshim");
153                transliteratedParshaMap.put(JewishCalendar.Parsha.BEHAR_BECHUKOSAI, "Behar Bechukosai");
154                transliteratedParshaMap.put(JewishCalendar.Parsha.CHUKAS_BALAK, "Chukas Balak");
155                transliteratedParshaMap.put(JewishCalendar.Parsha.MATOS_MASEI, "Matos Masei");
156                transliteratedParshaMap.put(JewishCalendar.Parsha.NITZAVIM_VAYEILECH, "Nitzavim Vayeilech");
157                transliteratedParshaMap.put(JewishCalendar.Parsha.SHKALIM, "Shekalim");
158                transliteratedParshaMap.put(JewishCalendar.Parsha.ZACHOR, "Zachor");
159                transliteratedParshaMap.put(JewishCalendar.Parsha.PARA, "Parah");
160                transliteratedParshaMap.put(JewishCalendar.Parsha.HACHODESH, "Hachodesh");
161                transliteratedParshaMap.put(JewishCalendar.Parsha.SHUVA, "Shuva");
162                transliteratedParshaMap.put(JewishCalendar.Parsha.SHIRA, "Shira");
163                transliteratedParshaMap.put(JewishCalendar.Parsha.HAGADOL, "Hagadol");
164                transliteratedParshaMap.put(JewishCalendar.Parsha.CHAZON, "Chazon");
165                transliteratedParshaMap.put(JewishCalendar.Parsha.NACHAMU, "Nachamu");
166                
167                hebrewParshaMap = new EnumMap<>(JewishCalendar.Parsha.class);
168                hebrewParshaMap.put(JewishCalendar.Parsha.NONE, "");
169                hebrewParshaMap.put(JewishCalendar.Parsha.BERESHIS, "בראשית");
170                hebrewParshaMap.put(JewishCalendar.Parsha.NOACH, "נח");
171                hebrewParshaMap.put(JewishCalendar.Parsha.LECH_LECHA, "לך לך");
172                hebrewParshaMap.put(JewishCalendar.Parsha.VAYERA, "וירא");
173                hebrewParshaMap.put(JewishCalendar.Parsha.CHAYEI_SARA, "חיי שרה");
174                hebrewParshaMap.put(JewishCalendar.Parsha.TOLDOS, "תולדות");
175                hebrewParshaMap.put(JewishCalendar.Parsha.VAYETZEI, "ויצא");
176                hebrewParshaMap.put(JewishCalendar.Parsha.VAYISHLACH, "וישלח");
177                hebrewParshaMap.put(JewishCalendar.Parsha.VAYESHEV, "וישב");
178                hebrewParshaMap.put(JewishCalendar.Parsha.MIKETZ, "מקץ");
179                hebrewParshaMap.put(JewishCalendar.Parsha.VAYIGASH, "ויגש");
180                hebrewParshaMap.put(JewishCalendar.Parsha.VAYECHI, "ויחי");
181                hebrewParshaMap.put(JewishCalendar.Parsha.SHEMOS, "שמות");
182                hebrewParshaMap.put(JewishCalendar.Parsha.VAERA, "וארא");
183                hebrewParshaMap.put(JewishCalendar.Parsha.BO, "בא");
184                hebrewParshaMap.put(JewishCalendar.Parsha.BESHALACH, "בשלח");
185                hebrewParshaMap.put(JewishCalendar.Parsha.YISRO, "יתרו");
186                hebrewParshaMap.put(JewishCalendar.Parsha.MISHPATIM, "משפטים");
187                hebrewParshaMap.put(JewishCalendar.Parsha.TERUMAH, "תרומה");
188                hebrewParshaMap.put(JewishCalendar.Parsha.TETZAVEH, "תצוה");
189                hebrewParshaMap.put(JewishCalendar.Parsha.KI_SISA, "כי תשא");
190                hebrewParshaMap.put(JewishCalendar.Parsha.VAYAKHEL, "ויקהל");
191                hebrewParshaMap.put(JewishCalendar.Parsha.PEKUDEI, "פקודי");
192                hebrewParshaMap.put(JewishCalendar.Parsha.VAYIKRA, "ויקרא");
193                hebrewParshaMap.put(JewishCalendar.Parsha.TZAV, "צו");
194                hebrewParshaMap.put(JewishCalendar.Parsha.SHMINI, "שמיני");
195                hebrewParshaMap.put(JewishCalendar.Parsha.TAZRIA, "תזריע");
196                hebrewParshaMap.put(JewishCalendar.Parsha.METZORA, "מצרע");
197                hebrewParshaMap.put(JewishCalendar.Parsha.ACHREI_MOS, "אחרי מות");
198                hebrewParshaMap.put(JewishCalendar.Parsha.KEDOSHIM, "קדושים");
199                hebrewParshaMap.put(JewishCalendar.Parsha.EMOR, "אמור");
200                hebrewParshaMap.put(JewishCalendar.Parsha.BEHAR, "בהר");
201                hebrewParshaMap.put(JewishCalendar.Parsha.BECHUKOSAI, "בחקתי");
202                hebrewParshaMap.put(JewishCalendar.Parsha.BAMIDBAR, "במדבר");
203                hebrewParshaMap.put(JewishCalendar.Parsha.NASSO, "נשא");
204                hebrewParshaMap.put(JewishCalendar.Parsha.BEHAALOSCHA, "בהעלתך");
205                hebrewParshaMap.put(JewishCalendar.Parsha.SHLACH, "שלח לך");
206                hebrewParshaMap.put(JewishCalendar.Parsha.KORACH, "קרח");
207                hebrewParshaMap.put(JewishCalendar.Parsha.CHUKAS, "חוקת");
208                hebrewParshaMap.put(JewishCalendar.Parsha.BALAK, "בלק");
209                hebrewParshaMap.put(JewishCalendar.Parsha.PINCHAS, "פינחס");
210                hebrewParshaMap.put(JewishCalendar.Parsha.MATOS, "מטות");
211                hebrewParshaMap.put(JewishCalendar.Parsha.MASEI, "מסעי");
212                hebrewParshaMap.put(JewishCalendar.Parsha.DEVARIM, "דברים");
213                hebrewParshaMap.put(JewishCalendar.Parsha.VAESCHANAN, "ואתחנן");
214                hebrewParshaMap.put(JewishCalendar.Parsha.EIKEV, "עקב");
215                hebrewParshaMap.put(JewishCalendar.Parsha.REEH, "ראה");
216                hebrewParshaMap.put(JewishCalendar.Parsha.SHOFTIM, "שופטים");
217                hebrewParshaMap.put(JewishCalendar.Parsha.KI_SEITZEI, "כי תצא");
218                hebrewParshaMap.put(JewishCalendar.Parsha.KI_SAVO, "כי תבוא");
219                hebrewParshaMap.put(JewishCalendar.Parsha.NITZAVIM, "נצבים");
220                hebrewParshaMap.put(JewishCalendar.Parsha.VAYEILECH, "וילך");
221                hebrewParshaMap.put(JewishCalendar.Parsha.HAAZINU, "האזינו");
222                hebrewParshaMap.put(JewishCalendar.Parsha.VZOS_HABERACHA, "וזאת הברכה");
223                hebrewParshaMap.put(JewishCalendar.Parsha.VAYAKHEL_PEKUDEI, "ויקהל פקודי");
224                hebrewParshaMap.put(JewishCalendar.Parsha.TAZRIA_METZORA, "תזריע מצרע");
225                hebrewParshaMap.put(JewishCalendar.Parsha.ACHREI_MOS_KEDOSHIM, "אחרי מות קדושים");
226                hebrewParshaMap.put(JewishCalendar.Parsha.BEHAR_BECHUKOSAI, "בהר בחקתי");
227                hebrewParshaMap.put(JewishCalendar.Parsha.CHUKAS_BALAK, "חוקת בלק");
228                hebrewParshaMap.put(JewishCalendar.Parsha.MATOS_MASEI, "מטות מסעי");
229                hebrewParshaMap.put(JewishCalendar.Parsha.NITZAVIM_VAYEILECH, "נצבים וילך");
230                hebrewParshaMap.put(JewishCalendar.Parsha.SHKALIM, "שקלים");
231                hebrewParshaMap.put(JewishCalendar.Parsha.ZACHOR, "זכור");
232                hebrewParshaMap.put(JewishCalendar.Parsha.PARA, "פרה");
233                hebrewParshaMap.put(JewishCalendar.Parsha.HACHODESH, "החדש");
234                hebrewParshaMap.put(JewishCalendar.Parsha.SHUVA, "שובה");
235                hebrewParshaMap.put(JewishCalendar.Parsha.SHIRA, "שירה");
236                hebrewParshaMap.put(JewishCalendar.Parsha.HAGADOL, "הגדול");
237                hebrewParshaMap.put(JewishCalendar.Parsha.CHAZON, "חזון");
238                hebrewParshaMap.put(JewishCalendar.Parsha.NACHAMU, "נחמו");
239        }
240
241        /**
242         * Returns if the {@link #formatDayOfWeek(JewishDate)} will use the long format such as ראשון or short such as א when formatting
243         * the day of week in {@link #isHebrewFormat() Hebrew}.
244         * 
245         * @return the longWeekFormat
246         * @see #setLongWeekFormat(boolean)
247         * @see #formatDayOfWeek(JewishDate)
248         */
249        public boolean isLongWeekFormat() {
250                return longWeekFormat;
251        }
252
253        /**
254         * Setting to control if the {@link #formatDayOfWeek(JewishDate)} will use the long format such as ראשון or short such as א when
255         * formatting the day of week in {@link #isHebrewFormat() Hebrew}.
256         * 
257         * @param longWeekFormat the longWeekFormat to set
258         */
259        public void setLongWeekFormat(boolean longWeekFormat) {
260                this.longWeekFormat = longWeekFormat;
261                if (longWeekFormat) {
262                        weekFormat = DateTimeFormatter.ofPattern("EEEE");
263                } else {
264                        weekFormat = DateTimeFormatter.ofPattern("EEE");
265                }
266        }
267
268        /**
269         * The <a href="https://en.wikipedia.org/wiki/Geresh#Punctuation_mark">gersh</a> character is the ׳ char that is similar to a
270         * single quote and is used in formatting Hebrew numbers.
271         */
272        private static final String GERESH = "׳";
273        
274        /**
275         * The <a href="https://en.wikipedia.org/wiki/Gershayim#Punctuation_mark">gershyim</a> character is the ״ char that is similar
276         * to a double quote and is used in formatting Hebrew numbers.
277         */
278        private static final String GERSHAYIM = "״";
279        
280        /**
281         * Transliterated month names that default to <code>["Nissan", "Iyar", "Sivan", "Tammuz", "Av", "Elul", "Tishrei", "Cheshvan",
282         * "Kislev", "Teves", "Shevat", "Adar", "Adar II", "Adar I" ]</code>.
283         * @see #getTransliteratedMonthList()
284         * @see #setTransliteratedMonthList(String[])
285         */
286        private String[] transliteratedMonths = { "Nissan", "Iyar", "Sivan", "Tammuz", "Av", "Elul", "Tishrei", "Cheshvan",
287                        "Kislev", "Teves", "Shevat", "Adar", "Adar II", "Adar I" };
288        
289        /**
290         * The Hebrew omer prefix charachter. It defaults to ב producing בעומר, but can be set to ל to produce לעומר (or any other prefix).
291         * @see #getHebrewOmerPrefix()
292         * @see #setHebrewOmerPrefix(String)
293         */
294        private String hebrewOmerPrefix = "ב";
295
296        /**
297         * The default value for formatting "Shabbos" (Saturday) when transliterated.
298         * @see #getTransliteratedShabbosDayOfWeek()
299         * @see #setTransliteratedShabbosDayOfWeek(String)
300         */
301        private String transliteratedShabbosDayOfWeek = "Shabbos";
302
303        /**
304         * Returns the day of Shabbos transliterated into Latin chars. The default uses Ashkenazi pronunciation "Shabbos". This can be
305         * overwritten using the {@link #setTransliteratedShabbosDayOfWeek(String)}. It is uesd by {@link #formatDayOfWeek(JewishDate)}.
306         * 
307         * @return the transliteratedShabbos. The default list of months uses Ashkenazi pronunciation "Shabbos".
308         * @see #setTransliteratedShabbosDayOfWeek(String)
309         * @see #formatDayOfWeek(JewishDate)
310         */
311        public String getTransliteratedShabbosDayOfWeek() {
312                return transliteratedShabbosDayOfWeek;
313        }
314
315        /**
316         * Setter to override the default transliterated name of "Shabbos" to alternate spelling such as "Shabbat" used by
317         * the {@link #formatDayOfWeek(JewishDate)}.
318         * 
319         * @param transliteratedShabbos the transliteratedShabbos to set
320         * @see #getTransliteratedShabbosDayOfWeek()
321         * @see #formatDayOfWeek(JewishDate)
322         */
323        public void setTransliteratedShabbosDayOfWeek(String transliteratedShabbos) {
324                this.transliteratedShabbosDayOfWeek = transliteratedShabbos;
325        }
326
327        /**
328         * See {@link #getTransliteratedHolidayList()} and {@link #setTransliteratedHolidayList(String[])}.
329         */
330        private String[] transliteratedHolidays = {"Erev Pesach", "Pesach", "Chol Hamoed Pesach", "Pesach Sheni",
331                        "Erev Shavuos", "Shavuos", "Seventeenth of Tammuz", "Tishah B'Av", "Tu B'Av", "Erev Rosh Hashana",
332                        "Rosh Hashana", "Fast of Gedalyah", "Erev Yom Kippur", "Yom Kippur", "Erev Succos", "Succos",
333                        "Chol Hamoed Succos", "Hoshana Rabbah", "Shemini Atzeres", "Simchas Torah", "Erev Chanukah", "Chanukah",
334                        "Tenth of Teves", "Tu B'Shvat", "Fast of Esther", "Purim", "Shushan Purim", "Purim Katan", "Rosh Chodesh",
335                        "Yom HaShoah", "Yom Hazikaron", "Yom Ha'atzmaut", "Yom Yerushalayim", "Lag B'Omer", "Shushan Purim Katan",
336                        "Isru Chag"};
337
338        /**
339         * Returns the array of <em>Yomim Tovim</em> (holidays) transliterated into Latin chars. This is used by the {@link
340         * #formatYomTov(JewishCalendar)} when formatting the <em>Yom Tov</em> String. The default list of months usesnAshkenazi
341         * pronunciation in typical American English spelling. The default list is currently
342         * <code>["Erev Pesach", "Pesach", "Chol Hamoed Pesach", "Pesach Sheni", "Erev Shavuos", "Shavuos", "Seventeenth of Tammuz",
343         * "Tishah B'Av", "Tu B'Av", "Erev Rosh Hashana", "Rosh Hashana", "Fast of Gedalyah", "Erev Yom Kippur", "Yom Kippur", "Erev
344         * Succos", "Succos", "Chol Hamoed Succos", "Hoshana Rabbah", "Shemini Atzeres", "Simchas Torah", "Erev Chanukah", "Chanukah",
345         * "Tenth of Teves", "Tu B'Shvat", "Fast of Esther", "Purim", "Shushan Purim",m"Purim Katan", "Rosh Chodesh", "Yom HaShoah",
346         * "Yom Hazikaron", "Yom Ha'atzmaut", "Yom Yerushalayim", "Lag B'Omer", "Shushan Purim Katan", "Isru Chag"]</code>.
347         * 
348         * @return the array of transliterated <em>Yomim Tovim</em> (holidays). 
349         * @see #setTransliteratedMonthList(String[])
350         * @see #formatYomTov(JewishCalendar)
351         * @see #isHebrewFormat()
352         */
353        public String[] getTransliteratedHolidayList() {
354                return transliteratedHolidays;
355        }
356
357        /**
358         * Sets the array of <em>Yomim Tovim</em> (holidays) transliterated into Latin chars. This is used by the
359         * {@link #formatYomTov(JewishCalendar)} when formatting the <em>Yom Tov</em> String. The list uses the following order and uses
360         * the spelling as follows. 
361         * <code>["Erev Pesach", "Pesach", "Chol Hamoed Pesach", "Pesach Sheni", "Erev Shavuos", "Shavuos", "Seventeenth of Tammuz",
362         * "Tishah B'Av", "Tu B'Av", "Erev Rosh Hashana", "Rosh Hashana", "Fast of Gedalyah", "Erev Yom Kippur", "Yom Kippur", "Erev
363         * Succos", "Succos", "Chol Hamoed Succos", "Hoshana Rabbah", "Shemini Atzeres", "Simchas Torah", "Erev Chanukah", "Chanukah",
364         * "Tenth of Teves", "Tu B'Shvat", "Fast of Esther", "Purim", "Shushan Purim", "Purim Katan", "Rosh Chodesh", "Yom HaShoah",
365         * "Yom Hazikaron", "Yom Ha'atzmaut", "Yom Yerushalayim", "Lag B'Omer", "Shushan Purim Katan", "Isru Chag"]</code>.
366         * 
367         * @param transliteratedHolidays the transliteratedHolidays to set. Ensure that the sequence exactly matches the list returned
368         *         by the default.
369         */
370        public void setTransliteratedHolidayList(String[] transliteratedHolidays) {
371                this.transliteratedHolidays = transliteratedHolidays;
372        }
373
374        /**
375         * Hebrew <em>Yomim Tovim</em> (holidays) array in the following format.<br>
376         * <code>&rlm;["ערב פסח", "פסח", "חול המועד פסח", "פסח שני", "ערב שבועות", "שבועות", "שבעה עשר בתמוז", "תשעה באב", "ט״ו באב", "ערב ראש השנה", "ראש השנה",
377         * "צום גדליה", "ערב יום כיפור", "יום כיפור", "ערב סוכות", "סוכות", "חול המועד סוכות", "הושענא רבה", "שמיני עצרת", "שמחת תורה", "ערב חנוכה", "חנוכה", "עשרה בטבת",
378         * "ט״ו בשבט", "תענית אסתר", "פורים", "שושן פורים", "פורים קטן", "ראש חודש", "יום השואה", "יום הזיכרון", "יום העצמאות", "יום ירושלים", "ל״ג בעומר", "שושן פורים קטן"]</code>
379         */
380        private final String[] hebrewHolidays = { "ערב פסח", "פסח",
381                        "חול המועד פסח",
382                        "פסח שני", "ערב שבועות",
383                        "שבועות",
384                        "שבעה עשר בתמוז",
385                        "תשעה באב", "ט״ו באב",
386                        "ערב ראש השנה",
387                        "ראש השנה", "צום גדליה",
388                        "ערב יום כיפור",
389                        "יום כיפור", "ערב סוכות",
390                        "סוכות",
391                        "חול המועד סוכות",
392                        "הושענא רבה",
393                        "שמיני עצרת",
394                        "שמחת תורה", "ערב חנוכה",
395                        "חנוכה", "עשרה בטבת",
396                        "ט״ו בשבט", "תענית אסתר",
397                        "פורים", "שושן פורים",
398                        "פורים קטן", "ראש חודש",
399                        "יום השואה",
400                        "יום הזיכרון",
401                        "יום העצמאות",
402                        "יום ירושלים",
403                        "ל״ג בעומר",
404                        "שושן פורים קטן",
405                        "אסרו חג"};
406
407        /**
408         * Formats the <em>Yom Tov</em> (holiday) in Hebrew or transliterated Latin characters.
409         * 
410         * @param jewishCalendar the JewishCalendar
411         * @return the formatted <em>Yom Tov</em> (holiday) or an empty String if the day is not a <em>Yom Tov</em> (holiday).
412         * @see #isHebrewFormat()
413         */
414        public String formatYomTov(JewishCalendar jewishCalendar) {
415                int index = jewishCalendar.getYomTovIndex();
416                if (index == JewishCalendar.CHANUKAH) {
417                        int dayOfChanukah = jewishCalendar.getDayOfChanukah();
418                        return hebrewFormat ? (formatHebrewNumber(dayOfChanukah) + " " + hebrewHolidays[index])
419                                        : (transliteratedHolidays[index] + " " + dayOfChanukah);
420                }
421                return index == -1 ? "" : hebrewFormat ? hebrewHolidays[index] : transliteratedHolidays[index];
422        }
423
424        /**
425         * Formats a day as Rosh Chodesh in the format of in the format of ראש חודש שבט or Rosh Chodesh Shevat. If it is not Rosh Chodesh,
426         * an empty <code>String</code> will be returned.
427         * @param jewishCalendar the JewishCalendar
428         * @return The formatted <code>String</code> in the format of ראש חודש שבט or Rosh Chodesh Shevat. If it is not Rosh Chodesh, an
429         *         empty <code>String</code> will be returned.
430         */
431        public String formatRoshChodesh(JewishCalendar jewishCalendar) {
432                if (!jewishCalendar.isRoshChodesh()) {
433                        return "";
434                }
435                int month = jewishCalendar.getJewishMonth();
436                if (jewishCalendar.getJewishDayOfMonth() == 30) {
437                        if (month < JewishCalendar.ADAR || (month == JewishCalendar.ADAR && jewishCalendar.isJewishLeapYear())) {
438                                month++;
439                        } else { // roll to Nissan
440                                month = JewishCalendar.NISSAN;
441                        }
442                }
443
444                // This method is only about formatting, so we shouldn't make any changes to the params passed in...
445                jewishCalendar = (JewishCalendar) jewishCalendar.clone();
446                jewishCalendar.setJewishMonth(month);
447                String formattedRoshChodesh = hebrewFormat ? hebrewHolidays[JewishCalendar.ROSH_CHODESH]
448                                : transliteratedHolidays[JewishCalendar.ROSH_CHODESH];
449                formattedRoshChodesh += " " + formatMonth(jewishCalendar);
450                return formattedRoshChodesh;
451        }
452
453        /**
454         * Returns if the formatter is set to use Hebrew formatting in the various formatting methods.
455         * 
456         * @return the hebrewFormat
457         * @see #setHebrewFormat(boolean)
458         * @see #format(JewishDate)
459         * @see #formatDayOfWeek(JewishDate)
460         * @see #formatMonth(JewishDate)
461         * @see #formatOmer(JewishCalendar)
462         * @see #formatYomTov(JewishCalendar)
463         */
464        public boolean isHebrewFormat() {
465                return hebrewFormat;
466        }
467
468        /**
469         * Sets the formatter to format in Hebrew in the various formatting methods.
470         * 
471         * @param hebrewFormat <code>true</code> to format in Hebrew.
472         * @see #isHebrewFormat()
473         * @see #format(JewishDate)
474         * @see #formatDayOfWeek(JewishDate)
475         * @see #formatMonth(JewishDate)
476         * @see #formatOmer(JewishCalendar)
477         * @see #formatYomTov(JewishCalendar)
478         */
479        public void setHebrewFormat(boolean hebrewFormat) {
480                this.hebrewFormat = hebrewFormat;
481        }
482
483        /**
484         * Returns the Hebrew Omer prefix. By default it is the letter ב producing בעומר, but it can be set to ל to produce לעומר (or any
485         * other prefix) using the {@link #setHebrewOmerPrefix(String)}.
486         * 
487         * @return the hebrewOmerPrefix
488         * @see #hebrewOmerPrefix
489         * @see #setHebrewOmerPrefix(String)
490         * @see #formatOmer(JewishCalendar)
491         */
492        public String getHebrewOmerPrefix() {
493                return hebrewOmerPrefix;
494        }
495
496        /**
497         * Method to set the Hebrew Omer prefix. By default it is the letter ב producing בעומר, but it can be set to ל to format it לעומר
498         * (or any other prefix).
499         * @param hebrewOmerPrefix the hebrewOmerPrefix to set. You can set it to ל to produce to לעומר.
500         * @see #hebrewOmerPrefix
501         * @see #getHebrewOmerPrefix()
502         * @see #formatOmer(JewishCalendar)
503         */
504        public void setHebrewOmerPrefix(String hebrewOmerPrefix) {
505                this.hebrewOmerPrefix = hebrewOmerPrefix;
506        }
507        
508        /**
509         * Returns the Hebrew array of months in the order of<br><code>&rlm;["ניסן", "אייר", "סיון", "תמוז", "אב", "אלול", "תשרי", "חשון", "כסלו", "טבת", "שבט", "אדר", 
510         * "אדר ב", "אדר א"]</code>. This list has a length of 14 starting with "ניסן" and ending with 3 variations of Adar -
511         * "אדר", "אדר ב", "אדר א".
512         * @return the array of Hebrew months.
513         * @see #hebrewMonths
514         * @see #setHebrewMonthList(String[])
515         */
516        public String[] getHebrewMonthList() {
517                return hebrewMonths;
518        }
519        
520        /**
521         * Setter method to allow overriding of the default list of Hebrew month names. This allows changing things such as the default
522         * month name of חשון to מרחשון, etc. This list expects a length of 14 starting with "ניסן" and ending with 3 variations of Adar -
523         * "אדר", "אדר ב", "אדר".
524         * 
525         * @param hebrewMonths the array of Hebrew months beginning in "ניסן" and ending in "אדר", "אדר ב", "אדר א"
526         * @see #getHebrewMonthList()
527         */
528        public void setHebrewMonthList(String[] hebrewMonths) {
529                if(hebrewMonths.length !=14) {
530                        throw new IllegalArgumentException("The Hebrew month array must have a length of 14.");
531                }
532                this.hebrewMonths = hebrewMonths;
533        }
534
535        /**
536         * Returns the array of months transliterated into Latin chars. The default list of months uses Ashkenazi pronunciation in
537         * typical American English spelling. This list has a length of 14 with 3 variations for Adar - "Adar", "Adar II", "Adar I".
538         * The array of months beginn in Nissan and end in "Adar", "Adar II", "Adar I". The default list is
539         * <code>["Nissan", "Iyar", "Sivan", "Tammuz", "Av", "Elul", "Tishrei", "Cheshvan", "Kislev", "Teves", "Shevat", "Adar",
540         * "Adar II", "Adar I"]</code>.
541         * 
542         * @return the array of 14 month names beginning in Nissan and ending in "Adar", "Adar II", "Adar I".
543         * @see #setTransliteratedMonthList(String[])
544         */
545        public String[] getTransliteratedMonthList() {
546                return transliteratedMonths;
547        }
548
549        /**
550         * Setter method to allow overriding of the default list of months transliterated into Latin chars. The default list uses
551         * Ashkenazi American English transliteration. The array of 14 transliterated month names begin in "Nissan" and end in the
552         * 3 Adar variations - "Adar", "Adar II", "Adar I". The default list is
553         * <code>["Nissan", "Iyar", "Sivan", "Tammuz", "Av", "Elul", "Tishrei", "Cheshvan", "Kislev", "Teves", "Shevat", "Adar",
554         * "Adar II", "Adar I"]</code>.
555         * 
556         * @param transliteratedMonths the array of 14 month names beginning in Nissan and ending in "Adar", "Adar II", "Adar I".
557         * @see #getTransliteratedMonthList()
558         */
559        public void setTransliteratedMonthList(String[] transliteratedMonths) {
560                if(transliteratedHolidays.length !=14) {
561                        throw new IllegalArgumentException("The transliterated month array must have a length of 14.");
562                }
563                this.transliteratedMonths = transliteratedMonths;
564        }
565
566        /**
567         * List of Hebrew months. The list has* a length of 14 starting with "ניסן" and ending with the 3 variations of Adar -
568         * "אדר", "אדר ב", "אדר א".
569         * 
570         * @see #getHebrewMonthList()
571         * @see #setHebrewMonthList(String[])
572         * @see #formatMonth(JewishDate)
573         */
574        private String[] hebrewMonths = { "ניסן", "אייר",
575                        "סיון", "תמוז", "אב", "אלול",
576                        "תשרי", "חשון", "כסלו",
577                        "טבת", "שבט", "אדר", "אדר ב",
578                        "אדר א" };
579
580        /**
581         * Unicode list of Hebrew days of week in the format of <code>&rlm;["ראשון", "שני", "שלישי", "רביעי", "חמישי", "ששי", "שבת"]</code>
582         */
583        private static final String[] hebrewDaysOfWeek = { "ראשון", "שני", "שלישי", "רביעי", "חמישי", "ששי", "שבת" };
584
585        /**
586         * Formats the day of week. If {@link #isHebrewFormat() Hebrew formatting} is set, it will display in the format ראשון etc. If
587         * Hebrew formatting is not in use it will return it in the format of Sunday etc. There are various formatting options that will
588         * affect the output.
589         * 
590         * @param jewishDate the JewishDate Object
591         * @return the formatted day of week
592         * @see #isHebrewFormat()
593         * @see #isLongWeekFormat()
594         */
595        public String formatDayOfWeek(JewishDate jewishDate) {
596                if (hebrewFormat) {
597                        if (isLongWeekFormat()) {
598                                return hebrewDaysOfWeek[jewishDate.getDayOfWeek() - 1];
599                        } else {
600                                if (jewishDate.getDayOfWeek() == 7) {
601                                        return formatHebrewNumber(300);
602                                } else {
603                                        return formatHebrewNumber(jewishDate.getDayOfWeek());
604                                }
605                        }
606                } else {
607                        if (jewishDate.getDayOfWeek() == 7) {
608                                if (isLongWeekFormat()) {
609                                        return getTransliteratedShabbosDayOfWeek();
610                                } else {
611                                        return getTransliteratedShabbosDayOfWeek().substring(0,3);
612                                }
613                        } else {
614                                return weekFormat.format(jewishDate.getLocalDate());
615                        }
616                }
617        }
618
619        /**
620         * Returns whether the class is set to use the Geresh ׳ and Gershayim ״ in formatting Hebrew dates and numbers. When true and
621         * output would look like כ״א שבט תש״כ (or כ״א שבט תש״ך). When set to false, this output would display as כא שבט תשכ.
622         * 
623         * @return true if set to use the Geresh ׳ and Gershayim ״ in formatting Hebrew dates and numbers.
624         */
625        public boolean isUseGershGershayim() {
626                return useGershGershayim;
627        }
628
629        /**
630         * Sets whether to use the Geresh ׳ and Gershayim ״ in formatting Hebrew dates and numbers. The default value is true and output
631         * would look like כ״א שבט תש״כ (or כ״א שבט תש״ך). When set to false, this output would display as כא שבט תשכ (or כא שבט תשך).
632         * Single digit days or month or years such as כ׳ שבט ו׳ אלפים show the use of the Geresh.
633         * 
634         * @param useGershGershayim set this to false to omit the Geresh ׳ and Gershayim ״ in formatting
635         */
636        public void setUseGershGershayim(boolean useGershGershayim) {
637                this.useGershGershayim = useGershGershayim;
638        }
639
640        /**
641         * Returns whether the class is set to use the מנצפ״ך letters when formatting years ending in 20, 40, 50, 80 and 90 to produce
642         * תש״פ if false or תש״ף if true. Traditionally non-final form letters are used, so the year 5780 would be formatted as תש״פ if
643         * the default false is used here. If this returns true, the format תש״ף would be used.
644         * 
645         * @return true if set to use final form letters when formatting Hebrew years. The default value is false.
646         */
647        public boolean isUseFinalFormLetters() {
648                return useFinalFormLetters;
649        }
650
651        /**
652         * When formatting a Hebrew Year, traditionally years ending in 20, 40, 50, 80 and 90 are formatted using non-final form letters
653         * for example תש״פ for the year 5780. Setting this to true (the default is false) will use the final form letters for מנצפ״ך and
654         * will format the year 5780 as תש״ף.
655         * 
656         * @param useFinalFormLetters Set this to true to use final form letters when formatting Hebrew years.
657         */
658        public void setUseFinalFormLetters(boolean useFinalFormLetters) {
659                this.useFinalFormLetters = useFinalFormLetters;
660        }
661
662        /**
663         * Returns whether the class is set to use the thousands digit when formatting a Hebrew Year. Traditionally the thousands digit
664         * is omitted and output for a year such as 5729 (1969 Gregorian) would be calculated as 729 and formatted as תשכ״ט. When set to
665         * true the long format year such,  as ה׳ תשכ״ט for 5729/1969 is returned.
666         * 
667         * @return true if set to use the thousands digit when formatting Hebrew dates and numbers.
668         */
669        public boolean isUseLongHebrewYears() {
670                return useLonghebrewYears;
671        }
672
673        /**
674         * When formatting a Hebrew Year, traditionally the thousands digit is omitted and output for a year such as 5729 (1969 
675         * Gregorian) would be calculated for 729 and format as תשכ״ט. This method allows setting this to true to return the long format
676         * year such as ה׳ תשכ״ט for 5729/1969.
677         * 
678         * @param useLongHebrewYears Set this to true to use the long formatting
679         */
680        public void setUseLongHebrewYears(boolean useLongHebrewYears) {
681                this.useLonghebrewYears = useLongHebrewYears;
682        }
683        /**
684         * Formats the Jewish date. If the formatter is set to Hebrew, it will format in the form, "day Month year" for example
685         * כ״א שבט תשכ״ט, and the format "21 Shevat, 5729" if not.
686         * 
687         * @param jewishDate the JewishDate to be formatted
688         * @return the formatted date. If the formatter is set to Hebrew, it will format in the form, "day Month year" as כ״א שבט תשכ״ט,
689         *         and "21 Shevat, 5729" if not.
690         */
691        public String format(JewishDate jewishDate) {
692                if (isHebrewFormat()) {
693                        return formatHebrewNumber(jewishDate.getJewishDayOfMonth()) + " " + formatMonth(jewishDate) + " "
694                                        + formatHebrewNumber(jewishDate.getJewishYear());
695                } else {
696                        return jewishDate.getJewishDayOfMonth() + " " + formatMonth(jewishDate) + ", " + jewishDate.getJewishYear();
697                }
698        }
699
700        /**
701         * Returns a string of the current Hebrew month formatted as "אדר ב׳" or "Adar II" depending on how {@link #isHebrewFormat()}
702         * is set.
703         * 
704         * @param jewishDate the JewishDate to format
705         * @return the formatted month name formatted as "אדר ב׳" or "Adar II" depending on how {@link #isHebrewFormat()} is set.
706         * @see #isHebrewFormat()
707         * @see #setHebrewFormat(boolean)
708         * @see #getTransliteratedMonthList()
709         * @see #setTransliteratedMonthList(String[])
710         */
711        public String formatMonth(JewishDate jewishDate) {
712                final int month = jewishDate.getJewishMonth();
713                if (isHebrewFormat()) {
714                        if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR) {
715                                return hebrewMonths[JewishDate.ADAR_II] + (useGershGershayim ? GERESH : ""); // return Adar I, not Adar in a leap year
716                        } else if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR_II) {
717                                return hebrewMonths[JewishDate.ADAR] + (useGershGershayim ? GERESH : "");
718                        } else {
719                                return hebrewMonths[month - 1];
720                        }
721                } else {
722                        if (jewishDate.isJewishLeapYear() && month == JewishDate.ADAR) {
723                                return transliteratedMonths[JewishDate.ADAR_II]; // return Adar I, not Adar in a leap year
724                        } else {
725                                return transliteratedMonths[month - 1];
726                        }
727                }
728        }
729
730        /**
731         * Returns a String of the Omer day in the form ל״ג בעומר if Hebrew Format is set, or "Omer X" or "Lag B'Omer" if not. An empty
732         * string if there is no Omer this day.
733         * 
734         * @param jewishCalendar the JewishCalendar to be formatted
735         * @return a String of the Omer day in the form or an empty string if there is no Omer this day. The default formatting has a
736         * ב prefix that would output בעומר, but this can be set via the {@link #setHebrewOmerPrefix(String)} method to use a ל and
737         *         output ל״ג לעומר.
738         * @see #isHebrewFormat()
739         * @see #getHebrewOmerPrefix()
740         * @see #setHebrewOmerPrefix(String)
741         */
742        public String formatOmer(JewishCalendar jewishCalendar) {
743                int omer = jewishCalendar.getDayOfOmer();
744                if (omer == -1) {
745                        return "";
746                }
747                if (hebrewFormat) {
748                        return formatHebrewNumber(omer) + " " + hebrewOmerPrefix + "עומר";
749                } else {
750                        if (omer == 33) { // if Lag B'Omer
751                                return transliteratedHolidays[33];
752                        } else {
753                                return "Omer " + omer;
754                        }
755                }
756        }
757
758        /**
759         * Returns the kviah in the traditional 3 letter Hebrew format where the first letter represents the day of week of Rosh Hashana,
760         * the second letter represents the lengths of Cheshvan and Kislev ({@link JewishDate#SHELAIMIM Shelaimim} , {@link
761         * JewishDate#KESIDRAN Kesidran} or {@link JewishDate#CHASERIM Chaserim}) and the 3rd letter represents the day of week of Pesach.
762         * For example 5729 (1969) would return בשה (Rosh Hashana on Monday, Shelaimim, and Pesach on Thursday), while 5771 (2011) would
763         * return השג (Rosh Hashana on Thursday, Shelaimim, and Pesach on Tuesday).
764         * 
765         * @param jewishYear the Jewish year
766         * @return the Hebrew String such as בשה for 5729 (1969) and השג for 5771 (2011).
767         */
768        public String getFormattedKviah(int jewishYear) {
769                JewishDate jewishDate = new JewishDate(jewishYear, JewishDate.TISHREI, 1); // set date to Rosh Hashana
770                int kviah = jewishDate.getCheshvanKislevKviah();
771                int roshHashanaDayOfWeek = jewishDate.getDayOfWeek();
772                String returnValue = formatHebrewNumber(roshHashanaDayOfWeek);
773                returnValue += (kviah == JewishDate.CHASERIM ? "ח" : kviah == JewishDate.SHELAIMIM ? "ש" : "כ");
774                jewishDate.setJewishDate(jewishYear, JewishDate.NISSAN, 15); // set to Pesach of the given year
775                int pesachDayOfWeek = jewishDate.getDayOfWeek();
776                returnValue += formatHebrewNumber(pesachDayOfWeek);
777                returnValue = returnValue.replaceAll(GERESH, "");// geresh is never used in the kviah format
778                // boolean isLeapYear = JewishDate.isJewishLeapYear(jewishYear);
779                // for efficiency we can avoid the expensive recalculation of the pesach day of week by adding 1 day to Rosh
780                // Hashana for a 353-day year, 2 for a 354-day year, 3 for a 355 or 383-day year, 4 for a 384-day year and 5 for
781                // a 385-day year
782                return returnValue;
783        }
784
785        /**
786         * Formats the <a href="https://en.wikipedia.org/wiki/Daf_Yomi">Daf Yomi</a> Bavli in the format of "עירובין נ״ב" if {@link
787         * #isHebrewFormat()} is set to <code>true</code>, or the transliterated format of "Eruvin 52" if set to <code>false</code>.
788         * @param daf the Daf to be formatted.
789         * @return the formatted daf.
790         */
791        public String formatDafYomiBavli(Daf daf) {
792                if (hebrewFormat) {
793                        return daf.getMasechta() + " " + formatHebrewNumber(daf.getDaf());
794                } else {
795                        return daf.getMasechtaTransliterated() + " " + daf.getDaf();
796                }
797        }
798        
799        /**
800         * Formats the <a href="https://en.wikipedia.org/wiki/Jerusalem_Talmud#Daf_Yomi_Yerushalmi">Daf Yomi Yerushalmi</a> in the format
801         * of "עירובין נ״ב" in {@link #isHebrewFormat()} is set to <code>true</code>, or the transliterated format of "Eruvin 52" if set to
802         * <code>false</code>.
803         * 
804         * @param daf the Daf to be formatted.
805         * @return the formatted daf.
806         */
807        public String formatDafYomiYerushalmi(Daf daf) {
808                if (daf == null) {
809                        if (hebrewFormat) {
810                                return Daf.getYerushalmiMasechtos()[39];
811                        } else {
812                                return Daf.getYerushalmiMasechtosTransliterated()[39];
813                        }
814                }
815                if (hebrewFormat) {                     
816                        return daf.getYerushalmiMasechta() + " " + formatHebrewNumber(daf.getDaf());
817                } else {
818                        return daf.getYerushalmiMasechtaTransliterated() + " " + daf.getDaf();
819                }
820        }
821
822        /**
823         * Returns a Hebrew formatted string of a number. The method can calculate from 0 to 9999.
824         * <ul>
825         * <li>Single digit numbers such as 3, 30 and 100 will be returned with a ׳ (<a
826         * href="http://en.wikipedia.org/wiki/Geresh">Geresh</a>) appended as at the end. For example ג׳, ל׳ and ק׳</li>
827         * <li>multi digit numbers such as 21 and 769 will be returned with a ״ (<a
828         * href="http://en.wikipedia.org/wiki/Gershayim">Gershayim</a>) between the second to last and last letters. For
829         * example כ״א, תשכ״ט</li>
830         * <li>15 and 16 will be returned as ט״ו and ט״ז</li>
831         * <li>Single digit numbers (years assumed) such as 6000 (%1000=0) will be returned as ו׳אלפים</li>
832         * <li>0 will return אפס</li>
833         * </ul>
834         * 
835         * @param number the number to be formatted. It will throw an IllegalArgumentException if the number is &lt; 0 or &gt; 9999.
836         * @return the Hebrew formatted number such as תשכ״ט
837         * @see #isUseFinalFormLetters()
838         * @see #isUseGershGershayim()
839         * @see #isHebrewFormat()
840         * 
841         */
842        public String formatHebrewNumber(int number) {
843                if (number < 0) {
844                        throw new IllegalArgumentException("negative numbers can't be formatted");
845                } else if (number > 9999) {
846                        throw new IllegalArgumentException("numbers > 9999 can't be formatted");
847                }
848
849                String ALAFIM = "אלפים";
850                String EFES = "אפס";
851
852                String[] jHundreds = new String[] { "", "ק", "ר", "ש", "ת", "תק", "תר", "תש", "תת", "תתק" };
853                String[] jTens = new String[] { "", "י", "כ", "ל", "מ", "נ", "ס", "ע", "פ", "צ" };
854                String[] jTenEnds = new String[] { "", "י", "ך", "ל", "ם", "ן", "ס", "ע", "ף", "ץ" };
855                String[] tavTaz = new String[] { "טו", "טז" };
856                String[] jOnes = new String[] { "", "א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט" };
857
858                if (number == 0) { // do we really need this? Should it be applicable to a date?
859                        return EFES;
860                }
861                int shortNumber = number % 1000; // discard thousands
862                // next check for all possible single Hebrew digit years
863                boolean singleDigitNumber = (shortNumber < 11 || (shortNumber < 100 && shortNumber % 10 == 0) ||
864                                (shortNumber <= 400 && shortNumber % 100 == 0));
865                int thousands = number / 1000; // get # thousands
866                StringBuilder sb = new StringBuilder();
867                // append thousands to String
868                if (number % 1000 == 0) { // in year is 5000, 4000 etc
869                        sb.append(jOnes[thousands]);
870                        if (isUseGershGershayim()) {
871                                sb.append(GERESH);
872                        }
873                        sb.append(" ");
874                        sb.append(ALAFIM); // add # of thousands plus the word "thousand" (override alafim boolean)
875                        return sb.toString();
876                } else if (useLonghebrewYears && number >= 1000) { // if alafim boolean display thousands
877                        sb.append(jOnes[thousands]);
878                        if (isUseGershGershayim()) {
879                                sb.append(GERESH); // append thousands quote
880                        }
881                        sb.append(" ");
882                }
883                number = number % 1000; // remove 1000s
884                int hundreds = number / 100; // # of hundreds
885                sb.append(jHundreds[hundreds]); // add hundreds to String
886                number = number % 100; // remove 100s
887                if (number == 15) { // special case 15
888                        sb.append(tavTaz[0]);
889                } else if (number == 16) { // special case 16
890                        sb.append(tavTaz[1]);
891                } else {
892                        int tens = number / 10;
893                        if (number % 10 == 0) { // if evenly divisible by 10
894                                if (!singleDigitNumber) {
895                                        if (isUseFinalFormLetters()) {
896                                                sb.append(jTenEnds[tens]); // years like 5780 will end with a final form ף
897                                        } else {
898                                                sb.append(jTens[tens]); // years like 5780 will end with a regular פ
899                                        }
900                                } else {
901                                        sb.append(jTens[tens]); // standard letters so years like 5050 will end with a regular nun
902                                }
903                        } else {
904                                sb.append(jTens[tens]);
905                                number = number % 10;
906                                sb.append(jOnes[number]);
907                        }
908                }
909                if (isUseGershGershayim()) {
910                        if (singleDigitNumber) {
911                                sb.append(GERESH); // append single quote
912                        } else { // append double quote before last digit
913                                sb.insert(sb.length() - 1, GERSHAYIM);
914                        }
915                }
916                return sb.toString();
917        }       
918
919        /**
920         * Returns the map of transliterated parshiyos used by this formatter. This list using the default <em>Ashkenazi</em>
921         * pronunciation. This list can be overridden (for <em>Sephardi</em> English transliteration for example) by setting the
922         * {@link #setTransliteratedParshiosList(EnumMap)}. The list includes double and special <em>parshiyos</em> in the following
923         * order and spelling "<em>Bereshis, Noach, Lech Lecha, Vayera, Chayei Sara, Toldos, Vayetzei, Vayishlach, Vayeshev, Miketz,
924         * Vayigash, Vayechi, Shemos, Vaera, Bo, Beshalach, Yisro, Mishpatim, Terumah, Tetzaveh, Ki Sisa, Vayakhel, Pekudei, Vayikra,
925         * Tzav, Shmini, Tazria, Metzora, Achrei Mos, Kedoshim, Emor, Behar, Bechukosai, Bamidbar, Nasso, Beha'aloscha, Sh'lach,
926         * Korach, Chukas, Balak, Pinchas, Matos, Masei, Devarim, Vaeschanan, Eikev, Re'eh, Shoftim, Ki Seitzei, Ki Savo, Nitzavim,
927         * Vayeilech, Ha'Azinu, Vezos Habracha, Vayakhel Pekudei, Tazria Metzora, Achrei Mos Kedoshim, Behar Bechukosai, Chukas Balak,
928         * Matos Masei, Nitzavim Vayeilech, Shekalim, Zachor, Parah, Hachodesh,Shuva, Shira, Hagadol, Chazon, Nachamu</em>".
929         * 
930         * @return the map of transliterated Parshios
931         * @see #setTransliteratedParshiosList(EnumMap)
932         * @see #formatParsha(JewishCalendar)
933         * @see #formatParsha(JewishCalendar.Parsha)
934         */
935        public EnumMap<JewishCalendar.Parsha, String> getTransliteratedParshiosList() {
936                return transliteratedParshaMap;
937        }
938
939        /**
940         * Setter method to allow overriding of the default list of parshiyos transliterated into Latin chars. The
941         * default uses Ashkenazi American English transliteration.
942         * 
943         * @param transliteratedParshaMap the transliterated Parshios as an EnumMap to set
944         * @see #getTransliteratedParshiosList() for information on the format.
945         */
946        public void setTransliteratedParshiosList(EnumMap<JewishCalendar.Parsha, String> transliteratedParshaMap) {
947                this.transliteratedParshaMap = transliteratedParshaMap;
948        }
949        
950        /**
951         * Returns a String with the name of the current parsha(ios). This method gets the current <em>parsha</em> by calling {@link
952         * JewishCalendar#getParshah()} that does not return a <em>parsha</em> for any non-<em>Shabbos</em> or a <em>Shabbos</em> that
953         * occurs on a <em>Yom Tov</em>, and will return an empty <code>String</code> in those cases. If the class {@link
954         * #isHebrewFormat() is set to format in Hebrew} it will return a <code>String</code> of the current parsha(ios) in Hebrew for
955         * example בראשית or נצבים וילך for a double parsha, or an empty string will be returned if there is not parsha that week. If not set
956         * to Hebrew, it returns a string of the parsha(ios) transliterated into Latin chars. The default uses Ashkenazi pronunciation
957         * in typical American English spelling, for example Bereshis, Nitzavim Vayeilech for a double parsha, An empty string if there
958         * are none.
959         * 
960         * @param jewishCalendar the JewishCalendar Object
961         * @return today's parsha(ios) in Hebrew for example, if the formatter is set to format in Hebrew, returns a string of the current
962         *         parsha(ios) in Hebrew for example בראשית or נצבים וילך, for a double parsha or an empty <code>String</code> if there is
963         *         no parsha that week. If not set to Hebrew, it returns a string of the parsha(ios) transliterated into Latin chars. The
964         *         default uses Ashkenazi pronunciation in typical American English spelling, for example Bereshis, Nitzavim Vayeilech for
965         *         a double parsha, or an empty <code>String</code> if there are none.
966         * @see #formatParsha(JewishCalendar)
967         * @see #isHebrewFormat()
968         * @see JewishCalendar#getParshah()
969         */
970        public String formatParsha(JewishCalendar jewishCalendar) {
971                JewishCalendar.Parsha parsha =  jewishCalendar.getParshah();
972                return formatParsha(parsha);
973        }
974
975        /**
976         * Returns a <code>String</code> with the name of the current parsha(ios). This method overloads {@link
977         * #formatParsha(JewishCalendar)} and unlike that method, it will format the <em>parsha</em> passed to this method regardless of
978         * the day of week. This is the way to format a <em>parsha</em> retrieved from calling
979         * {@link JewishCalendar#getUpcomingParshah()}.
980         *
981         * @param parsha a JewishCalendar.Parsha object
982         * @return today's parsha(ios) in Hebrew for example, if the formatter is set to format in Hebrew, returns a <code>String</code>
983         *         of the current parsha(ios) in Hebrew for example בראשית or נצבים וילך for a double parsha, or an empty <code>String</code>
984         *         if there is no parsha that week. If not set to Hebrew, it returns a string of the parsha(ios) transliterated into
985         *         Latin chars. The default uses Ashkenazi pronunciation in typical American English spelling, for example Bereshis,  or
986         *         Nitzavim Vayeilech for a double parsha, or an empty string if there are none.
987         * @see #formatParsha(JewishCalendar)
988         * @see JewishCalendar#getUpcomingParshah()
989         */
990        public String formatParsha(JewishCalendar.Parsha parsha) {
991                return hebrewFormat ? hebrewParshaMap.get(parsha) : transliteratedParshaMap.get(parsha);
992        }
993        
994        /**
995         * Returns a String with the name of the current special parsha of Shekalim, Zachor, Parah or Hachodesh or an empty String for a
996         * non-special parsha. If the formatter is set to format in Hebrew, it returns a string of the current special parsha in Hebrew,
997         * for example שקלים, זכור, פרה or החדש, or an empty <code>string</code> if the date is not a special parsha. If not set to Hebrew,
998         * it returns a string of the special parsha transliterated into Latin chars. The default uses Ashkenazi pronunciation in typical
999         * American English spelling Shekalim, Zachor, Parah or Hachodesh.
1000         * 
1001         * @param jewishCalendar the JewishCalendar Object
1002         * @return today's special parsha. If the formatter is set to format in Hebrew, returns a string of the current special parsha in
1003         *         Hebrew for in the format of שקלים, זכור, פרה or החדש or an empty string if there are none. If not set to Hebrew, it
1004         *         returns a string of the special parsha transliterated into Latin chars. The default uses Ashkenazi pronunciation in
1005         *         typical American English spelling of Shekalim, Zachor, Parah or Hachodesh. An empty string if there are none.
1006         */
1007        public String formatSpecialParsha(JewishCalendar jewishCalendar) {
1008                JewishCalendar.Parsha specialParsha =  jewishCalendar.getSpecialShabbos();
1009                return hebrewFormat ? hebrewParshaMap.get(specialParsha) : transliteratedParshaMap.get(specialParsha);
1010        }
1011}