SimpleDateFormat 和基于语言环境的格式字符串 [英] SimpleDateFormat and locale based format string

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

问题描述

我正在尝试根据给定的语言环境以不同的方式在 Java 中格式化日期.例如,我希望英语用户看到Nov 1, 2009"(格式为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天全站免登陆