15

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

Community
  • 1
  • 1
RandomQuestion
  • 6,176
  • 16
  • 52
  • 91

2 Answers2

20

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.

Andreas
  • 138,167
  • 8
  • 112
  • 195
  • what is the difference between `yyyy` and `uuuu` – Sumit Apr 15 '19 at 18:25
  • 1
    @Sumit See last line of answer: *"You might want to actually read the [documentation](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns)"*. --- Or read the answers to this question: [`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?](https://stackoverflow.com/q/41177442/5221149) – Andreas Apr 15 '19 at 20:01
  • 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 Apr 16 '19 at 17:16
  • @Sumit It's not a contradiction, it's an example of the difference. If you did Julius Caesar example with `y` without `GG`, you'd get `44` alone, which is certainly wrong, since correct year is `44 BC` or `-43`. It's like using `h` without `a`, i.e. `17:15` and `5:15 PM` are both correct, but `5:15` alone is the wrong time of day. – Andreas Apr 16 '19 at 18:59
  • that makes more sense. so the correct way would be to use `GG` as well if you are using `y`, or use `u` if you are ok with having a negative value as year. – Sumit Apr 16 '19 at 19:03
  • @Sumit And that is exactly what my answer to that other question said, if you read the paragraph starting the "Conclusion:". ***Conclusion:** If you use `y` you should also use `G`. Since `G` is rarely used, the correct year symbol is `u`, not `y`, otherwise a non-positive year will show incorrectly.* – Andreas Apr 16 '19 at 19:05
  • 3
    -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. – Kenny Worden Sep 05 '19 at 13:46
  • 1
    -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 Feb 21 '20 at 12:49
4

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.

T. Claverie
  • 8,107
  • 1
  • 10
  • 24