How to compare two string dates in Java?

后端 未结 8 1292
刺人心
刺人心 2020-12-15 22:41

I have two dates in String format like below -

String startDate = \"2014/09/12 00:00\";

String endDate = \"2014/09/13 00:00\";

I want to

相关标签:
8条回答
  • 2020-12-15 23:34
    public class DateComparision
    {
    
        public static void main(String args[]) throws AssertionError, ParseException
        {
    
            DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    
            //comparing date using compareTo method in Java
            System.out.println("Comparing two Date in Java using CompareTo method");
    
            compareDatesByCompareTo(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
            compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
            compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
    
            //comparing dates in java using Date.before, Date.after and Date.equals
            System.out.println("Comparing two Date in Java using Date's before, after and equals method");
    
            compareDatesByDateMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
            compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
            compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
    
            //comparing dates in java using Calendar.before(), Calendar.after and Calendar.equals()
    
            System.out.println("Comparing two Date in Java using Calendar's before, after and equals method");
            compareDatesByCalendarMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
            compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
            compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));
    
        }
    
        public static void compareDatesByCompareTo(DateFormat df, Date oldDate, Date newDate)
        {
            //how to check if date1 is equal to date2
            if (oldDate.compareTo(newDate) == 0)
            {
                System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
            }
    
            //checking if date1 is less than date 2
            if (oldDate.compareTo(newDate) < 0)
            {
                System.out.println(df.format(oldDate) + " is less than " + df.format(newDate));
            }
    
            //how to check if date1 is greater than date2 in java
            if (oldDate.compareTo(newDate) > 0)
            {
                System.out.println(df.format(oldDate) + " is greater than " + df.format(newDate));
            }
        }
    
        public static void compareDatesByDateMethods(DateFormat df, Date oldDate, Date newDate)
        {
            //how to check if two dates are equals in java
            if (oldDate.equals(newDate))
            {
                System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
            }
    
            //checking if date1 comes before date2
            if (oldDate.before(newDate))
            {
                System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
            }
    
            //checking if date1 comes after date2
            if (oldDate.after(newDate))
            {
                System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
            }
        }
    
        public static void compareDatesByCalendarMethods(DateFormat df, Date oldDate, Date newDate)
        {
    
            //creating calendar instances for date comparision
            Calendar oldCal = Calendar.getInstance();
            Calendar newCal = Calendar.getInstance();
    
            oldCal.setTime(oldDate);
            newCal.setTime(newDate);
    
            //how to check if two dates are equals in java using Calendar
            if (oldCal.equals(newCal))
            {
                System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
            }
    
            //how to check if one date comes before another using Calendar
            if (oldCal.before(newCal))
            {
                System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
            }
    
            //how to check if one date comes after another using Calendar
            if (oldCal.after(newCal))
            {
                System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
            }
        }
    }
    

    OUTPUT

    Comparing two Date in Java using CompareTo method
    01-01-2012 and 01-01-2012 are equal to each other
    02-03-2012 is less than 04-05-2012
    02-03-2012 is greater than 01-02-2012
    
    Comparing two Date in Java using Date's before, after and equals method
    01-01-2012 and 01-01-2012 are equal to each other
    02-03-2012 comes before 04-05-2012
    02-03-2012 comes after 01-02-2012
    
    Comparing two Date in Java using Calendar's before, after and equals method
    01-01-2012 and 01-01-2012 are equal to each other
    02-03-2012 comes before 04-05-2012
    02-03-2012 comes after 01-02-2012
    
    0 讨论(0)
  • 2020-12-15 23:36

    I think it could be done much simpler,

    Using Joda Time

    You can try parsing this dates simply:

    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm");
    DateTime d1 = formatter.parseDateTime(startDate);
    DateTime d2 = formatter.parseDateTime(endDate);
    
    Assert.assertTrue(d1.isBefore(d2));
    Assert.assertTrue(d2.isAfter(d1));
    
    0 讨论(0)
提交回复
热议问题