Answers for "How to compare two string dates with different date formats in Java"

0

How to compare two string dates with different date formats in Java

package javaapplication;

import java.text.*;
import java.util.Date;
import java.text.ParseException;

public class date {

    public static void main(String[] args) {

        String startDate = "21-08-2027";
        String endDate = "21/08/2021";

        try {

            Date dateStart = new SimpleDateFormat("dd-MM-yyyy").parse(startDate);

            Date dateEnd = new SimpleDateFormat("dd/MM/yyyy").parse(endDate);

            if (dateStart.after(dateEnd)) {
                System.out.println("Start Date is greater than End Date");

            } else if (dateStart.before(dateEnd)) {

                System.out.println("Start Date is less than End Date");

            } else {

                System.out.println("Start Date is Equal to End Date");
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
Posted by: Guest on October-03-2021

Code answers related to "How to compare two string dates with different date formats in Java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language