public final class DateTimeFormatter extends Object
Formatter for printing and parsing date-time objects.
public final class LocalDate extends Object
A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03. LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value “2nd October 2007” can be stored in a LocalDate.
public String format(DateTimeFormatter formatter)
Formats this date using the specified formatter. This date will be passed to the formatter to produce a string.
package com.candidjava.datetime; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortDates { public static void main(String[] args) { try { DateTimeFormatter dateFormat=DateTimeFormatter.ofPattern("dd-MM-yyyy"); List<LocalDate> datelist=new ArrayList<LocalDate>(); datelist.add(LocalDate.of(2002, 2, 22)); datelist.add(LocalDate.of(2017, 9, 30)); datelist.add(LocalDate.of(2000, 1, 1)); datelist.add(LocalDate.of(2000, 1, 3)); datelist.add(LocalDate.of(2015, 8, 27)); datelist.add(LocalDate.of(2015, 8, 28)); System.out.println("------------------------------------"); System.out.println("Before Sort:"); System.out.println("------------------------------------"); for (LocalDate localDate : datelist) { System.out.println(localDate.format(dateFormat)); } Collections.sort(datelist); System.out.println("------------------------------------"); System.out.println("After Sort:"); System.out.println("------------------------------------"); for (LocalDate localDate : datelist) { System.out.println(localDate.format(dateFormat)); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
OUTPUT
------------------------------------ Before Sort: ------------------------------------ 22-02-2002 30-09-2017 01-01-2000 03-01-2000 27-08-2015 28-08-2015 ------------------------------------ After Sort: ------------------------------------ 01-01-2000 03-01-2000 22-02-2002 27-08-2015 28-08-2015 30-09-2017