fis*_*ben 74 java locale date simpledateformat
我正在尝试基于给定的语言环境以不同的方式在Java中格式化日期.例如,我希望英语用户看到"2009年11月1日"(由"MMM d,yyyy"格式化)和挪威用户看到"1. nov.2009"("d.MMM.yyyy").
如果我将语言环境添加到SimpleDateFormat构造函数中,那么月份部分可以正常工作,但其余部分呢?
我希望我可以将与locales配对的格式字符串添加到SimpleDateFormat,但我找不到任何方法来执行此操作.是否可以或者我是否需要让我的代码检查语言环境并添加相应的格式字符串?
red*_*hka 83
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE dd MMM yyyy", Locale.ENGLISH);
String formatted = dateFormat.format(the_date_you_want_here);
Run Code Online (Sandbox Code Playgroud)
jar*_*bjo 78
使用DateFormat.getDateInstance(int style,Locale locale)而不是创建自己的模式SimpleDateFormat.
Jua*_*nZe 29
使用style + locale:DateFormat.getDateInstance(int style,Locale locale)
查看 http://java.sun.com/j2se/1.5.0/docs/api/java/text/DateFormat.html
运行以下示例以查看差异:
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormatDemoSO {
public static void main(String args[]) {
int style = DateFormat.MEDIUM;
//Also try with style = DateFormat.FULL and DateFormat.SHORT
Date date = new Date();
DateFormat df;
df = DateFormat.getDateInstance(style, Locale.UK);
System.out.println("United Kingdom: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.US);
System.out.println("USA: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.FRANCE);
System.out.println("France: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.ITALY);
System.out.println("Italy: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.JAPAN);
System.out.println("Japan: " + df.format(date));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
United Kingdom: 25-Sep-2017
USA: Sep 25, 2017
France: 25 sept. 2017
Italy: 25-set-2017
Japan: 2017/09/25
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 13
LocalDate.now().format(
DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM )
.withLocale( new Locale( "no" , "NO" ) )
)
Run Code Online (Sandbox Code Playgroud)
麻烦的类java.util.Date和SimpleDateFormat现在是遗留的,取而代之的是java.time类.
LocalDate该LocalDate级表示没有时间一天和不同时区的日期,唯一的价值.
时区对于确定日期至关重要.对于任何给定的时刻,日期在全球范围内因地区而异.例如,法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是"昨天" .
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Run Code Online (Sandbox Code Playgroud)
DateTimeFormatter使用DateTimeFormatter来生成表示只有日期部分或时间部分的字符串.
该DateTimeFormatter类别可自动定位.
要进行本地化,请指定:
FormatStyle 确定字符串的长度或缩写.Locale 确定(a)翻译日期名称,月份名称等的人类语言,以及(b)决定缩写,大小写,标点符号等问题的文化规范.例:
Locale l = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( l );
String output = ld.format( f );
Run Code Online (Sandbox Code Playgroud)
走向另一个方向,您可以解析本地化的字符串.
LocalDate ld = LocalDate.parse( input , f );
Run Code Online (Sandbox Code Playgroud)
请注意,区域设置和时区是完全正交的问题.您可以用日语或以印地语提供的奥克兰新西兰时刻呈现蒙特利尔时刻.
另一个例子:将6 junio 2012(西班牙语)改为2012-06-06(标准ISO 8601格式).默认情况下,java.time类使用ISO 8601格式来解析/生成字符串.
String input = "6 junio 2012";
Locale l = new Locale ( "es" , "ES" );
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "d MMMM uuuu" , l );
LocalDate ld = LocalDate.parse ( input , f );
String output = ld.toString(); // 2012-06-06.
Run Code Online (Sandbox Code Playgroud)
下面是一些示例代码,用于在多个语言环境中仔细阅读多种格式的结果,并自动进行本地化.
An EnumSet是一种实现Set,在收集Enum对象时针对低内存使用和快速执行速度进行了高度优化.所以,EnumSet.allOf( FormatStyle.class )给我们一个FormatStyle循环所有四个枚举对象的集合.有关更多信息,请参阅枚举类型的Oracle教程.
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 );
List < Locale > locales = new ArrayList <>( 3 );
locales.add( Locale.CANADA_FRENCH );
locales.add( new Locale( "no" , "NO" ) );
locales.add( Locale.US );
// Or use all locales (almost 800 of them, for about 120K text results).
// Locale[] locales = Locale.getAvailableLocales(); // All known locales. Almost 800 of them.
for ( Locale locale : locales )
{
System.out.println( "------| LOCALE: " + locale + " — " + locale.getDisplayName() + " |----------------------------------" + System.lineSeparator() );
for ( FormatStyle style : EnumSet.allOf( FormatStyle.class ) )
{
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( style ).withLocale( locale );
String output = ld.format( f );
System.out.println( output );
}
System.out.println( "" );
}
System.out.println( "« fin »" + System.lineSeparator() );
Run Code Online (Sandbox Code Playgroud)
输出.
------| LOCALE: fr_CA — French (Canada) |----------------------------------
mardi 23 janvier 2018
23 janvier 2018
23 janv. 2018
18-01-23
------| LOCALE: no_NO — Norwegian (Norway) |----------------------------------
tirsdag 23. januar 2018
23. januar 2018
23. jan. 2018
23.01.2018
------| LOCALE: en_US — English (United States) |----------------------------------
Tuesday, January 23, 2018
January 23, 2018
Jan 23, 2018
1/23/18
« fin »
Run Code Online (Sandbox Code Playgroud)
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
您可以直接与数据库交换java.time对象.使用符合JDBC 4.2或更高版本的JDBC驱动程序.不需要字符串,不需要课程.java.sql.*
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
小智 8
日期字符串的本地化:
基于redsonic的帖子:
private String localizeDate(String inputdate, Locale locale) {
Date date = new Date();
SimpleDateFormat dateFormatCN = new SimpleDateFormat("dd-MMM-yyyy", locale);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
date = dateFormat.parse(inputdate);
} catch (ParseException e) {
log.warn("Input date was not correct. Can not localize it.");
return inputdate;
}
return dateFormatCN.format(date);
}
String localizedDate = localizeDate("05-Sep-2013", new Locale("zh","CN"));
Run Code Online (Sandbox Code Playgroud)
将像05-九月-2013
这将根据用户的当前语言环境显示日期:
返回日期和时间:
import java.text.DateFormat;
import java.util.Date;
Date date = new Date();
DateFormat df = DateFormat.getDateTimeInstance();
String myDate = df.format(date);
Run Code Online (Sandbox Code Playgroud)
1969 年 12 月 31 日下午 7:00:02
要仅返回日期,请使用:
DateFormat.getDateInstance()
Run Code Online (Sandbox Code Playgroud)
1969 年 12 月 31 日