![]() ![]() As the name implies, this formatter provides a convenient way to format or parse an instant in UTC.Īccording to the official documentation, an instant cannot be formatted as a date or time without specifying a time zone. DateTimeFormatter for Time ZonesĭateTimeFormatter comes with a great ISO instant formatter called ISO_INSTANT. The output of this last snippet shows that the given time is actually before noon. We may want to parse a time String with our custom formatter and check if it's before noon: DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a") When we want to show AM/PM, we should use lower-case “hh” for hours and add an “a” pattern: String timeColonPattern = "hh:mm:ss a" Note that “HH” is an hour-of-day pattern that generates the output of 0-23. ![]() LocalTime colonTime = LocalTime.of(17, 35, 50).plus(329, ChronoUnit.MILLIS) If we want to add milliseconds to the output, we should add “SSS” to the pattern: String timeColonPattern = "HH:mm:ss SSS" LocalTime colonTime = LocalTime.of(17, 35, 50) DateTimeFormatter for TimeĭateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern) This code snippet checks whether the date “ ” is one of a leap year, which it isn't. (om(europeanDateFormatter.parse("")).isLeapYear()) Likewise, custom formatting patterns can also be used to parse a String that holds a date: DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy") In our example, it would be “July.” A five-letter pattern, “MMMMM,” will make the formatter use the “narrow form.” In our case, “J” would be used. If we happen to use a four-letter pattern for the month, “MMMM,” then we'll get a “full form” representation. When we don't need the mentioned padding with zeroes, we can use a one-letter pattern “M,” which will show January as “1.” If the month number is less than 10, it will be padded with a zero. ![]() If we use a two-letter pattern for the month, we'll get a two-digit month representation. The number of letters in the pattern format is significant. This is an extract of the official Java documentation to DateTimeFormatter class. M/L month-of-year number/text 7 07 Jul July J Note that we used the ofLocalizedDateTime() method of DateTimeFormatter this time. ![]() ZonedDateTime zonedDateTime = ZonedDateTime.of(anotherSummerDay, anotherTime, ZoneId.of("Europe/Helsinki")) ĭateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)ĭateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)ĭateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)ĭateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) LocalTime anotherTime = LocalTime.of(13, 12, 45) To use FormatStyle with time, we have to use the ZonedDateTime instance, otherwise, a DateTimeException will be thrown: LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23) We may also use predefined formatting styles for date and time. The output of these different formatting styles of the same date are: Tuesday, August 23, 2016 (DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(anotherSummerDay)) (DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(anotherSummerDay)) (DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(anotherSummerDay)) (DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(anotherSummerDay)) In such cases, we may use enum (FULL, LONG, MEDIUM, SHORT) values with our DateTimeFormatter: LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |