DateCalendarConverter { public String convert(String inputDateStr) { if (inputDateStr == null || "".equals(inputDateStr.trim())) { return inputDateStr; } if (inputChronologyType.equals(outputChronologyType) && inputFormatPattern.equals(outputFormatPattern)) { return inputDateStr; } LocalDate inputLocalDate = parseStringToDate(inputDateStr); return formatDateToString(inputLocalDate); } DateCalendarConverter();  DateCalendarConverter(Chronology inputChronologyType, Chronology outputChronologyType);  DateCalendarConverter(String inputFormatPattern, String outputFormatPattern);  DateCalendarConverter(String inputFormatPattern, String outputFormatPattern, Chronology inputChronologyType,
            Chronology outputChronologyType, Locale inputLocale, Locale outputLocale);  DateCalendarConverter(String inputFormatPattern, String outputFormatPattern, Chronology inputChronologyType,
            Chronology outputChronologyType); String convert(String inputDateStr); static final String DEFAULT_PATTERN; static final String DEFAULT_PATTERN_WITH_ERA; static final Locale DEFAULT_OUTPUT_LOCALE; static final Locale DEFAULT_INPUT_LOCALE; }
@Test public void testConvert_IsoDateTo() { assertEquals(HIJRAH_STR, new DateCalendarConverter(IsoChronology.INSTANCE, HijrahChronology.INSTANCE).convert(ISO_STR)); assertEquals(JAPANESE_STR, new DateCalendarConverter(IsoChronology.INSTANCE, JapaneseChronology.INSTANCE).convert(ISO_STR)); assertEquals(JAPANESE_STR_1, new DateCalendarConverter(IsoChronology.INSTANCE, JapaneseChronology.INSTANCE).convert(ISO_STR_1)); if (isReiwaEraSupported()) { assertEquals(JAPANESE_STR_2, new DateCalendarConverter(IsoChronology.INSTANCE, JapaneseChronology.INSTANCE).convert(ISO_STR_2)); } assertEquals(MINGUO_STR, new DateCalendarConverter(IsoChronology.INSTANCE, MinguoChronology.INSTANCE).convert(ISO_STR)); assertEquals(THAIBUDDHIST_STR, new DateCalendarConverter(IsoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE).convert(ISO_STR)); assertEquals(ISO_STR2, new DateCalendarConverter(PATTERN, PATTERN2, IsoChronology.INSTANCE, IsoChronology.INSTANCE) .convert(ISO_STR)); assertEquals(ISO_STR3, new DateCalendarConverter(PATTERN, PATTERN3, IsoChronology.INSTANCE, IsoChronology.INSTANCE) .convert(ISO_STR)); assertEquals(ISO_STR4, new DateCalendarConverter(PATTERN, PATTERN4, IsoChronology.INSTANCE, IsoChronology.INSTANCE) .convert(ISO_STR)); assertEquals(ISO_STR5, new DateCalendarConverter(PATTERN, PATTERN5, IsoChronology.INSTANCE, IsoChronology.INSTANCE) .convert(ISO_STR)); assertEquals(HIJRA_STR1, new DateCalendarConverter(PATTERN, PATTERN1, IsoChronology.INSTANCE, HijrahChronology.INSTANCE) .convert(ISO_STR)); assertEquals(HIJRA_STR1, new DateCalendarConverter(PATTERN1, PATTERN1, IsoChronology.INSTANCE, HijrahChronology.INSTANCE) .convert(ISO_STR1)); assertEquals(HIJRAH_STR, new DateCalendarConverter(PATTERN1, PATTERN, IsoChronology.INSTANCE, HijrahChronology.INSTANCE) .convert(ISO_STR1)); final String convertedHijrahDate = new DateCalendarConverter(PATTERN1, PATTERN5, IsoChronology.INSTANCE, HijrahChronology.INSTANCE) .convert(ISO_STR1); assertTrue(HIJRAH_STR5_JAVA8.equals(convertedHijrahDate) || HIJRAH_STR5.equals(convertedHijrahDate)); assertEquals(JAPANESE_STR1, new DateCalendarConverter(PATTERN, PATTERN1, IsoChronology.INSTANCE, JapaneseChronology.INSTANCE) .convert(ISO_STR)); assertEquals(JAPANESE_STR1, new DateCalendarConverter(PATTERN1, PATTERN1, IsoChronology.INSTANCE, JapaneseChronology.INSTANCE) .convert(ISO_STR1)); assertEquals(JAPANESE_STR, new DateCalendarConverter(PATTERN1, PATTERN, IsoChronology.INSTANCE, JapaneseChronology.INSTANCE) .convert(ISO_STR1)); assertEquals(MINGUO_STR1, new DateCalendarConverter(PATTERN, PATTERN1, IsoChronology.INSTANCE, MinguoChronology.INSTANCE) .convert(ISO_STR)); assertEquals(MINGUO_STR1, new DateCalendarConverter(PATTERN1, PATTERN1, IsoChronology.INSTANCE, MinguoChronology.INSTANCE) .convert(ISO_STR1)); assertEquals(MINGUO_STR, new DateCalendarConverter(PATTERN1, PATTERN, IsoChronology.INSTANCE, MinguoChronology.INSTANCE) .convert(ISO_STR1)); assertEquals(THAIBUDDHIST_STR1, new DateCalendarConverter(PATTERN, PATTERN1, IsoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE) .convert(ISO_STR)); assertEquals(THAIBUDDHIST_STR1, new DateCalendarConverter(PATTERN1, PATTERN1, IsoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE) .convert(ISO_STR1)); assertEquals(THAIBUDDHIST_STR, new DateCalendarConverter(PATTERN1, PATTERN, IsoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE) .convert(ISO_STR1)); }
@Test public void testConvert_withLocale() { final String convertedChineseDateFromEnglish = new DateCalendarConverter(PATTERN_ENGLISH_DATE, PATTERN_CHINESE_DATE, IsoChronology.INSTANCE, MinguoChronology.INSTANCE, Locale.US, Locale.CHINESE).convert(ENGLISH_DATE); assertTrue(EXPECT_CHINESE_DATE_JAVA8.equals(convertedChineseDateFromEnglish) || EXPECT_CHINESE_DATE.equals(convertedChineseDateFromEnglish)); assertEquals(ENGLISH_DATE, new DateCalendarConverter(PATTERN_CHINESE_DATE, PATTERN_ENGLISH_DATE, MinguoChronology.INSTANCE, IsoChronology.INSTANCE, Locale.CHINESE, Locale.US).convert(EXPECT_CHINESE_DATE)); final String convertedChineseDateFromFrench = new DateCalendarConverter(PATTERN_FRENCH_DATE, PATTERN_CHINESE_DATE, IsoChronology.INSTANCE, MinguoChronology.INSTANCE, Locale.FRANCE, Locale.CHINESE).convert(FRENCH_DATE); assertTrue(EXPECT_CHINESE_DATE_JAVA8.equals(convertedChineseDateFromFrench) || EXPECT_CHINESE_DATE.equals(convertedChineseDateFromFrench)); assertEquals(FRENCH_DATE, new DateCalendarConverter(PATTERN_CHINESE_DATE, PATTERN_FRENCH_DATE, MinguoChronology.INSTANCE, IsoChronology.INSTANCE, Locale.CHINESE, Locale.FRANCE).convert(EXPECT_CHINESE_DATE)); }
@Test public void testConvert_HijrahDateTo() { assertEquals(ISO_STR, new DateCalendarConverter(HijrahChronology.INSTANCE, IsoChronology.INSTANCE).convert(HIJRAH_STR)); assertEquals(JAPANESE_STR, new DateCalendarConverter(HijrahChronology.INSTANCE, JapaneseChronology.INSTANCE).convert(HIJRAH_STR)); assertEquals(MINGUO_STR, new DateCalendarConverter(HijrahChronology.INSTANCE, MinguoChronology.INSTANCE).convert(HIJRAH_STR)); assertEquals(THAIBUDDHIST_STR, new DateCalendarConverter(HijrahChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE) .convert(HIJRAH_STR)); assertEquals(JAPANESE_STR1, new DateCalendarConverter(PATTERN, PATTERN1, HijrahChronology.INSTANCE, JapaneseChronology.INSTANCE) .convert(HIJRAH_STR)); assertEquals(MINGUO_STR, new DateCalendarConverter(PATTERN6, PATTERN, HijrahChronology.INSTANCE, MinguoChronology.INSTANCE) .convert(HIJRAH_STR2)); final String convertedHijrahDate = new DateCalendarConverter(PATTERN, PATTERN5, HijrahChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE, Locale.US, Locale.US).convert(HIJRAH_STR); assertTrue( THAIBUDDHIST_STR5_JAVA8.equals(convertedHijrahDate) || THAIBUDDHIST_STR5.equals(convertedHijrahDate)); }
@Test public void testConvert_JapaneseDateTo() { assertEquals(ISO_STR, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA)); assertEquals(ISO_STR_1, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA_1)); if (isReiwaEraSupported()) { assertEquals(ISO_STR_2, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA_2)); assertEquals(StringUtils.EMPTY, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA_3)); } else { assertEquals(StringUtils.EMPTY, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA_4)); } assertEquals(HIJRAH_STR, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, HijrahChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA)); assertEquals(MINGUO_STR, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, MinguoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA)); assertEquals(THAIBUDDHIST_STR, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA)); final String convertedJapaneseDate = new DateCalendarConverter(PATTERN_WITH_G, PATTERN5, JapaneseChronology.INSTANCE, MinguoChronology.INSTANCE, Locale.JAPAN, Locale.US).convert(JAPANESE_DATE_WITH_ERA); assertTrue(MINGUO_STR5_JAVA8.equals(convertedJapaneseDate) || MINGUO_STR5.contentEquals(convertedJapaneseDate)); assertEquals(ISO_STR, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.US, Locale.US).convert("0008-10-29 Heisei")); Assert.assertFalse(ISO_STR.equals(new DateCalendarConverter(null, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.US, Locale.US).convert(JAPANESE_STR))); }
@Test public void testConvert_MinguoDateTo() { assertEquals(ISO_STR, new DateCalendarConverter(MinguoChronology.INSTANCE, IsoChronology.INSTANCE).convert(MINGUO_STR)); assertEquals(HIJRAH_STR, new DateCalendarConverter(MinguoChronology.INSTANCE, HijrahChronology.INSTANCE).convert(MINGUO_STR)); assertEquals(JAPANESE_STR, new DateCalendarConverter(MinguoChronology.INSTANCE, JapaneseChronology.INSTANCE).convert(MINGUO_STR)); assertEquals(THAIBUDDHIST_STR, new DateCalendarConverter(MinguoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE) .convert(MINGUO_STR)); assertEquals(JAPANESE_STR5, new DateCalendarConverter(PATTERN, PATTERN5, MinguoChronology.INSTANCE, JapaneseChronology.INSTANCE) .convert(MINGUO_STR)); }
@Test public void testConvert_ThaiBuddhistDateTo() { assertEquals(ISO_STR, new DateCalendarConverter(ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); assertEquals(HIJRAH_STR, new DateCalendarConverter(ThaiBuddhistChronology.INSTANCE, HijrahChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); assertEquals(JAPANESE_STR, new DateCalendarConverter(ThaiBuddhistChronology.INSTANCE, JapaneseChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); assertEquals(MINGUO_STR, new DateCalendarConverter(ThaiBuddhistChronology.INSTANCE, MinguoChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); assertEquals(ISO_STR5, new DateCalendarConverter(PATTERN, PATTERN5, ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); }
@Test public void testConvert_SpecialCases() { assertEquals("", new DateCalendarConverter(HijrahChronology.INSTANCE, JapaneseChronology.INSTANCE).convert("")); assertEquals(" ", new DateCalendarConverter(IsoChronology.INSTANCE, HijrahChronology.INSTANCE).convert(" ")); assertEquals(null, new DateCalendarConverter(HijrahChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE).convert(null)); assertEquals("", new DateCalendarConverter(HijrahChronology.INSTANCE, HijrahChronology.INSTANCE).convert("aa")); assertEquals("", new DateCalendarConverter( PATTERN1, PATTERN, HijrahChronology.INSTANCE, HijrahChronology.INSTANCE).convert("aa")); assertEquals(ISO_STR5, new DateCalendarConverter(null, PATTERN5, ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); assertEquals(ISO_STR, new DateCalendarConverter(PATTERN, null, ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); assertEquals(ISO_STR, new DateCalendarConverter(null, null, ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert(THAIBUDDHIST_STR)); }
@Test public void testConvert_MeasureTheExecutionTime() { Chronology[] chronologys = { IsoChronology.INSTANCE, HijrahChronology.INSTANCE, JapaneseChronology.INSTANCE, MinguoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE }; for (Chronology sourceChronology : chronologys) { for (Chronology targetChronology : chronologys) { DateCalendarConverter dateCalendarConverter = new DateCalendarConverter("dd-MM-yyyy", PATTERN, sourceChronology, targetChronology); InputStream dateStream = this.getClass().getResourceAsStream("dateList.txt"); BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(dateStream, "UTF-8")); } catch (UnsupportedEncodingException e) { LOGGER.error(e.getMessage(), e); Assert.fail(e.getMessage()); } try { long startTime = System.currentTimeMillis(); String line; while ((line = br.readLine()) != null) { dateCalendarConverter.convert(line); } long endTime = System.currentTimeMillis(); System.out.println("the execution time of " + sourceChronology.getId() + "-->" + targetChronology.getId() + " : " + (endTime - startTime) + "ms"); } catch (FileNotFoundException e) { LOGGER.error(e.getMessage(), e); Assert.fail(e.getMessage()); } catch (IOException e) { LOGGER.error(e.getMessage(), e); Assert.fail(e.getMessage()); } finally { try { br.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); Assert.fail(e.getMessage()); } } } } }
@Test public void testConvertDateWithResolverStyleStrict() { String ISODateValid = "2017-02-28"; String ISODateInValid = "2017-02-30"; assertEquals("", new DateCalendarConverter(IsoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE) .convert(ISODateInValid)); assertEquals("", new DateCalendarConverter(IsoChronology.INSTANCE, MinguoChronology.INSTANCE).convert("2017-04-32")); assertEquals("", new DateCalendarConverter("yyyy-MM-dd G", PATTERN, IsoChronology.INSTANCE, ThaiBuddhistChronology.INSTANCE).convert("2017-02-30 AD")); assertEquals(ISODateValid, new DateCalendarConverter(ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert("2560-02-28")); assertEquals("", new DateCalendarConverter(ThaiBuddhistChronology.INSTANCE, IsoChronology.INSTANCE) .convert("2560-02-30")); assertEquals(ISODateValid, new DateCalendarConverter(MinguoChronology.INSTANCE, IsoChronology.INSTANCE).convert("0106-02-28")); assertEquals("", new DateCalendarConverter(MinguoChronology.INSTANCE, IsoChronology.INSTANCE).convert("0106-02-30")); assertEquals("", new DateCalendarConverter(JapaneseChronology.INSTANCE, IsoChronology.INSTANCE).convert("0029-02-28")); assertEquals(ISODateValid, new DateCalendarConverter(PATTERN_WITH_G, null, JapaneseChronology.INSTANCE, IsoChronology.INSTANCE, Locale.US, Locale.US).convert("0029-02-28 Heisei")); }