Java中的本地化日期格式 [英] Localized date format in Java
问题描述
我有一个毫秒的时间戳,想要格式化它,表示日期,月份,年份和小时的分钟。
I have a timestamp in millis and want to format it indicating day, month, year and the hour with minutes precission.
我知道我可以指定格式这个:
I know I can specify the format like this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy HH:mm");
String formatted = simpleDateFormat.format(900000)
但我希望格式为使用用户的语言环境进行本地化。我也试过了
But I'd like the format to be localized with the user's locale. I've also tried
DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
DATE_FORMAT.format(new Date());
但它没有显示小时。我该怎么做?
But it does not show the hour. How can I do it?
推荐答案
正在使用joda时间( http://joda-time.sourceforge.net/ )无从谈起?如果没有,那么我会全心全意地推荐使用这个精彩的库而不是繁琐的Java API。
Is using joda time (http://joda-time.sourceforge.net/) out of the question? If not, then I would wholeheartedly recommend using this wonderful library instead of the cumbersome Java API.
如果没有,你可以使用 DateFormat.getDateTimeInstance( int,int,Locale)
If not, you could use DateFormat.getDateTimeInstance(int, int, Locale)
第一个int是小时的样式,另一个是时间样式,所以尝试使用:
The first int is the style for hour, the other is the style for time, so try using:
DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
String formattedDate = f.format(new Date());
System.out.println("Date: " + formattedDate);
看看这是否适合你。
Locale.GERMANY的输出:
日期:25.07.13 10:57
Output for Locale.GERMANY: Date: 25.07.13 10:57
Locale.US的输出:
日期:7/25/13上午10:57
Output for Locale.US: Date: 7/25/13 10:57 AM
这篇关于Java中的本地化日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!