The date and time classes support many methods to get data out of them:
LocalDate date = LocalDate.of(2016, Month.JANUARY, 20); System.out.println(date.getDayOfWeek()); // MONDAY System.out.println(date.getMonth()); // JANUARY System.out.println(date.getYear()); // 2016 System.out.println(date.getDayOfYear()); // 20
Java has DateTimeFormatter class to format any type of date and/or time object.
What changes is the format. DateTimeFormatter is in the package java.time.format.
LocalDate date = LocalDate.of(2016, Month.JANUARY, 20); LocalTime time = LocalTime.of(11, 12, 34); LocalDateTime dateTime = LocalDateTime.of(date, time); System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME)); System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
ISO is a standard for dates. The output of the previous code looks like this:
2016-01-20 11:12:34 2016-01-20T11:12:34
There are some predefined formats that are more useful:
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println(shortDateTime.format(dateTime)); // 1/20/20 System.out.println(shortDateTime.format(date)); // 1/20/20 System.out.println(shortDateTime.format(time)); // UnsupportedTemporalTypeException
The last line throws an exception because a time cannot be formatted as a date.
The following statements print exactly the same thing as the previous code:
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println(dateTime.format(shortDateTime)); System.out.println(date.format(shortDateTime)); System.out.println(time.format(shortDateTime));
There are two predefined formats that can show up on the exam: SHORT and MEDIUM.
LocalDate date = LocalDate.of(2016, Month.JANUARY, 20); LocalTime time = LocalTime.of(11, 12, 34); LocalDateTime dateTime = LocalDateTime.of(date, time); DateTimeFormatter shortF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT); DateTimeFormatter mediumF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); System.out.println(shortF.format(dateTime)); // 1/20/20 11:12 AM System.out.println(mediumF.format(dateTime)); // Jan 20, 2016 11:12:34 AM
To use one of the predefined formats, create your own.
For example, this code spells out the month:
DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm");
System.out.println(dateTime.format(f)); // January 20, 2016, 11:12
MMMM -- M represents the month. The more Ms you have, the more verbose the Java output. For example, M outputs 1, MM outputs 01, MMM outputs Jan, and MMMM outputs January.
dd -- d represents the date in the month. As with month, the more ds you have, the more verbose the Java output. dd means to include the leading zero for a single-digit month.
, to output a comma.
yyyy -- y represents the year. yy outputs a two-digit year and yyyy outputs a four-digit year.
hh -- h represents the hour. Use hh to include the leading zero if you're outputting a single-digit hour.
: -- to output a colon.
mm -- m represents the minute.
Can you figure out which of these lines will throw an exception?
1: DateTimeFormatter f = DateTimeFormatter.ofPattern("hh:mm");
2: f.format(dateTime);
3: f.format(date);
4: f.format(time);
We have h for hour and m for minute. Remember M (uppercase) is month and m (lowercase) is minute.
We can only use this formatter with objects containing times. Therefore, line 3 will throw an exception.
parse() method takes a formatter as well its parameter. If you don't specify one, it uses the default for that type.
DateTimeFormatter f = DateTimeFormatter.ofPattern("MM dd yyyy"); LocalDate date = LocalDate.parse("01 02 2015", f); LocalTime time = LocalTime.parse("11:22"); System.out.println(date); // 2015-01-02 System.out.println(time); // 11:22
Here we show using both a custom formatter and a default value.