如何在Java 8中大写三个字母月份名称日期的第一个字母

问题描述 投票:1回答:3

我有以下字符串代表这样的日期“20190123”,我想将其转换为这种格式“25 Dic 2019”月份名称应该是西班牙语,第一个字母应该是大写的到目前为止我有这个

    String input = "12252013";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MMddyyyy" );
    LocalDate localDate = LocalDate.parse( input , formatter );
    Locale spanishLocale=new Locale("es", "ES");
    String dateInSpanish=localDate.format(DateTimeFormatter.ofPattern("dd MMM, yyyy",spanishLocale));
    System.out.println("'2016-01-01' in Spanish: "+dateInSpanish);

这打印"25 dic, 2013"我希望它像这个"25 Dic 2019"

java date date-format java-date
3个回答
2
投票

Feature, not a bug

西班牙语在西班牙的正确本地化使用小写作为月份名称。

你希望第一个字母大写是不正确的。如果您坚持这一结果,则需要对解决方案进行硬编码,而不是依赖于自动本地化功能。


2
投票

DateTimeFormatter根据区域设置规则使用月份名称和大小写。

在英语中,强制要求首字母大写,而西班牙语不是这样。对于西班牙语,必须将它作为RAE的小写字母

你必须使用类DateFormatSymbols来覆盖下面的月份名称,或者只需要通过编写一些代码将第一个空格后的dateInspanish第一个字母转换为最终字符串。

解决方案仅适用于Java7或更低版​​本。对于Java 8及更高版本,请查看@Ole V.V.的答案

        String input = "12252013";
        Locale spanishLocale=new Locale("es", "ES");

        DateFormatSymbols sym = DateFormatSymbols.getInstance(spanishLocale);
        sym.setMonths(new String[]{"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" });
        sym.setShortMonths(new String[]{"Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic" });

        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, spanishLocale);
        SimpleDateFormat sdf = (SimpleDateFormat) df;
        sdf.setDateFormatSymbols(sym);

        Date inputDate = new SimpleDateFormat("MMddyyyy").parse(input);

        String dateInSpanish = sdf.format(inputDate);
        System.out.println(dateInSpanish);

0
投票

这是气馁的答案!我想你比我更好地说和写西班牙语,但我的理解是西班牙语 - 就像世界上大多数语言一样 - 在月份名称中使用了一个小的第一个字母。当然你写的是“Diciembre es unmesfrío”(12月是一个寒冷的月份;来自谷歌翻译),但在“25 dic,2013”​​的中间你需要一个小d。

在任何情况下,如果您指示Java产生不正确的输出,Java当然可以产生不正确的输出。因此,如果您的用户坚持:

    Locale spanishLocale = new Locale("es", "ES");
    Map<Long, String> monthAbbreviations = Stream.of(Month.values())
            .collect(Collectors.toMap(m -> Long.valueOf(m.getValue()), 
                    m -> {
                        String abbrev = m.getDisplayName(TextStyle.SHORT, spanishLocale);
                        // Capitalize first letter against Spanish usage
                        return abbrev.substring(0, 1).toUpperCase(spanishLocale)
                                + abbrev.substring(1);
                    }));
    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .appendPattern("dd ")
            .appendText(ChronoField.MONTH_OF_YEAR, monthAbbreviations)
            .appendPattern(", yyyy")
            .toFormatter(spanishLocale);

    LocalDate localDate = LocalDate.of(2013, Month.DECEMBER, 25);
    String dateInSpanish=localDate.format(formatter);
    System.out.println("'2013-12-25' in Spanish with non-current capitalization: " + dateInSpanish);

“2013-12-25”,西班牙语,非流动资本化:25 Dic。,2013

如果您不希望点代表缩写,请修改代码以删除它。

链接:Months of the year in many different languages

© www.soinside.com 2019 - 2024. All rights reserved.