It should be

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

//or

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");

instead of

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");

From JAVADoc:

Offset X and x: This formats the offset based on the number of pattern letters. One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'. Two letters outputs the hour and minute, without a colon, such as '+0130'. Three letters outputs the hour and minute, with a colon, such as '+01:30'. Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. Six or more letters throws IllegalArgumentException. Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.

Answer from ninja.coder on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › format › DateTimeFormatter.html
DateTimeFormatter (Java Platform SE 8 )
October 20, 2025 - Java™ Platform Standard Ed. 8 ... Formatter for printing and parsing date-time objects. This class provides the main application entry point for printing and parsing and provides common implementations of DateTimeFormatter:
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › time › format › DateTimeFormatter.html
DateTimeFormatter (Java SE 22 & JDK 22)
July 16, 2024 - As this formatter has an optional element, it may be necessary to parse using parseBest(java.lang.CharSequence, java.time.temporal.TemporalQuery<?>...). The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style. public static final DateTimeFormatter RFC_1123_DATE_TIME
🌐
Baeldung
baeldung.com › home › java › java dates › guide to datetimeformatter
Guide to DateTimeFormatter | Baeldung
March 26, 2025 - Learn how to use the Java 8 DateTimeFormatter class to format and parse dates and times
🌐
W3Schools
w3schools.com › java › java_date.asp
Java Date and Time
import java.time.LocalDateTime; // Import the LocalDateTime class import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class public class Main { public static void main(String[] args) { LocalDateTime myDateObj = LocalDateTime.now(); System.out.println("Before formatting: " + myDateObj); DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formattedDate = myDateObj.format(myFormatObj); System.out.println("After formatting: " + formattedDate); } } The output will be: Try it Yourself » ·
🌐
Medium
medium.com › @medcherrou › a-comprehensive-guide-to-date-and-time-formatting-in-java-with-datetimeformatter-fefb8e48ce9f
A Comprehensive Guide to Date and Time Formatting in Java with DateTimeFormatter | by Med Cherrou | Medium
December 13, 2024 - DateTimeFormatter is a class in the java.time.format package that helps in parsing and formatting date-time objects (LocalDate, LocalTime, LocalDateTime, ZonedDateTime, etc.).
🌐
Android Developers
developer.android.com › api reference › datetimeformatter
DateTimeFormatter | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java date time › java datetimeformatter (with examples)
Java DateTimeFormatter (with Examples)
June 11, 2024 - Java DateTimeFormatter helps in uniformly parsing and printing the date-time objects in various inbuilt and custom formatting patterns.
🌐
Oracle
docs.oracle.com › en › java › javase › 25 › docs › api › java.base › java › time › format › DateTimeFormatter.html
DateTimeFormatter (Java SE 25 & JDK 25)
January 20, 2026 - The toFormat() method returns an implementation of java.text.Format. Patterns are based on a simple sequence of letters and symbols. A pattern is used to create a Formatter using the ofPattern(String) and ofPattern(String, Locale) methods. For example, "d MMM uuuu" will format 2011-12-03 as '3 Dec 2011'. A formatter created from a pattern can be used as many times as necessary, it is immutable and is thread-safe. ... LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd"); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter);
Top answer
1 of 1
4

What is happening here is that when you parse the string, an additional resolution phase happens, and turns the yyyy format specifier representing the "Year Of Era" temporal field into a "Year" temporal field in the returned TemporalAccessor, and you end up with (yearMonthTemp.toString()):

{Year=2001, MonthOfYear=2},ISO

When you format, the yyyy format specifier expects to format a "Year Of Era" temporal field, but the temporal accessor doesn't have it, as you can clearly see above.

If one needed to change the format of a Temporal without knowing the formats head of time, eg from yyyyMM to MMM-yyyy, you would need two different DateTimeFormatters. The formatters would be completely compatible in terms of the fields that they parse and the temporal objects they produce.

If all you want to do is changing formats, you can set the resolver style to STRICT (by default this is SMART):

DateTimeFormatter.ofPattern("yyyyMM")
    .withResolverStyle(ResolverStyle.STRICT);

Though I cannot find documentation for this, I have found that this will prevent it from automatically changing "Year Of Era" to "Year" (which is typically denoted uuuu). From my testing, every parsed temporal field will be present in the result. Assuming that you have the same format specifiers in the formatting DateFormatter, it will get the same temporal fields successfully.

Note that this also means that you cannot easily get something like a YearMonth from this DateTimeFormatter, because YearMonth.parse/YearMonth.from expects a "Year". not "Year Of Era".

// does not work
System.out.println(YearMonth.from(yearMonthTemp));
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › time › format › DateTimeFormatter.html
DateTimeFormatter (Java SE 17 & JDK 17)
January 20, 2026 - As this formatter has an optional element, it may be necessary to parse using parseBest(java.lang.CharSequence, java.time.temporal.TemporalQuery<?>...). The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style. public static final DateTimeFormatter RFC_1123_DATE_TIME
🌐
Joda
joda.org › joda-time › apidocs › org › joda › time › format › DateTimeFormatter.html
DateTimeFormatter (Joda-Time 2.14.1 API)
java.lang.Object · org.joda.time.format.DateTimeFormatter · public class DateTimeFormatter extends Object · Controls the printing and parsing of a datetime to and from a string. This class is the main API for printing and parsing used by most applications.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-time-format-datetimeformatterbuilder-class-in-java
java.time.format.DateTimeFormatterBuilder Class in Java - GeeksforGeeks
July 23, 2025 - // Java program to illustrate DateTimeFormatterBuilder // Using optionalStart() and optionalEnd() Methods // Importing required libraries import java.io.*; import java.lang.*; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; // Main class public class GFG { // Main driver methods public static void main(String[] args) { // Creating an object of DateTimeFormatter class DateTimeFormatter parser = new DateTimeFormatterBuilder() .appendPattern("[yyyy][yyyyM
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › time › format › DateTimeFormatter.html
DateTimeFormatter (Java SE 21 & JDK 21)
January 20, 2026 - As this formatter has an optional element, it may be necessary to parse using parseBest(java.lang.CharSequence, java.time.temporal.TemporalQuery<?>...). The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style. public static final DateTimeFormatter RFC_1123_DATE_TIME
🌐
Coderanch
coderanch.com › t › 772301 › java › Java-Time-Parsing-Date-Format
Using Java Time Parsing a Date to Specified Format (Java in General forum at Coderanch)
May 1, 2023 - Steve Dyke wrote:The date I am getting from a remote data source example 01/03/2022 I want to change it to this format: 2023-01-01 The following code gives me an error: java.time.format.DateTimeParseException: Text '01/03/2022' could not be parsed at index 0 You would need to parse it with "MM/dd/uuuu".
🌐
Java Guides
javaguides.net › 2024 › 06 › java-datetimeformatter.html
Java DateTimeFormatter
June 30, 2024 - The DateTimeFormatter class in Java is a versatile tool for formatting and parsing date-time objects. It supports both predefined and custom formats, allowing for flexible date and time handling.
🌐
Scijava
javadoc.scijava.org › Java9 › java › time › format › class-use › DateTimeFormatter.html
Uses of Class java.time.format.DateTimeFormatter (Java SE 9 & JDK 9 )
Report a bug or suggest an enhancement For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
Javaplanet
javaplanet.io › home › java.time › datetimeformatter
DateTimeFormatter -
September 12, 2025 - import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class FormatterExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formatted = now.format(customFormatter); System.out.println("Current Date-Time (formatted): " + formatted); } }