2011-01-18 34 views
6

Tôi có mẫu định dạng ngày: MMM yyyyDateFormat: viết tắt tháng có dấu chấm

Và muốn rằng: nếu tên tháng được cắt ngắn thì dấu chấm sẽ được in sau tên. Nhưng nếu tên tháng không bị cắt ngắn thì không có dấu chấm nào được thêm vào.

Ví dụ:

  • tháng năm 2010 nên in: May 2010 (không chấm) - May là chỉ có 3 lá thư dài, vì vậy không có dấu chấm cần thiết, bởi vì nó không phải là một từ viết tắt.
  • Tháng 12 năm 2100 sẽ in: Dec. 2010 (có dấu chấm) - Tháng 12 dài hơn 3 chữ cái, do đó, có dấu chấm cần thiết, vì chữ viết tắt.

Điều này có thể xảy ra với mẫu không hoặc tôi có cần triển khai bằng "tay" không?

Trả lời

6

Những gì bạn có thể làm là sử dụng tùy chỉnh DateFormatSymbols trong trình định dạng của bạn, trong đó bạn ghi đè mảng tháng ngắn bằng một tháng có chứa "Tháng Năm" thay vì "Tháng Năm".

Cập nhật: Xin lỗi, tôi có sai sót cuối cùng, dĩ nhiên nó phải là một cách khác, ngắn hạn ban đầu là "Jan", "Feb" v.v ... và bạn nên thay thế chúng bằng "Jan", "Tháng Hai" cho mỗi tháng trừ tháng Năm.

6

Tôi đã triển khai biziclop solution. - Nó hoạt động.

Nếu bất cứ ai đang interessted trong, ở đây nó là:

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)); 
    } 
} 
Các vấn đề liên quan