Java – use a specific pattern to format an instant as a string
•
Java
I'm trying to format instant as a string using a specific format According to the question of format instant to string, I do 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 this happened?
thank you
Solution
The mode yyyy-mm-dd't'hh: mm'z 'is wrong:
>Yyyy – week based year error: use uuuu year > mm – month of the year > DD – annual error: use DD date > HH – morning clock hours (1-12) no morning / afternoon. You may want HH hours (0-23) > mm – minutes
This is strange because you even refer to a link with the correct pattern characters Of course, unless you think case has nothing to do with lowercase, but if so, how do you think mm (month) and mm (minute) work?
You may want to actually read the document
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码