View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.time;
18  
19  import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.junit.jupiter.api.Assumptions.assumeFalse;
24  import static org.junit.jupiter.api.Assumptions.assumeTrue;
25  
26  import java.io.Serializable;
27  import java.text.DateFormatSymbols;
28  import java.text.FieldPosition;
29  import java.text.ParseException;
30  import java.text.SimpleDateFormat;
31  import java.util.Calendar;
32  import java.util.Date;
33  import java.util.GregorianCalendar;
34  import java.util.Locale;
35  import java.util.TimeZone;
36  
37  import org.apache.commons.lang3.AbstractLangTest;
38  import org.apache.commons.lang3.JavaVersion;
39  import org.apache.commons.lang3.SerializationUtils;
40  import org.apache.commons.lang3.SystemUtils;
41  import org.junit.jupiter.api.Test;
42  import org.junitpioneer.jupiter.DefaultLocale;
43  import org.junitpioneer.jupiter.DefaultTimeZone;
44  import org.junitpioneer.jupiter.cartesian.ArgumentSets;
45  import org.junitpioneer.jupiter.cartesian.CartesianTest;
46  
47  /**
48   * Tests {@link FastDatePrinter}.
49   */
50  class FastDatePrinterTest extends AbstractLangTest {
51  
52      private enum Expected1806 {
53          India(INDIA, "+05", "+0530", "+05:30"), Greenwich(TimeZones.GMT, "Z", "Z", "Z"), NewYork(NEW_YORK, "-05", "-0500", "-05:00");
54  
55          final TimeZone zone;
56  
57          final String one;
58          final String two;
59          final String three;
60  
61          Expected1806(final TimeZone zone, final String one, final String two, final String three) {
62              this.zone = zone;
63              this.one = one;
64              this.two = two;
65              this.three = three;
66          }
67      }
68  
69      private static final String YYYY_MM_DD = "yyyy/MM/dd";
70      private static final TimeZone NEW_YORK = TimeZones.getTimeZone("America/New_York");
71      private static final TimeZone INDIA = TimeZones.getTimeZone("Asia/Calcutta");
72  
73      private static final Locale SWEDEN = new Locale("sv", "SE");
74  
75      private static Calendar initializeCalendar(final TimeZone tz) {
76          final Calendar cal = Calendar.getInstance(tz);
77          cal.set(Calendar.YEAR, 2001);
78          cal.set(Calendar.MONTH, 1); // not daylight savings
79          cal.set(Calendar.DAY_OF_MONTH, 4);
80          cal.set(Calendar.HOUR_OF_DAY, 12);
81          cal.set(Calendar.MINUTE, 8);
82          cal.set(Calendar.SECOND, 56);
83          cal.set(Calendar.MILLISECOND, 235);
84          return cal;
85      }
86  
87      static ArgumentSets testStandaloneMonthForMonthOnlyPatternFactory() {
88          // Month-only patterns, with or without literals, use the standalone form; patterns with another field keep the format form.
89          return ArgumentSets.argumentsForFirstParameter(Locale.US, Locale.GERMANY, new Locale("ru"), new Locale("cs"), new Locale("pl"), new Locale("fi"))
90                  .argumentsForNextParameter("M", "MM", "MMM", "MMMM", "MMMMM", "'m:'MMMM", "MMMM''", "MMMM,", "MMMM-", "MMMM ", "d MMMM", "MMMM yyyy",
91                          "MMM MMM", "MMM LLL", "MMMM LLLL", "LLL", "LLLL")
92                  .argumentsForNextParameter(Calendar.JANUARY, Calendar.FEBRUARY, Calendar.MARCH, Calendar.APRIL, Calendar.MAY, Calendar.JUNE, Calendar.JULY,
93                          Calendar.AUGUST, Calendar.SEPTEMBER, Calendar.OCTOBER, Calendar.NOVEMBER, Calendar.DECEMBER);
94      }
95  
96      private DatePrinter getDateInstance(final int dateStyle, final Locale locale) {
97          return getInstance(AbstractFormatCache.getPatternForStyle(Integer.valueOf(dateStyle), null, locale), TimeZone.getDefault(), Locale.getDefault());
98      }
99  
100     DatePrinter getInstance(final String format) {
101         return getInstance(format, TimeZone.getDefault(), Locale.getDefault());
102     }
103 
104     private DatePrinter getInstance(final String format, final Locale locale) {
105         return getInstance(format, TimeZone.getDefault(), locale);
106     }
107 
108     private DatePrinter getInstance(final String format, final TimeZone timeZone) {
109         return getInstance(format, timeZone, Locale.getDefault());
110     }
111 
112     /**
113      * Gets the printer instance to use for testing. Override this method in derived tests to change how instances are constructed.
114      *
115      * @param format   The format string to use
116      * @param timeZone The time zone to use
117      * @param locale   The locale to use
118      * @return The DatePrinter to use for testing
119      */
120     protected DatePrinter getInstance(final String format, final TimeZone timeZone, final Locale locale) {
121         return new FastDatePrinter(format, timeZone, locale);
122     }
123 
124     @Test
125     void test1806() {
126         for (final Expected1806 trial : Expected1806.values()) {
127             final Calendar cal = initializeCalendar(trial.zone);
128 
129             DatePrinter printer = getInstance("X", trial.zone);
130             assertEquals(trial.one, printer.format(cal));
131 
132             printer = getInstance("XX", trial.zone);
133             assertEquals(trial.two, printer.format(cal));
134 
135             printer = getInstance("XXX", trial.zone);
136             assertEquals(trial.three, printer.format(cal));
137         }
138     }
139 
140     @Test
141     void test1806Argument() {
142         assertIllegalArgumentException(() -> getInstance("XXXX"));
143     }
144 
145     @Test
146     void testAppendableOptions() {
147         final DatePrinter format = getInstance("yyyy-MM-dd HH:mm:ss.SSS Z", TimeZones.GMT);
148         final Calendar calendar = Calendar.getInstance();
149         final StringBuilder sb = new StringBuilder();
150         final String expected = format.format(calendar, sb).toString();
151         sb.setLength(0);
152 
153         final Date date = calendar.getTime();
154         assertEquals(expected, format.format(date, sb).toString());
155         sb.setLength(0);
156 
157         final long epoch = date.getTime();
158         assertEquals(expected, format.format(epoch, sb).toString());
159     }
160 
161     @Test
162     void testDayNumberOfWeek() {
163         final DatePrinter printer = getInstance("u");
164         final Calendar calendar = Calendar.getInstance();
165 
166         calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
167         assertEquals("1", printer.format(calendar.getTime()));
168 
169         calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
170         assertEquals("6", printer.format(calendar.getTime()));
171 
172         calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
173         assertEquals("7", printer.format(calendar.getTime()));
174     }
175 
176     @Test
177     void testEquals() {
178         final DatePrinter printer1 = getInstance(YYYY_MM_DD);
179         final DatePrinter printer2 = getInstance(YYYY_MM_DD);
180 
181         assertEquals(printer1, printer2);
182         assertEquals(printer1.hashCode(), printer2.hashCode());
183 
184         assertNotEquals(printer1, new Object());
185     }
186 
187     /**
188      * Tests that 'G' names the era of the calendar being formatted when that calendar is not of the type the locale defaults to, like SimpleDateFormat.
189      */
190     @Test
191     void testEraMixedCalendarTypes() {
192         final Calendar cal = Calendar.getInstance(TimeZones.GMT, Locale.US);
193         cal.clear();
194         cal.set(2024, Calendar.MAY, 1);
195         final Date date = cal.getTime();
196         // Gregorian, Thai Buddhist and Japanese Imperial calendars.
197         final Locale[] locales = { Locale.US, new Locale("th", "TH"), new Locale("ja", "JP", "JP") };
198         for (final Locale locale : locales) {
199             for (final Locale calendarLocale : locales) {
200                 final Calendar calendar = Calendar.getInstance(TimeZones.GMT, calendarLocale);
201                 calendar.setTime(date);
202                 for (final String pattern : new String[] { "G", "GGGG" }) {
203                     final SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
204                     sdf.setCalendar((Calendar) calendar.clone());
205                     assertEquals(sdf.format(date), getInstance(pattern, TimeZones.GMT, locale).format(calendar),
206                             () -> locale + " " + calendar.getCalendarType() + " " + pattern);
207                 }
208             }
209         }
210     }
211 
212     /**
213      * Tests that 'G' uses the era names of the locale's calendar when that calendar is not Gregorian, like SimpleDateFormat.
214      */
215     @Test
216     void testEraNonGregorianCalendar() {
217         final Calendar cal = Calendar.getInstance(TimeZones.GMT, Locale.US);
218         cal.clear();
219         cal.set(2024, Calendar.MAY, 1);
220         final Date date = cal.getTime();
221         for (final Locale locale : new Locale[] { new Locale("th", "TH"), new Locale("ja", "JP", "JP") }) {
222             for (final String pattern : new String[] { "G", "GGGG" }) {
223                 final SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
224                 sdf.setTimeZone(TimeZones.GMT);
225                 assertEquals(sdf.format(date), getInstance(pattern, TimeZones.GMT, locale).format(date), () -> locale + " " + pattern);
226             }
227         }
228     }
229 
230     @DefaultLocale(language = "en", country = "US")
231     @DefaultTimeZone("America/New_York")
232     @Test
233     void testFormat() {
234         final GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20);
235         final GregorianCalendar cal2 = new GregorianCalendar(2003, 6, 10, 9, 0, 0);
236         final Date date1 = cal1.getTime();
237         final Date date2 = cal2.getTime();
238         final long millis1 = date1.getTime();
239         final long millis2 = date2.getTime();
240 
241         DatePrinter fdf = getInstance("yyyy-MM-dd'T'HH:mm:ss");
242         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
243         assertEquals(sdf.format(date1), fdf.format(date1));
244         assertEquals("2003-01-10T15:33:20", fdf.format(date1));
245         assertEquals("2003-01-10T15:33:20", fdf.format(cal1));
246         assertEquals("2003-01-10T15:33:20", fdf.format(millis1));
247         assertEquals("2003-07-10T09:00:00", fdf.format(date2));
248         assertEquals("2003-07-10T09:00:00", fdf.format(cal2));
249         assertEquals("2003-07-10T09:00:00", fdf.format(millis2));
250 
251         fdf = getInstance("Z");
252         assertEquals("-0500", fdf.format(date1));
253         assertEquals("-0500", fdf.format(cal1));
254         assertEquals("-0500", fdf.format(millis1));
255 
256         assertEquals("-0400", fdf.format(date2));
257         assertEquals("-0400", fdf.format(cal2));
258         assertEquals("-0400", fdf.format(millis2));
259 
260         fdf = getInstance("ZZ");
261         assertEquals("-05:00", fdf.format(date1));
262         assertEquals("-05:00", fdf.format(cal1));
263         assertEquals("-05:00", fdf.format(millis1));
264 
265         assertEquals("-04:00", fdf.format(date2));
266         assertEquals("-04:00", fdf.format(cal2));
267         assertEquals("-04:00", fdf.format(millis2));
268 
269         final String pattern = "GGGG GGG GG G yyyy yyy yy y MMMM MMM MM M LLLL LLL LL L"
270                 + " dddd ddd dd d DDDD DDD DD D EEEE EEE EE E aaaa aaa aa a zzzz zzz zz z";
271         fdf = getInstance(pattern);
272         sdf = new SimpleDateFormat(pattern);
273         // SDF bug fix starting with Java 7
274         assertEquals(sdf.format(date1).replace("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date1));
275         assertEquals(sdf.format(date2).replace("2003 03 03 03", "2003 2003 03 2003"), fdf.format(date2));
276     }
277 
278     @Test
279     void testHourFormats() {
280         final Calendar calendar = Calendar.getInstance();
281         calendar.clear();
282         final DatePrinter printer = getInstance("K k H h");
283 
284         calendar.set(Calendar.HOUR_OF_DAY, 0);
285         assertEquals("0 24 0 12", printer.format(calendar));
286 
287         calendar.set(Calendar.HOUR_OF_DAY, 12);
288         assertEquals("0 12 12 12", printer.format(calendar));
289 
290         calendar.set(Calendar.HOUR_OF_DAY, 23);
291         assertEquals("11 23 23 11", printer.format(calendar));
292     }
293 
294     @Test
295     void testLang1103() {
296         final Calendar cal = Calendar.getInstance(SWEDEN);
297         cal.set(Calendar.DAY_OF_MONTH, 2);
298 
299         assertEquals("2", getInstance("d", SWEDEN).format(cal));
300         assertEquals("02", getInstance("dd", SWEDEN).format(cal));
301         assertEquals("002", getInstance("ddd", SWEDEN).format(cal));
302         assertEquals("0002", getInstance("dddd", SWEDEN).format(cal));
303         assertEquals("00002", getInstance("ddddd", SWEDEN).format(cal));
304     }
305 
306     @Test
307     void testLang303() {
308         final Calendar cal = Calendar.getInstance();
309         cal.set(2004, Calendar.DECEMBER, 31);
310 
311         DatePrinter format = getInstance(YYYY_MM_DD);
312         final String output = format.format(cal);
313 
314         format = SerializationUtils.deserialize(SerializationUtils.serialize((Serializable) format));
315         assertEquals(output, format.format(cal));
316     }
317 
318     @Test
319     void testLang538() {
320         // more commonly constructed with: cal = new GregorianCalendar(2009, 9, 16, 8, 42, 16)
321         // for the unit test to work in any time zone, constructing with GMT-8 rather than default locale time zone
322         final GregorianCalendar cal = new GregorianCalendar(TimeZones.getTimeZone("GMT-8"));
323         cal.clear();
324         cal.set(2009, Calendar.OCTOBER, 16, 8, 42, 16);
325 
326         final DatePrinter format = getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZones.GMT);
327         assertEquals("2009-10-16T16:42:16.000Z", format.format(cal.getTime()), "dateTime");
328         assertEquals("2009-10-16T16:42:16.000Z", format.format(cal), "dateTime");
329     }
330 
331     @Test
332     void testLang645() {
333         final Locale locale = new Locale("sv", "SE");
334 
335         final Calendar cal = Calendar.getInstance();
336         cal.set(2010, Calendar.JANUARY, 1, 12, 0, 0);
337         final Date d = cal.getTime();
338 
339         final DatePrinter fdf = getInstance("EEEE', week 'ww", locale);
340 
341         assertEquals("fredag, week 53", fdf.format(d));
342     }
343 
344     /**
345      * According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916), the format method did contain a bug: it did not use the TimeZone data.
346      *
347      * This method test that the bug is fixed.
348      */
349     @Test
350     void testLang916() {
351 
352         final Calendar cal = Calendar.getInstance(TimeZones.getTimeZone("Europe/Paris"));
353         cal.clear();
354         cal.set(2009, 9, 16, 8, 42, 16);
355 
356         // calendar fast.
357         {
358             final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZones.getTimeZone("Europe/Paris")).format(cal);
359             assertEquals("2009-10-16T08:42:16 +0200", value, "calendar");
360         }
361         {
362             final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZones.getTimeZone("Asia/Kolkata")).format(cal);
363             assertEquals("2009-10-16T12:12:16 +0530", value, "calendar");
364         }
365         {
366             final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZones.getTimeZone("Europe/London")).format(cal);
367             assertEquals("2009-10-16T07:42:16 +0100", value, "calendar");
368         }
369     }
370 
371     @Test
372     void testLocaleMatches() {
373         final DatePrinter printer = getInstance(YYYY_MM_DD, SWEDEN);
374         assertEquals(SWEDEN, printer.getLocale());
375     }
376 
377     /**
378      * Tests that pre-1000AD years get padded with yyyy
379      */
380     @Test
381     void testLowYearPadding() {
382         final Calendar cal = Calendar.getInstance();
383         final DatePrinter format = getInstance(YYYY_MM_DD);
384 
385         cal.set(1, Calendar.JANUARY, 1);
386         assertEquals("0001/01/01", format.format(cal));
387         cal.set(10, Calendar.JANUARY, 1);
388         assertEquals("0010/01/01", format.format(cal));
389         cal.set(100, Calendar.JANUARY, 1);
390         assertEquals("0100/01/01", format.format(cal));
391         cal.set(999, Calendar.JANUARY, 1);
392         assertEquals("0999/01/01", format.format(cal));
393     }
394 
395     /**
396      * Show Bug #39410 is solved
397      */
398     @Test
399     void testMilleniumBug() {
400         final Calendar cal = Calendar.getInstance();
401         final DatePrinter format = getInstance("dd.MM.yyyy");
402 
403         cal.set(1000, Calendar.JANUARY, 1);
404         assertEquals("01.01.1000", format.format(cal));
405     }
406 
407     @Test
408     void testMonthOnlyPatternOnJava8() {
409         assumeFalse(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
410         final Locale[] locales = { Locale.US, Locale.GERMANY, new Locale("ru"), new Locale("cs"), new Locale("pl"), new Locale("fi") };
411         for (final Locale locale : locales) {
412             final DateFormatSymbols symbols = new DateFormatSymbols(locale);
413             for (int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++) {
414                 final Calendar cal = Calendar.getInstance(TimeZones.GMT, locale);
415                 cal.clear();
416                 cal.set(2024, month, 10);
417                 final Date date = cal.getTime();
418                 // Preserve the DateFormatSymbols names used before the Java 9 standalone-month rule.
419                 assertEquals(symbols.getShortMonths()[month], getInstance("MMM", TimeZones.GMT, locale).format(date), () -> locale + " MMM");
420                 assertEquals(symbols.getMonths()[month], getInstance("MMMM", TimeZones.GMT, locale).format(date), () -> locale + " MMMM");
421                 assertEquals(symbols.getMonths()[month], getInstance("MMMMM", TimeZones.GMT, locale).format(date), () -> locale + " MMMMM");
422                 assertEquals(symbols.getMonths()[month] + ",", getInstance("MMMM,", TimeZones.GMT, locale).format(date), () -> locale + " MMMM,");
423             }
424         }
425     }
426 
427     @Test
428     void testPatternMatches() {
429         final DatePrinter printer = getInstance(YYYY_MM_DD);
430         assertEquals(YYYY_MM_DD, printer.getPattern());
431     }
432 
433     @Test
434     void testPatternsWithoutTextMonthFields() {
435         final Calendar cal = Calendar.getInstance(TimeZones.GMT, Locale.US);
436         cal.clear();
437         cal.set(2024, Calendar.FEBRUARY, 10, 13, 45);
438         final Date date = cal.getTime();
439         final String[] patterns = { "M", "MM", "L", "LL", "yyyy-MM-dd", "'MMMM'", "'abc'", "", "HH:mm", "yyyy-dd" };
440         for (final String pattern : patterns) {
441             final SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
442             sdf.setTimeZone(TimeZones.GMT);
443             assertEquals(sdf.format(date), getInstance(pattern, TimeZones.GMT, Locale.US).format(date), () -> pattern);
444         }
445     }
446 
447     /**
448      * Test case for {@link FastDateParser#FastDateParser(String, TimeZone, Locale)}.
449      */
450     @Test
451     void testShortDateStyleWithLocales() {
452         final Locale usLocale = Locale.US;
453         final Locale swedishLocale = new Locale("sv", "SE");
454         final Calendar cal = Calendar.getInstance();
455         cal.set(2004, Calendar.FEBRUARY, 3);
456         DatePrinter fdf = getDateInstance(FastDateFormat.SHORT, usLocale);
457         assertEquals("2/3/04", fdf.format(cal));
458 
459         fdf = getDateInstance(FastDateFormat.SHORT, swedishLocale);
460         assertEquals("2004-02-03", fdf.format(cal));
461 
462     }
463 
464     /**
465      * testLowYearPadding showed that the date was buggy This test confirms it, getting 366 back as a date
466      */
467     @Test
468     void testSimpleDate() {
469         final Calendar cal = Calendar.getInstance();
470         final DatePrinter format = getInstance(YYYY_MM_DD);
471 
472         cal.set(2004, Calendar.DECEMBER, 31);
473         assertEquals("2004/12/31", format.format(cal));
474         cal.set(999, Calendar.DECEMBER, 31);
475         assertEquals("0999/12/31", format.format(cal));
476         cal.set(1, Calendar.MARCH, 2);
477         assertEquals("0001/03/02", format.format(cal));
478     }
479 
480     /**
481      * Tests that a month-only pattern uses the standalone month names, like SimpleDateFormat. In some languages, like the Slavic and Baltic ones, they are a
482      * different grammatical form from the names used next to a day: "MMMM" alone prints Russian February as nominative "февраль" where "d MMMM" prints
483      * genitive "февраля".
484      *
485      * @param locale the locale to test.
486      * @param pattern the date format pattern to test.
487      * @param month the calendar month to test.
488      * @throws ParseException if the formatted date cannot be parsed.
489      */
490     @CartesianTest
491     @CartesianTest.MethodFactory("testStandaloneMonthForMonthOnlyPatternFactory")
492     void testStandaloneMonthForMonthOnlyPattern(final Locale locale, final String pattern, final int month) throws ParseException {
493         // SimpleDateFormat forces the standalone form for a month-only pattern only since Java 9 (JDK-8114833); Java 8 has no such rule.
494         assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
495         final Calendar cal = Calendar.getInstance(TimeZones.GMT, locale);
496         cal.clear();
497         cal.set(2024, month, 10);
498         final Date date = cal.getTime();
499         final SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
500         sdf.setTimeZone(TimeZones.GMT);
501         final FastDateFormat fdf = FastDateFormat.getInstance(pattern, TimeZones.GMT, locale);
502         assertEquals(sdf.format(date), fdf.format(date), () -> locale + " " + pattern);
503         // FastDateParser accepts all name styles, so the printer's output parses back.
504         assertEquals(month, DateUtils.toCalendar(fdf.parse(fdf.format(date)), TimeZones.GMT).get(Calendar.MONTH), () -> locale + " " + pattern);
505     }
506 
507     @SuppressWarnings("deprecation")
508     @Test
509     void testStringBufferOptions() {
510         final DatePrinter format = getInstance("yyyy-MM-dd HH:mm:ss.SSS Z", TimeZones.GMT);
511         final Calendar calendar = Calendar.getInstance();
512         final StringBuffer sb = new StringBuffer();
513         final String expected = format.format(calendar, sb, new FieldPosition(0)).toString();
514         sb.setLength(0);
515         assertEquals(expected, format.format(calendar, sb).toString());
516         sb.setLength(0);
517 
518         final Date date = calendar.getTime();
519         assertEquals(expected, format.format(date, sb, new FieldPosition(0)).toString());
520         sb.setLength(0);
521         assertEquals(expected, format.format(date, sb).toString());
522         sb.setLength(0);
523 
524         final long epoch = date.getTime();
525         assertEquals(expected, format.format(epoch, sb, new FieldPosition(0)).toString());
526         sb.setLength(0);
527         assertEquals(expected, format.format(epoch, sb).toString());
528     }
529 
530     @DefaultTimeZone("UTC")
531     @Test
532     void testTimeZoneAsZ() {
533         final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
534         final FastDateFormat noColonFormat = FastDateFormat.getInstance("Z");
535         assertEquals("+0000", noColonFormat.format(c));
536 
537         final FastDateFormat isoFormat = FastDateFormat.getInstance("ZZ");
538         assertEquals("Z", isoFormat.format(c));
539 
540         final FastDateFormat colonFormat = FastDateFormat.getInstance("ZZZ");
541         assertEquals("+00:00", colonFormat.format(c));
542     }
543 
544     @Test
545     void testTimeZoneMatches() {
546         final DatePrinter printer = getInstance(YYYY_MM_DD, NEW_YORK);
547         assertEquals(NEW_YORK, printer.getTimeZone());
548     }
549 
550     @Test
551     void testToStringContainsName() {
552         final DatePrinter printer = getInstance(YYYY_MM_DD);
553         assertTrue(printer.toString().startsWith("FastDate"));
554     }
555 
556     @DefaultLocale(language = "en", country = "US")
557     @DefaultTimeZone("America/New_York")
558     @Test
559     void testWeekYear() {
560         final GregorianCalendar cal = new GregorianCalendar(2020, 12, 31, 0, 0, 0);
561         final DatePrinter printer4Digits = getInstance("YYYY");
562         final DatePrinter printer4DigitsFallback = getInstance("YYY");
563         final DatePrinter printer2Digits = getInstance("YY");
564         final DatePrinter printer4DigitAnotherFallback = getInstance("Y");
565         assertEquals("2021", printer4Digits.format(cal));
566         assertEquals("2021", printer4DigitsFallback.format(cal));
567         assertEquals("2021", printer4DigitAnotherFallback.format(cal));
568         assertEquals("21", printer2Digits.format(cal));
569     }
570 
571     @DefaultLocale(language = "en", country = "US")
572     @DefaultTimeZone("America/New_York")
573     @Test
574     void testWeekYearBc() {
575         final GregorianCalendar cal = new GregorianCalendar(42, Calendar.JULY, 15);
576         cal.set(Calendar.ERA, GregorianCalendar.BC);
577         assertEquals(-41, cal.getWeekYear());
578         assertEquals(new SimpleDateFormat("YYYY").format(cal.getTime()), getInstance("YYYY").format(cal));
579         assertEquals(new SimpleDateFormat("YYYYY").format(cal.getTime()), getInstance("YYYYY").format(cal));
580         assertEquals(new SimpleDateFormat("YY").format(cal.getTime()), getInstance("YY").format(cal));
581         assertEquals("-0041", getInstance("YYYY").format(cal));
582         assertEquals("-00041", getInstance("YYYYY").format(cal));
583         assertEquals("-41", getInstance("YY").format(cal));
584         // Padded to four digits like the AD case, see testWeekYear.
585         assertEquals("-0041", getInstance("YYY").format(cal));
586         assertEquals("-0041", getInstance("Y").format(cal));
587         // The week year of 1 BC is 0, which the digit rules already render.
588         final GregorianCalendar oneBc = new GregorianCalendar(1, Calendar.JULY, 15);
589         oneBc.set(Calendar.ERA, GregorianCalendar.BC);
590         assertEquals("0000", getInstance("YYYY").format(oneBc));
591     }
592 }