LocalDateTime compareTo() method in Java with Examples Last Updated : 30 Nov, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The compareTo() method of LocalDateTime class in Java is used to compare this date-time to the date-time passed as the parameter. Syntax: public int compareTo(ChronoLocalDateTime anotherDate) Parameter: This method accepts a parameter anotherDate which specifies the other date-time to be compare to. It should not be null. Returns: The function returns an integer value which is the comparator value after comparison. Below programs illustrate the LocalDateTime.compareTo() method: Program 1: Java // Program to illustrate the compareTo() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Parses the date LocalDateTime dt1 = LocalDateTime .parse("2018-11-03T12:45:30"); // Prints the date System.out.println("Date 1: " + dt1); // Parses the date LocalDateTime dt2 = LocalDateTime .parse("2015-01-05T12:45:30"); // Prints the date System.out.println("Date 2: " + dt2); // Compares the date System.out.println("After comparison: " + dt2.compareTo(dt1)); } } Output: Date 1: 2018-11-03T12:45:30 Date 2: 2015-01-05T12:45:30 After comparison: -3 Program 2: Java // Program to illustrate the compareTo() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Parses the date LocalDateTime dt1 = LocalDateTime .parse("2010-12-05T12:50:30"); // Prints the date System.out.println("Date 1: " + dt1); // Parses the date LocalDateTime dt2 = LocalDateTime .parse("2012-05-10T12:50:30"); // Prints the date System.out.println("Date 2: " + dt2); // Compares the date System.out.println("After comparison: " + dt2.compareTo(dt1)); } } Output: Date 1: 2010-12-05T12:50:30 Date 2: 2012-05-10T12:50:30 After comparison: 2 Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#compareTo(java.time.chrono.ChronoLocalDateTime) Comment More infoAdvertise with us Next Article LocalTime compareTo() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-LocalDateTime Java-Functions Java-time package Practice Tags : Java Similar Reads LocalTime compareTo() method in Java with Examples The compareTo() method of a LocalTime class is used to compare this LocalTime object to the LocalTime passed as parameter to check whether both LocalTimes are equal. The comparison between both LocalTimes is based on timeline position of the local times within a day. The value to be returned by this 3 min read LocalDateTime equals() method in Java with Examples The equals() method of LocalDateTime class in Java checks if this date-time is equal to another date-time. This other date-time is passed as the parameter. This method returns a boolean value showing the same. Syntax: public boolean equals(Object date2) Parameter: This method accepts a parameter dat 2 min read LocalDateTime from() method in Java with Examples The from() method of LocalDateTime class is used to obtain an instance of LocalDateTime from the temporal object passed as the parameter. Syntax: public static LocalDateTime from(TemporalAccessor temporal) Parameter: This method accepts a parameter temporal which specifies the temporal object to be 1 min read LocalTime atDate() method in Java with Examples The atDate() method of a LocalTime class is used to combine this LocalTime Object with a LocalDate Object to create a LocalDateTime. All possible combinations of date and time are valid. Syntax: public LocalDateTime atDate(LocalDate date) Parameters: This method accepts a single parameter date which 1 min read LocalDateTime until() Method in Java with Examples until() method of the LocalDateTime class used to calculate the amount of time between two LocalDateTime objects using TemporalUnit. The start and end points are this and the specified LocalDateTime passed as a parameter. The result will be negative if the end is before the start. The calculation re 2 min read LocalDateTime query() Method in Java with Examples query() method of an LocalDateTime class used to query this LocalDateTime using the specified query as parameter.The TemporalQuery object passed as parameter define the logic to be used to obtain the result from this LocalDateTime. Syntax: public <R> R query(TemporalQuery<R> query) Param 2 min read Like