SimpleDateFormat和基于区域设置的格式字符串 [英] SimpleDateFormat and locale based format string

查看:101
本文介绍了SimpleDateFormat和基于区域设置的格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据给定的区域设置以不同的方式在Java格式化日期。例如,我希望英语用户能够看到2009年11月1日(格式为MMM d,yyyy)和挪威用户看到1. nov。2009(d。MMM。yyyy)。

I'm trying to format a date in Java in different ways based on the given locale. For instance I want English users to see "Nov 1, 2009" (formatted by "MMM d, yyyy") and Norwegian users to see "1. nov. 2009" ("d. MMM. yyyy").

如果我将区域设置添加到SimpleDateFormat构造函数中,月份部分可以正常工作,但是其余部分呢?

The month part works OK if I add the locale to the SimpleDateFormat constructor, but what about the rest?

我希望我可以添加格式字符串与语言环境配对SimpleDateFormat,但我找不到任何方法来做到这一点。是否可能或需要让我的代码检查区域设置并添加相应的格式字符串?

I was hoping I could add format strings paired with locales to SimpleDateFormat, but I can't find any way to do this. Is it possible or do I need to let my code check the locale and add the corresponding format string?

推荐答案

使用 DateFormat.getDateInstance( int style,Locale locale),而不是使用 SimpleDateFormat 创建自己的模式。

Use DateFormat.getDateInstance(int style, Locale locale) instead of creating your own patterns with SimpleDateFormat.

这篇关于SimpleDateFormat和基于区域设置的格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆