如何检查Java中的日期是否大于其他日期?(复制)

时间:2022-07-02 06:56:28

This question already has an answer here:

这个问题已经有了答案:

I am working on a Java application which generates a report for a duration entered by a user in the command line. The user needs to enter the dates in the following format: dd-MM-yyyy

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

> java report startDate endDate

> java报告开始日期endDate

Example:

例子:

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

java报告01-01-2013 31-03-2013

In the code I save the dates in two strings. I have to make sure that the start date entered by the user should be a date earlier than the end-date. Is there an built-in function which can help me achieve this by passing these two strings to it?

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

4 个解决方案

#1


90  

You can use Date.before() or Date.after() or Date.equals() for date comparison.

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

Taken from 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();
    }
}

#2


10  

Parse the two dates firstDate and secondDate using SimpleDateFormat.

使用SimpleDateFormat解析两个日期firstDate和secondDate。

firstDate.after(secondDate);

firstDate.after(secondDate);

firstDate.before(secondDate);

firstDate.before(secondDate);

#3


10  

Parse the string into date, then compare using compareTo, before or after

将字符串解析为date,然后使用compareTo, before或after进行比较

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

i.e

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

date1.compareTo(date2);

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

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

#4


4  

You need to use a SimpleDateFormat (dd-MM-yyyy will be the format) to parse the 2 input strings to Date objects and then use the Date#before(otherDate) (or) Date#after(otherDate) to compare them.

您需要使用一个SimpleDateFormat (dd-MM-yyyy)来解析2个输入字符串到Date对象,然后使用Date#before(otherDate)(或)日期后(otherDate)来比较它们。

Try to implement the code yourself.

尝试自己实现代码。

#1


90  

You can use Date.before() or Date.after() or Date.equals() for date comparison.

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

Taken from 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();
    }
}

#2


10  

Parse the two dates firstDate and secondDate using SimpleDateFormat.

使用SimpleDateFormat解析两个日期firstDate和secondDate。

firstDate.after(secondDate);

firstDate.after(secondDate);

firstDate.before(secondDate);

firstDate.before(secondDate);

#3


10  

Parse the string into date, then compare using compareTo, before or after

将字符串解析为date,然后使用compareTo, before或after进行比较

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

i.e

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

date1.compareTo(date2);

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

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

#4


4  

You need to use a SimpleDateFormat (dd-MM-yyyy will be the format) to parse the 2 input strings to Date objects and then use the Date#before(otherDate) (or) Date#after(otherDate) to compare them.

您需要使用一个SimpleDateFormat (dd-MM-yyyy)来解析2个输入字符串到Date对象,然后使用Date#before(otherDate)(或)日期后(otherDate)来比较它们。

Try to implement the code yourself.

尝试自己实现代码。