Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateFormat: month abbreviation with dot

I have a date format pattern: MMM yyyy

And want that: if the month name is short cuted a dot is printed after the name. But if the month name is not short cuted no dot is added.

Example:

  • May 2010 should print: May 2010 (without dot) -- May is only 3 letters long, so there is no dot needed, because it is not an abbreviation.
  • December 2100 should print: Dec. 2010 (with dot) -- December is more than 3 letters long, so there is a dot needed, because it an abbreviation.

Is this possible with an pattern, or do I need to implement it by "hand"?

like image 581
Ralph Avatar asked Jan 18 '11 11:01

Ralph


People also ask

Is there a dot after month?

with months, you can either choose to put a dot (Nov.) or not (Nov). both forms are correct. with days, when you use the shortened forms, you don't need to put dots (Sun, Mon).

What is MMM in date format?

The MMM format for months is the short name i.e. Jan, Feb, Mar, Apr, etc.

What is DD in date?

dd – two-digit day of the month, e.g. 02. ddd – three-letter abbreviation for day of the week, e.g. Fri. dddd – day of the week spelled out in full, e.g. Friday.


2 Answers

What you can do is use a custom DateFormatSymbols in your formatter, in which you override the short months array 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, short months are originally "Jan", "Feb" etc and you should replace them with "Jan.", "Feb." for every month except for May.

like image 152
biziclop Avatar answered Oct 26 '22 14:10

biziclop


I have implemented biziclop solution. -- It works.

If anyone is interessted in, 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));
    }
}
like image 29
Ralph Avatar answered Oct 26 '22 16:10

Ralph