如何获取本地化日期模式字符串? [英] How do I get localized date pattern string?

查看:128
本文介绍了如何获取本地化日期模式字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DateFormat ,即我可以将当​​前日期格式化为短本地化日期,如下所示:

It is quite easy to format and parse Java Date (or Calendar) classes using instance of DateFormat, i.e. I could format current date into short localize date like this:

DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
String today = formatter.format(new Date());

我的问题是:我需要获取这个本地化的模式字符串(例如MM / dd / yy)。这应该是一个微不足道的任务,但是我找不到提供者...

My problem is: I need to obtain this localized pattern string (i.e. something like "MM/dd/yy"). This should be a trivial task, but I just couldn't find the provider...

推荐答案

对于 SimpleDateFormat ,您调用 toLocalizedPattern()

编辑:

对于Java 8用户:

For Java 8 users:

Java 8 Date Time API类似于Joda-time。要获得本地化模式,我们可以使用类
DateTimeFormatter

The Java 8 Date Time API is similar to Joda-time. To gain a localized pattern we can use class DateTimeFormatter

DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

请注意,当您在LocalDate上调用toString()时,您将获得格式为ISO-8601的日期

Note that when you call toString() on LocalDate, you will get date in format ISO-8601

请注意,Date Time API Java 8的灵感来自于Joda Time,大多数解决方案可以基于与时间有关的问题。

Note that Date Time API in Java 8 is inspired by Joda Time and most solution can be based on questions related to time.

这篇关于如何获取本地化日期模式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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