如何从 DateTimeFormatter 对象获取日期时间模式

How to get the date-time pattern from a DateTimeFormatter object

通过传递区域设置在 Joda 中获取短日期时间模式的常用方法如下所示:

String pattern = DateTimeFormat.patternForStyle("SS", formatLocale);

但是仍然有可能 从 DateTimeformatter 获取模式(例如:yy-dd-mm H:mm)?

DateTimeFormatter format;

一旦设置了所有详细信息(语言环境、区域、国家/地区),我可以 get/retrieve 从该格式对象进行模式化吗?

如果您正在寻找一种通用方法来检索任何任意构造的格式化程序的格式模式,那么答案是:不,不可能。

原因是通过构建器模式构造的格式化程序允许任何模式无法涵盖的格式细节。示例:

对于永远不会被任何模式符号覆盖的构建器方法appendYearOfCentury(...) there is no pattern equivalent. Or the formatter exploits the option to include a custom parser。类似的考虑也适用于其他时间库,包括 Java-8。