The DateTimeFormatter predefined formatter are listed in the following table:
Formatter | Target | Description | Example |
---|---|---|---|
BASIC_ISO_DATE | date, time, date and time | without using a separator between two date components. | 20200109, 20200109-0600 |
ISO_DATE, | date | ISO separators | 2020-01-09 |
ISO_TIME, | time | ISO separators | 15:38:32.927, 15:38:32.943-06:00, |
ISO_DATE_TIME | date and time | ISO separators | 2020-01-09-06:00, 2020-01-09T15:20:07.747-06:00, 2020-01-09T15:20:07.825-06:00[America/Chicago] |
ISO_INSTANT | instant or ZonedDateTime | in UTC format | 2020-01-09T21:23:56.870Z |
ISO_LOCAL_DATE, | Date | without an offset | 2020-01-09, |
ISO_LOCAL_TIME, | time | without an offset | 15:30:14.352, |
ISO_LOCAL_DATE_TIME | date and time | without an offset | 2020-01-09T15:29:11.384 |
ISO_OFFSET_DATE, | Date | with an offset using ISO format | 2020-01-09-06:00, |
ISO_OFFSET_TIME, | time | with an offset using ISO format | 15:34:29.851-06:00, |
ISO_OFFSET_DATE_TIME | date and time | with an offset using ISO format | 2020-01-09T15:33:07.07-06:0 |
ISO_ZONED_DATE_TIME | date and time | with a zone id, if available | 2020-01-09T15:45:49.112-06:00, 2020-01-09T15:45:49.128-06:00[America/Chicago] |
ISO_ORDINAL_DATE | date | with year and day-of-year | 2020-009 |
ISO_WEEK_DATE | date (week-based dates) | The format is year-Wweek_of_year-day_of_week | 2020-W02-4-06:00 2020-W02-4, '2020-W02-4' means the fourth day of the second week in 2020. |
RFC_1123_DATE_TIME | date and time | for e-mails using the RFC1123 specification | Thu, 9 Jan 2020 15:50:44 -060 |
The following code has runtime error.
A runtime error as a LocalTime does not contain date components
import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { String s = DateTimeFormatter.ISO_DATE.format(LocalTime.now()); System.out.println(s);/*from w w w . ja v a2 s. c o m*/ } }
The date time object being formatted must contain the components as suggested by formatter.
ISO_DATE formatter expects the date components.