/** * Gets the simple date format for the current user's locale. The format will be similar in size * to mm/dd/yyyy * * @return SimpleDateFormat for the user's current locale * @see org.openmrs.util.OpenmrsUtil#getDateFormat(Locale) * @should return a pattern with four y characters in it */ public static SimpleDateFormat getDateFormat() { return OpenmrsUtil.getDateFormat(getLocale()); }
/** * Get the current user's datetime format Will look similar to "mm-dd-yyyy hh:mm a". Depends on * user's locale. * * @return a simple date format * @should return a pattern with four y characters and two h characters in it * @should not allow the returned SimpleDateFormat to be modified * @since 1.9 */ public static SimpleDateFormat getDateTimeFormat(Locale locale) { SimpleDateFormat dateFormat; SimpleDateFormat timeFormat; dateFormat = getDateFormat(locale); timeFormat = getTimeFormat(locale); String pattern = dateFormat.toPattern() + " " + timeFormat.toPattern(); SimpleDateFormat sdf = new SimpleDateFormat(); sdf.applyPattern(pattern); return sdf; }
@Test public void openmrsDateFormat_shouldParseValidDate() throws ParseException { SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en", "GB")); sdf.parse("20/12/2001"); sdf = OpenmrsUtil.getDateFormat(new Locale("en", "US")); sdf.parse("12/20/2001"); }
@Test public void openmrsDateFormat_shouldAllowSingleDigitDatesAndMonths() throws ParseException { SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en")); sdf.parse("1/1/2001"); }
@Test public void openmrsDateFormat_shouldNotAllowTwoDigitYears() { try { SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en")); sdf.parse("01/01/01"); Assert.fail("Date with two-digit year should throw exception."); } catch (ParseException e) {} }
/** * @see OpenmrsUtil#getDateFormat(Locale) */ @Test public void getDateFormat_shouldNotAllowTheReturnedSimpleDateFormatToBeModified() { // start with a locale that is not currently cached by getDateFormat() Locale locale = new Locale("hk"); Assert.assertTrue("default locale is potentially already cached", !Context.getLocale().equals(locale)); // get the initially built dateformat from getDateFormat() SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(locale); Assert.assertNotSame("initial dateFormatCache entry is modifiable", OpenmrsUtil.getDateFormat(locale), sdf); // verify changing the pattern on our variable does not affect the cache sdf.applyPattern("yyyymmdd"); Assert.assertTrue("initial dateFormatCache pattern is modifiable", !OpenmrsUtil.getDateFormat(locale).toPattern() .equals(sdf.toPattern())); // the dateformat cache now contains the format for this locale; checking // a second time will guarantee we are looking at cached data and not the // initially built dateformat sdf = OpenmrsUtil.getDateFormat(locale); Assert.assertNotSame("cached dateFormatCache entry is modifiable", OpenmrsUtil.getDateFormat(locale), sdf); // verify changing the pattern on our variable does not affect the cache sdf.applyPattern("yyyymmdd"); Assert.assertTrue("cached dateFormatCache pattern is modifiable", !OpenmrsUtil.getDateFormat(locale).toPattern() .equals(sdf.toPattern())); }
/** * @see OpenmrsUtil#getDateFormat(Locale) */ @Test public void getDateFormat_shouldReturnAPatternWithFourYCharactersInIt() { Assert.assertEquals("MM/dd/yyyy", OpenmrsUtil.getDateFormat(Locale.US).toLocalizedPattern()); Assert.assertEquals("dd/MM/yyyy", OpenmrsUtil.getDateFormat(Locale.UK).toLocalizedPattern()); Assert.assertEquals("tt.MM.uuuu", OpenmrsUtil.getDateFormat(Locale.GERMAN).toLocalizedPattern()); Assert.assertEquals("dd-MM-yyyy", OpenmrsUtil.getDateFormat(new Locale("pt", "pt")).toLocalizedPattern()); }
SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en", "GB")); sdf.parse("1/13/2001"); Assert.fail("Date with invalid month should throw exception."); SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en", "GB")); sdf.parse("32/1/2001"); Assert.fail("Date with invalid day should throw exception."); SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en", "US")); sdf.parse("13/1/2001"); Assert.fail("Date with invalid month should throw exception."); SimpleDateFormat sdf = OpenmrsUtil.getDateFormat(new Locale("en", "US")); sdf.parse("1/32/2001"); Assert.fail("Date with invalid day should throw exception.");