[Java] How to use compareTo method of Date class

What is the compareTo method?

A method that compares the dates of the Date class, which compares the date of the caller of the method with the date of the argument.

Compares the argument with the caller and returns the value as follows:

0 if the value of the method caller is equal to the argument -1 if the method caller value is before the argument 1 if the method caller value is after the argument

How to use

        SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMdd");
		Date date1,date2;
        date1 = sdf.parse("20210117");
	    date2 = sdf.parse("20210118");

        System.out.println(date1.compareTo(date2)); // -1 ->date1 is before date2
        System.out.println(date2.compareTo(date1)); // 1 ->date2 is after date1

reference

Class Date #compareTo

Recommended Posts

[Java] How to use compareTo method of Date class
How to use java class
[Java] How to use Calendar class and Date class
[Java] How to use join method
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
[Java] How to use the File class
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Java] How to use the toString () method
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Java] How to use the Calendar class
Use of Date class
[Java] How to use FileReader class and BufferedReader class
Output of how to use the slice method
How to use the replace () method (Java Silver)
[Java] How to use Map
[Java] How to use Map
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
How to use the link_to method
How to use Java HttpClient (Get)
How to use the include? method
How to use the form_with method
How to disassemble Java class files
How to use Java HttpClient (Post)
How to use the wrapper class
How to use setDefaultCloseOperation () of JFrame
[Processing × Java] How to use variables
[Ruby] How to use any? Method
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Ruby inject method
How to use Java lambda expressions
How to use Java enum type
How to get the class name / method name running in Java
How to use trained model of tensorflow2.0 with Kotlin / Java
How to implement date calculation in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the hasNext function
[Java] How to compare with equals method
[java] Summary of how to handle char
[Rails] How to use the map method
[Easy-to-understand explanation! ] How to use Java instance
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to set the Date time to 00:00:00