java 如何控制DateFormat返回的月份和日期名称的大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/66750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:55:36  来源:igfitidea点击:

How to control the capitalization of month and day names returned by DateFormat?

javadateinternationalization

提问by user9661

Here is a quick test program:

这是一个快速测试程序:

    public static void main( String[] args )
{
    Date date = Calendar.getInstance().getTime();

    System.out.println("Months:");
    printDate( "MMMM", "en", date );
    printDate( "MMMM", "es", date );
    printDate( "MMMM", "fr", date );
    printDate( "MMMM", "de", date );

    System.out.println("Days:");
    printDate( "EEEE", "en", date );
    printDate( "EEEE", "es", date );
    printDate( "EEEE", "fr", date );
    printDate( "EEEE", "de", date );

}

public static void printDate( String format, String locale, Date date )
{
    System.out.println( locale + ": " + (new SimpleDateFormat( format, new Locale( locale ) )).format( date ) );
}

The output is:

输出是:

Months: en: September es: septiembre fr: septembre de: September Days: en: Monday es: lunes fr: lundi de: Montag

Months: en: September es: septiembre fr: septembre de: September Days: en: Monday es: lunes fr: lundi de: Montag

How can I control the capitalization of the names. For some reason the Spanish and French always seem to return names that start with a lowercase letter.

如何控制名称的大小写。出于某种原因,西班牙语和法语似乎总是返回以小写字母开头的名称。

回答by brabster

Not all languages share english capitalization rules. I guess you'd need to alter the data used by the API, but your non-english clients might not appreciate it...

并非所有语言都共享英文大小写规则。我猜您需要更改 API 使用的数据,但您的非英语客户可能不喜欢它...

about.com on french capitalization

about.com 法语大写

回答by Dan Dyer

Capitalisation rules are different for different languages. In French, month names should not be capitalised.

不同语言的大小写规则不同。在法语中,月份名称不应大写

回答by Chris Shaffer

You may not want to change the capitalization -- different cultures capitalize different words (for example, in German you capitalize every noun, not just proper nouns).

您可能不想更改大小写——不同的文化将不同的单词大写(例如,在德语中,您将每个名词都大写,而不仅仅是专有名词)。

回答by Basil Bourque

tl;dr

tl;博士

How can I control the capitalization of the names

如何控制名称的大小写

You don't. Different languages and different cultures have different rules about capitalization, punctuation, abbreviation, etc.

你没有。不同的语言和不同的文化对大小写、标点符号、缩写等有不同的规定。

getDisplayName— automatically localize

getDisplayName— 自动本地化

Month.from( LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) )    // Get current month.
     .getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH )     // Localize automatically. Specify `Locale` to determine human language and cultural norms for translation. 

février

费弗里耶

LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )                  // Get current date as seen by people in a certain region (time zone).
    .getDayOfWeek()                                               // Get the day-of-week as a pre-defined `DayOfWeek` enum object.
    .getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH )

lundi

伦迪

Localize

本地化

As others stated, you should not be forcing your own parochial (US English?) notions of capitalization. Use a decent date-time library, and let it automatically localize for you.

正如其他人所说,您不应该强迫自己狭隘的(美国英语?)大写概念。使用合适的日期时间库,让它自动为您本地化。

java.time

时间

You are using terrible old date-time classes that are now legacy, supplanted by the java.time classes.

您正在使用可怕的旧日期时间类,这些类现在是遗留的,被 java.time 类取代。

The LocalDateclass represents a date-only value without time-of-day and without time zone.

LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris Franceis a new day while still “yesterday” in Montréal Québec.

时区对于确定日期至关重要。对于任何给定时刻,日期因地区而异。例如,在法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天” 。

Specify a proper time zone namein the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as ESTor ISTas they are nottrue time zones, not standardized, and not even unique(!).

以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/regionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST

ZoneId z = ZoneId.of( "Asia/Kolkata" );
LocalDate today = LocalDate.now( z );

To work with a month, extract a Monthenum object. Ditto for day-of-week, DayOfWeek.

要处理一个月,请提取一个Month枚举对象。星期几同上DayOfWeek

Call getDisplayName. Pass a TextStylefor abbreviation. Pass a Localeto determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such. Note that Localehas nothingto do with time zone.

打电话getDisplayName。传递 aTextStyle为缩写。通过 aLocale来确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。请注意,Locale没有做的时区。

Month m = today.getMonth() ;
String mNameQuébec = m.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; 
String mNameGermany = m.getDisplayName( TextStyle.FULL , Locale.GERMANY ) ; 

…and…

…和…

DayOfWeek dow = today.getDayOfWeek() ;
String dowNameQuébec = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; 
String dowNameGermany = dow.getDisplayName( TextStyle.FULL , Locale.GERMANY ) ; 

About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

With a JDBC drivercomplying with JDBC 4.2or later, you may exchange java.timeobjects directly with your database. No need for strings or java.sql.* classes.

使用符合JDBC 4.2或更高版本的JDBC 驱动程序,您可以直接与您的数据库交换java.time对象。不需要字符串或 java.sql.* 类。

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答by rajat banerjee

I'm having a problem now where a sentence begins with "dimanche 07 mars", which wouldn't matter if it were not at the beginning of a sentence. I guess this cannot be changed, unless I do manual string manipulation on the first character of the string.

我现在遇到了一个问题,一个句子以“dimanche 07 mars”开头,如果它不在句子的开头就没有关系。我想这不能改变,除非我对字符串的第一个字符进行手动字符串操作。