2011-01-18 16 views
6

que tiene un patrón de formato de fecha: MMM yyyyDateFormat: el mes abreviatura con el punto

y desea que: si el nombre del mes se corta recortado un punto se imprime después del nombre. Pero si el nombre del mes no es breve, no se agrega ningún punto.

Ejemplo:

  • de mayo de 2010 debe imprimir: May 2010 (sin punto) - mayo está a sólo 3 cartas largas, por lo que no habrá ningún punto es necesario, porque no es una abreviatura.
  • Diciembre 2100 debe imprimir: Dec. 2010 (con punto) - Diciembre tiene más de 3 letras, por lo que se necesita un punto, porque es una abreviatura.

¿Esto es posible con un patrón, o tengo que implementarlo "a mano"?

Respuesta

6

Lo que puede hacer es usar un DateFormatSymbols personalizado en su formateador, en el que anula el conjunto de meses cortos con uno que contenga "mayo" en lugar de "mayo".

Actualización: Lo siento, tengo el último inconveniente, por supuesto que debería ser al revés, los meses cortos son originalmente "Jan", "Feb", etc. y debe reemplazarlos por "Jan.", "Feb." para todos los meses a excepción de mayo.

6

He implementado biziclop solution. -- Funciona.

Si alguien está en interessted, aquí está:

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)); 
    } 
} 
Cuestiones relacionadas