简体   繁体   English

本地日期:带有语言环境的格式

[英]Localdate: format with locale

I'm attempting to format a LoacalDate but I didn't find any information.我正在尝试格式化 LoacalDate 但我没有找到任何信息。 I need to format to another language.我需要格式化为另一种语言。 The case is simply I want to get the month of the year in Spanish.案例只是我想用西班牙语获得一年中的月份。 I'm trying to use:我正在尝试使用:

Locale locale = new Locale("es", "ES");

But I don't find a SimpleDateFormat or similar to the format LocalDate.但我没有找到 SimpleDateFormat 或类似于 LocalDate 的格式。

LocalDate.now().getMonth();

Can anyone help me?谁能帮我?

Sorry, I used the older (and still more commonly used) date time classes of Java, as you spoke about SimpleDateFormat which is part of the older API.抱歉,当您谈到 SimpleDateFormat 时,我使用了 Java 的较旧(并且仍然更常用)的日期时间类,它是旧 API 的一部分。

When you are using java.time.LocalDate the formatter you have to use is java.time.format.DateTimeFormatter :当您使用java.time.LocalDate时,您必须使用的格式化程序是java.time.format.DateTimeFormatter

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM", aLocale);
final String month = LocalDate.now().format(formatter);

tl;dr tl;博士

Month
.from(
    LocalDate.now()
)
.getDisplayName( 
    TextStyle.FULL_STANDALONE , 
    new Locale( "es" , "ES" )
)

See this code run live at IdeOne.com .查看此代码在 IdeOne.com 上实时运行

julio胡里奥

Month class Month

The Answer by Wimmer is correct. Wimmer的答案是正确的。 But if all you want is the name of the month, use the Month class.但如果您只想要月份的名称,请使用Month类。 That may prove more direct and more flexible.这可能更直接、更灵活。

The getDisplayName method generates a localized String in various lengths (abbreviations). getDisplayName方法生成各种长度(缩写)的本地化字符串。

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) );
Month month = Month.from( today );
String monthName = month.getDisplayName( TextStyle.FULL_STANDALONE , locale );

“standalone” Month Name “独立”月份名称

Note that TextStyle offers alternative “standalone” versions of a month name.请注意, TextStyle提供月份名称的替代“独立”版本。 In some languages the name may be different when used as part of a date-time versus when used in general without a specific date-time.在某些语言中,名称在用作日期时间的一部分时与在没有特定日期时间的情况下一般使用时可能不同。

我使用了这个并与我一起工作

LocalDate.now().month.getDisplayName(TextStyle.FULL,local)

Try this 尝试这个

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormater {    
   public static void main(String[] args) {
            final Locale spain = new Locale("es", "ES");
            Format formatter = new SimpleDateFormat("MMMM",spain); 
            String s = formatter.format(new Date());
            System.out.println(s);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM