DateTimeFormatter接受多个日期并转换为一个(java.time库) [英] DateTimeFormatter Accepting Multiple Dates and Converting to One (java.time library)
问题描述
我正在尝试写一个 DateTimeFormatter
,它允许我接受多种不同的 String
格式,然后将 String
格式转换为特定类型。由于项目的范围和已经存在的代码,我不能使用不同类型的格式化程序。
I am trying to write a DateTimeFormatter
that will allow me to take in multiple different String
formats, and then convert the String
formats to a specific type. Due to the scope of the project and the code that already exists, I cannot use a different type of formatter.
例如,我想接受 MM / dd / yyyy
以及 yyyy-MM-dd'T'HH:mm:ss
但是当我打印时我只想要打印到 MM / dd / yyyy
格式并在我调用 LocalDate.format(formatter)时以格式显示;
E.g., I want to accept MM/dd/yyyy
as well as yyyy-MM-dd'T'HH:mm:ss
but then when I print I only want to print to MM/dd/yyyy
format and have it in the format when I call LocalDate.format(formatter);
有人可以建议如何使用 java.time.format。*;
Could someone suggest ideas on how to do this with the java.time.format.*;
我可以在 org.joda
中执行此操作:
Here is how I could do it in org.joda
:
// MM/dd/yyyy format
DateTimeFormatter monthDayYear = DateTimeFormat.forPattern("MM/dd/yyyy");
// array of parsers, with all possible input patterns
DateTimeParser[] parsers = {
// parser for MM/dd/yyyy format
monthDayYear.getParser(),
// parser for yyyy-MM-dd'T'HH:mm:ss format
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").getParser()
};
DateTimeFormatter parser = new DateTimeFormatterBuilder()
// use the monthDayYear formatter for output (monthDayYear.getPrinter())
// and parsers array for input (parsers)
.append(monthDayYear.getPrinter(), parsers)
// create formatter (using UTC to avoid DST problems)
.toFormatter()
.withZone(DateTimeZone.UTC);
我还没有在网上找到一个好的/有效的例子。
I have not found a good/working example of this online.
推荐答案
我用 JDK 1.8.0_131 for Mac OS X 和 JDK 1.8.0111 for Windows (两者都有效)。
I've tested this with JDK 1.8.0_131 for Mac OS X and JDK 1.8.0111 for Windows (both worked).
我创建了一个带有可选部分的 DateTimeFormatter
(由<$ c分隔) $ c> [] ),解析两种情况( MM / dd / yyyy
和 yyyy-MM-dd' T'HH:mm:ss
)。
I've created a DateTimeFormatter
with optional sections (delimited by []
), to parse both cases (MM/dd/yyyy
and yyyy-MM-dd'T'HH:mm:ss
).
同样的格式化程序适用于您的情况( LocalDate
),但下面有一些注意事项。
The same formatter worked for your case (LocalDate
), but there are some considerations below.
// parse both formats (use optional section, delimited by [])
DateTimeFormatter parser = DateTimeFormatter.ofPattern("[MM/dd/yyyy][yyyy-MM-dd'T'HH:mm:ss]");
// parse MM/dd/yyyy
LocalDate d1 = LocalDate.parse("10/16/2016", parser);
// parse yyyy-MM-dd'T'HH:mm:ss
LocalDate d2 = LocalDate.parse("2016-10-16T10:20:30", parser);
// parser.format(d1) is the same as d1.format(parser)
System.out.println(parser.format(d1));
System.out.println(parser.format(d2));
输出为:
10/16/2016
10/16/2016
10/16/2016
10/16/2016
PS:这仅适用于 LocalDate
。如果我尝试使用时间字段格式化对象(例如 LocalDateTime
),则使用两种格式:
PS: this works only with LocalDate
. If I try to format an object with time fields (like LocalDateTime
), both formats are used:
System.out.println(parser.format(LocalDateTime.now()));
这打印:
06/18 / 20172017-06-18T07:40:55
06/18/20172017-06-18T07:40:55
请注意,它使用两种模式进行格式化。我的猜测是格式化程序检查对象是否包含每个可选节中的字段。由于 LocalDate
没有时间字段(小时/分钟/秒),第二个模式失败,它只打印第一个( MM / dd / YYYY
)。但是 LocalDateTime
包含所有时间字段,并且两种模式都有效,因此两者都用于格式化。
Note that it formatted with both patterns. My guess is that the formatter checks if the object has the fields in each optional section. As the LocalDate
has no time fields (hour/minute/second), the second pattern fails and it prints only the first one (MM/dd/yyyy
). But the LocalDateTime
has all the time fields, and both patterns are valid, so both are used to format.
我的结论是:这不是一般解决方案(如Joda-Time的版本),它更像是一个幸运的案例,其中涉及的模式创造了所需的情况。但我不会依赖于所有情况。
My conclusion is: this isn't a general solution (like the Joda-Time's version), it's more like a "lucky" case where the patterns involved created the desired situation. But I wouldn't rely on that for all cases.
无论如何,如果你只使用 LocalDate
,你可以尝试使用此代码。但是如果你正在使用其他类型,那么你可能不得不使用另一个格式化程序来输出,如下所示:
Anyway, if you are only using LocalDate
, you can try to use this code. But if you're working with another types, then you'll probably have to use another formatter for the output, like this:
// parser/formatter for month/day/year
DateTimeFormatter mdy = DateTimeFormatter.ofPattern("MM/dd/yyyy");
// parser for both patterns
DateTimeFormatter parser = new DateTimeFormatterBuilder()
// optional MM/dd/yyyy
.appendOptional(mdy)
// optional yyyy-MM-dd'T'HH:mm:ss (use built-in formatter)
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// create formatter
.toFormatter();
// parse MM/dd/yyyy
LocalDate d1 = LocalDate.parse("10/16/2016", parser);
// parse yyyy-MM-dd'T'HH:mm:ss
LocalDate d2 = LocalDate.parse("2016-10-16T10:20:30", parser);
// use mdy to format
System.out.println(mdy.format(d1));
System.out.println(mdy.format(d2));
// format object with time fields: using mdy formatter to avoid multiple pattern problem
System.out.println(mdy.format(LocalDateTime.now()));
输出为:
10/16/2016
10/16/2016
06/18/2017
10/16/2016
10/16/2016
06/18/2017
这篇关于DateTimeFormatter接受多个日期并转换为一个(java.time库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!