I'm seeing inconsistency between similar looking inputs when using the same DateTimeFormatter. I need this to compare with another input source of date times.
For example, given these inputs:
2020-06-29T01:00:00+01:00 -> my date time formatter -> 2020-06-29T01:00:00+01:00
2021-08-18T21:00:00+0000 -> my date time formatter -> 2021-08-18T21:00:00Z
I want the output to always be in YYYY-mm-ddT00:00:00Z format, so the second input yields my desired outcome.
The way I'm currently formatting it:
DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4)
.optionalStart()
.appendLiteral("-")
.optionalEnd()
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.optionalStart()
.appendLiteral("-")
.optionalEnd()
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('T')
.appendPattern("HH':'mm':'ss[XXX][X]")
.toFormatter();
public OffsetDateTime convert(String dateTime) {
return OffsetDateTime.parse(dateTime, FORMATTER);
}
public String getDateTimeString(OffsetDateTime offsetDateTime) {
return offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
public void doStuff(String input) {
OffsetDateTime odt = convert(input);
String finalResult = getDateTimeString(odt);
}
..
doStuff(input);
I've been looking through this for a few hours now, but the documentation to me isn't that clear and I've been having all sorts of trouble. Any tips?