DateFormat: abbreviation of the month with a period

I have a date format template: MMM yyyy

And I want: if the name of the month is abbreviated, a period is printed after the name. But if the month name is not short, the period is not added.

Example:

  • May 2010 should print: May 2010 (without a period) - May is only 3 letters long, so there is no need, because it is not an abbreviation.
  • December 2100 is due to print: Dec. 2010 Dec. 2010 (with a period) - December is longer than 3 letters, so a period is required because it is an abbreviation.

Is this possible with a template or do I need to implement it "by hand"?

+6
java formatting
source share
2 answers

What you can do is use DateFormatSymbols in your formatting, in which you redefine an array of short months with one that contains "May" instead of "May."

Update: Sorry, I got the last bit wrong, of course, it should be the other way around, the short months were initially β€œJan”, β€œFeb”, etc., and you should replace them with β€œJan.”, β€œFeb.” for every month except May.

+6
source share

I implemented a biziclop solution. - He works.

If someone is interested, here it is:

 import static org.junit.Assert.assertEquals; import java.text.DateFormat; import java.text.DateFormatSymbols; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import org.junit.Test; public class DateFormatTest { /** The locale where the tests are for. */ private static final Locale locale = Locale.ENGLISH; /** * Add a dot to all abbricated short months. * * @param dateFormatSymbols * @return */ private final DateFormatSymbols addDotToAbbricationMonths(final DateFormatSymbols dateFormatSymbols) { String[] shortMonths = dateFormatSymbols.getShortMonths(); for (int i = 0; i < shortMonths.length; i++) { if (dateFormatSymbols.getMonths()[i].length() > shortMonths[i].length()) { shortMonths[i] += '.'; } } dateFormatSymbols.setShortMonths(shortMonths); return dateFormatSymbols; } /** pattern under test. */ final DateFormat format = new SimpleDateFormat("MMM yyyy", addDotToAbbricationMonths(new DateFormatSymbols(locale))); /** Scenario: May is only 3 letters long, so there is no dot needed, because it is not an abbreviation. */ @Test public void testShortEnought() { Date firstMay = new GregorianCalendar(2010, Calendar.MAY, 1).getTime(); assertEquals("May 2010", format.format(firstMay)); } /** Scenario: December is more than 3 letters long, so there is a dot needed, because it an abbreviation. */ @Test public void testToLong() { Date firstDecember = new GregorianCalendar(2010, Calendar.DECEMBER, 1).getTime(); assertEquals("Dec. 2010", format.format(firstDecember)); } /** Scenario: if the DateFormatSymbols are changed for this special, it should not influence the other formatter. */ @Test public void noInfluence() { Date firstDecember = new GregorianCalendar(2010, Calendar.DECEMBER, 1).getTime(); assertEquals("Dec 2010", new SimpleDateFormat("MMM yyyy", locale).format(firstDecember)); } } 
+6
source share

All Articles