21

I'm trying to format Instant to String with a specific format. Based on the question here Format Instant to String, I'm doing this -

DateTimeFormatter formatter = DateTimeFormatter
        .ofPattern("YYYY-MM-DD'T'hh:mm'Z'")
        .withZone(ZoneOffset.UTC);

// Fails for current time with error 'Field DayOfYear cannot be printed as the 
// value 148 exceeds the maximum print width of 2'
LocalDateTime 
      .ofInstant(Instant.now(), ZoneOffset.UTC)
      .format(DATE_TIME_FORMATTER);

// But works for smaller values of Instant    
LocalDateTime
     .ofInstant(Instant.ofEpochMilli(604800000), ZoneOffset.UTC)
     .format(DATE_TIME_FORMATTER));

Any suggestions on why is this happening?

Thanks

3
  • 6
    D is not the same as d.
    – Ingo Bürk
    Commented May 27, 2016 at 22:27
  • oh man..just wasted a lot of time. Thnx! Commented May 27, 2016 at 22:31
  • Cant they have a better exception trace :-(
    – prash
    Commented Oct 19, 2016 at 13:10

2 Answers 2

22

Pattern YYYY-MM-DD'T'hh:mm'Z' is wrong:

  • YYYY - week-based-year       wrong: use uuuu year
  • MM - month-of-year
  • DD - day-of-year       wrong: use dd day-of-month
  • hh - clock-hour-of-am-pm (1-12)       without AM/PM you probably want HH hour-of-day (0-23)
  • mm - minute-of-hour

It's weird, because you even referenced a link that had the right pattern characters. Unless of course you thought upper- vs lower-case didn't matter, but if so, how did you think MM (month) vs mm (minute) worked?

You might want to actually read the documentation.

8
  • what is the difference between yyyy and uuuu
    – Sumit
    Commented Apr 15, 2019 at 18:25
  • 1
    @Sumit See last line of answer: "You might want to actually read the documentation". --- Or read the answers to this question: uuuu versus yyyy in DateTimeFormatter formatting pattern codes in Java?
    – Andreas
    Commented Apr 15, 2019 at 20:01
  • 1
    Yes, reading the documentation is actually what confused me. I couldn't understand why yyyy is wrong and why uuuu is correct. It says u is year and y` is year-of-era and then it gives the exact same example: 2004; 04 for both, based on that, I have no idea what is the difference between the two. The other SO answer link that you provided, you seem to have a contradictory opinion with your Julius Caesar example and using y GG seems to be better than using u to avoid negative years. But, the other answers written there were helpful so, Thanks!
    – Sumit
    Commented Apr 16, 2019 at 17:16
  • 8
    -1 due to a condescending tone. YYYY would appear to the layman (or someone skimming the documentation) to be a sensible way to attain the year. Thanks for your answer, but SO exists to clear up these misconceptions, even if said misconceptions are due to negligence. Commented Sep 5, 2019 at 13:46
  • 4
    -1 There is nothing wrong with the question and even though you answered it there was no need to be condescending "You might want to actually read the documentation".
    – Old Nick
    Commented Feb 21, 2020 at 12:49
8

Take a look at the documentation of the DateTimeFormatter. So, D stands for the day of the year, while d is the day of the month, which is what you want.

Plus, there are several formats that are already defined. The one you want is almost like DateTimeFormatter.ISO_INSTANT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.