DateFormat:带点的月份缩写

发布于 2024-10-13 04:55:14 字数 324 浏览 6 评论 0原文

我有一个日期格式模式: MMM yyyy

并希望:如果月份名称被缩短,则在名称后面打印一个点。但如果月份名称不是缩写的,则不会添加点。

示例:

  • May 2010 应打印:May 2010(不带点)——May 只有 3 个字母长,因此不需要点,因为它不是缩写。
  • December 2100 应打印:Dec. 2010(带点)——December 超过 3 个字母长,因此需要一个点,因为它是缩写。

这可以通过模式实现吗?还是我需要“手动”实现它?

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-10-20 04:55:14

您可以做的是使用自定义 DateFormatSymbols 在格式化程序中,您可以在其中使用包含“May”而不是“May.”的数组覆盖短月份数组。

更新:抱歉,我最后一点错了,当然应该反过来,短月份最初是“Jan”,“Feb”等,你应该将它们替换为“Jan”。 ,“二月。”除五月外的每个月。

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.

倥絔 2024-10-20 04:55:14

我已经实现了 biziclop 解决方案。 - 有用。

如果有人感兴趣,这里是:

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

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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文