如何在Java中检查日期是否大于另一个?

时间:2013-10-01 07:03:08

标签: java date

我正在开发一个Java应用程序,它在命令行中生成一个用户输入的持续时间的报告。用户需要按以下格式输入日期:dd-MM-yyyy

> java report startDate endDate

示例:

java report 01-01-2013 31-03-2013

在代码中,我将日期保存在两个字符串中。我必须确保用户输入的开始日期应该早于结束日期。 是否有一个内置函数可以通过将这两个字符串传递给它来帮助我实现这个目的?

4 个答案:

答案 0 :(得分:99)

您可以使用Date.before()Date.after()Date.equals()进行日期比较。

取自here

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

public class DateDiff {

    public static void main( String[] args )
    {
        compareDates("2017-01-13 00:00:00", "2017-01-14 00:00:00");// output will be Date1 is before Date2
        compareDates("2017-01-13 00:00:00", "2017-01-12 00:00:00");//output will be Date1 is after Date2
        compareDates("2017-01-13 00:00:00", "2017-01-13 10:20:30");//output will be Date1 is before Date2 because date2 is ahead of date 1 by 10:20:30 hours
        compareDates("2017-01-13 00:00:00", "2017-01-13 00:00:00");//output will be Date1 is equal Date2 because both date and time are equal
    }

    public static void compareDates(String d1,String d2)
    {
        try{
            // If you already have date objects then skip 1

            //1
            // Create 2 dates starts
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = sdf.parse(d1);
            Date date2 = sdf.parse(d2);

            System.out.println("Date1"+sdf.format(date1));
            System.out.println("Date2"+sdf.format(date2));System.out.println();

            // Create 2 dates ends
            //1

            // Date object is having 3 methods namely after,before and equals for comparing
            // after() will return true if and only if date1 is after date 2
            if(date1.after(date2)){
                System.out.println("Date1 is after Date2");
            }
            // before() will return true if and only if date1 is before date2
            if(date1.before(date2)){
                System.out.println("Date1 is before Date2");
            }

            //equals() returns true if both the dates are equal
            if(date1.equals(date2)){
                System.out.println("Date1 is equal Date2");
            }

            System.out.println();
        }
        catch(ParseException ex){
            ex.printStackTrace();
        }
    }

    public static void compareDates(Date date1,Date date2)
    {
        // if you already have date objects then skip 1
        //1

        //1

        //date object is having 3 methods namely after,before and equals for comparing
        //after() will return true if and only if date1 is after date 2
        if(date1.after(date2)){
            System.out.println("Date1 is after Date2");
        }

        //before() will return true if and only if date1 is before date2
        if(date1.before(date2)){
            System.out.println("Date1 is before Date2");
        }

        //equals() returns true if both the dates are equal
        if(date1.equals(date2)){
            System.out.println("Date1 is equal Date2");
        }

        System.out.println();
    }
}

答案 1 :(得分:10)

使用firstDate解析两个日期secondDateSimpleDateFormat

firstDate.after(secondDate);

firstDate.before(secondDate);

答案 2 :(得分:9)

将字符串解析为日期,然后使用compareTobefore或之后进行比较

Date d = new Date();
d.compareTo(anotherDate)

Date date1 = new SimpleDateFormat("MM/dd/yyyy").parse(date1string)
Date date2 = new SimpleDateFormat("MM/dd/yyyy").parse(date2string)

date1.compareTo(date2);

用于compareTo的javadoc     http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#compareTo(java.util.Date)

答案 3 :(得分:4)

您需要使用SimpleDateFormatdd-MM-yyyy将是格式)将2个输入字符串解析为Date个对象,然后使用Date#before(otherDate)(或){ {3}}比较它们。

尝试自己实现代码。