Java Reference
In-Depth Information
LocalDateTime aLDT = LocalDateTime . parse ( armisticeDateTime );
System . out . println ( "Date/Time: " + aLDT );
As you probably expect by now, the default format is the ISO8601 date format. However, we
often have to deal with dates in other formats. For this, the DateTimeFormatter allows you
to specify a particular pattern. For example, “dd MMM uuuu” represents the day of the
month (two digits), three letters of the name of the month (Jan, Feb, Mar, …), and a four-di-
git year:
DateTimeFormatter df = DateTimeFormatter . ofPattern ( "dd MMM uuuu" );
String anotherDate = "27 Jan 2011" ;
LocalDate random = LocalDate . parse ( anotherDate , df );
System . out . println ( anotherDate + " parses as " + random );
As its name implies, the DateTimeFormatter object is bidirectional; it can both parse input
and format output. We could add this line to the DateParse example:
System . out . println ( aLD + " formats as " + df . format ( aLD ));
When we run the program, we see the output as follows:
Date: 1914-11-11
Date/Time: 1914-11-11T11:11
27 Jan 2011 parses as 2011-01-27
1914-11-11 formats as 11 Nov 1914
Difference Between Two Dates
Problem
You need to compute the difference between two dates.
Solution
Use the static method Period.between() to find the difference between two LocalDates .
Search WWH ::




Custom Search