SoFunction
Updated on 2025-03-03

The issue of comparing the size of date type in Java program

Preface

When encountering time comparison problems during work, here are several commonly used Date type data comparison size methods.

  • compareTo()method
  • before()method
  • after()method
  • getTime()method

1. CompareTo method

    /**
      * Time comparison method compareTo
      * If d1 is larger than d2, the result = 1
      * If d1 is more than d2, etc. The result = 0
      * If d1 is smaller than d2, the result =-1
      *
      * @param d1
      * @param d2
      */
    private void printResultCompareTo(Date d1,Date d2){
        if((d2) == 1){
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "after");
        }else if((d2) == 0){
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " = " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) );
        }else{
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "Before");
        }
    }

2. Before method

    /**
      * Compare using the before method
      *
      * @param d1
      * @param d2
      */
    private void printResultBefore(Date d1,Date d2){
        if((d2)){
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "Before");
        }else{
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "after");
        }
    }

3. After method

    /**
      * Compare using after method
      *
      * @param d1
      * @param d2
      */
    private void printResultAfter(Date d1,Date d2){
        if((d2)){
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "after");
        }else{
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "Before");
        }
    }

4. getTime method

    /**
      * Use getTime method to get millisecond values ​​comparison
      *
      * @param d1
      * @param d2
      */
    private void printResultGetTime(Date d1,Date d2){
        if(() > ()){
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "after");
        }else if(() == ()){
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " = " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) );
        }else{
            (new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d1) + " exist " + new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(d2) + "Before");
        }
    }

Test code

    //Define a date1String1 string    private final String date1String1 = "2018-09-01 23:23:59";
    //Define a date1String2 string    private final String date1String2 = "2020-04-20 23:23:59";
    //Define a date1String3 string    private final String date1String3 = "2021-04-20 23:23:59";
    //Parameter formatting type    private final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    //Define date type data    private Date date1;
    private Date date2;
    private Date date3;

    @Before
    public void beforPrint() throws ParseException {
        date1 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(date1String1);
        date2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(date1String2);
        date3 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(date1String3);
        ("date1: " + date1String1);
        ("date2: " + date1String2);
        ("date3: " + date1String3);
    }

    /**
      * Compare with compareTo
      */
    @Test
    public void testCompare(){
        ("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------);
        printResultCompareTo(date1, date2);
        printResultCompareTo(date3, date2);
        ("------------------------------------------------------------------------------------------------------------------------------);
        printResultBefore(date1,date2);
        printResultBefore(date3, date2);
        ("------------------------------------------------------------------------------------------------------------------------------);
        printResultAfter(date1, date2);
        printResultAfter(date3, date2);
        ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------);
        printResultGetTime(date1, date2);
        printResultGetTime(date3, date2);
    }

Test results

as follows:

date1: 2018-09-01 23:23:59
date2: 2020-04-20 23:23:59
date3: 2021-04-20 23:23:59
--------------------------------------------------------------------------------------------------------------------------------
2018-09-01 23:23:59 before 2020-04-20 23:23:59
2021-04-20 23:23:59 After 2020-04-20 23:23:59
--------------------------------------------------------------------------------------------------------------------------------
2018-09-01 23:23:59 before 2020-04-20 23:23:59
2021-04-20 23:23:59 After 2020-04-20 23:23:59
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2018-09-01 23:23:59 before 2020-04-20 23:23:59
2021-04-20 23:23:59 After 2020-04-20 23:23:59
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2018-09-01 23:23:59 before 2020-04-20 23:23:59
2021-04-20 23:23:59 After 2020-04-20 23:23:59

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.