Java 如何比较字符串格式的两个日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32330661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:21:14  来源:igfitidea点击:

How to compare two dates in String format?

javadatejodatime

提问by Klausos Klausos

What is the easiest way to compare two dates in String format? I would need to know if date1comes after/before date2.

以字符串格式比较两个日期的最简单方法是什么?我需要知道是否date1在之后/之前date2

String date1 = "2015-12-01";
String date2 = "2015-12-31";

采纳答案by Micha? Szyd?owski

For a more generic approach, you can convert them both to Dateobjects of a defined format, and then use the appropriate methods:

对于更通用的方法,您可以将它们都转换Date为定义格式的对象,然后使用适当的方法:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date date1 = format.parse(date1);
Date date2 = format.parse(date2);

if (date1.compareTo(date2) <= 0) {
    System.out.println("earlier");
}

回答by Danil Gaponov

In this very case you can just compare strings date1.compareTo(date2).

在这种情况下,您可以只比较字符串date1.compareTo(date2)

EDIT: However, the proper way is to use SimpleDateFormat:

编辑:但是,正确的方法是使用SimpleDateFormat

DateFormat f = new SimpleDateFormat("yyyy-mm-dd");
Date d1 = f.parse(date1, new ParsePosition(0));
Date d2 = f.parse(date2, new ParsePosition(0));

And then compare dates:

然后比较日期:

d1.compareTo(d2);

The comparison will return negative value if d1is before d2and positive if d1is after d2.

如果d1在之前d2,比较将返回负值,如果d1在之后则返回正值d2

回答by Atais

With JodaTime, simply:

使用 JodaTime,只需:

DateTime d1 = DateTime.parse(date1);
DateTime d2 = DateTime.parse(date2);

Assert.assertTrue(d1.isBefore(d2));
Assert.assertTrue(d2.isAfter(d1));

回答by LBes

You can simply convert your string to dates:

您可以简单地将字符串转换为日期:

Date date1 = sdf.parse("2015-12-01");
Date date2 = sdf.parse("2010-01-01");

Then the comparison is done like: if(date1.after(date2)){ System.out.println("Date1 is after Date2"); }

然后比较是这样完成的: if(date1.after(date2)){ System.out.println("Date1 is after Date2"); }

if(date1.before(date2)){
            System.out.println("Date1 is before Date2");
}

if(date1.equals(date2)){
            System.out.println("Date1 is equal Date2");
}

If I remember correctly there's a ParseExceptionto handle

如果我没记错的话,有一个ParseException要处理的

回答by Abhijit Kumbhar

Yes you can compare this as follows:

是的,您可以按如下方式进行比较:

Date date1 = new Date();
Date date2 = new Date();

if(date1.before(date2)){
    // statement
}

if(date1.after(date2)){
    // statement
}

回答by akhil_mittal

I would advise you to convert them to Dateobjects.

我建议您将它们转换为Date对象。

String date1 = "2015-12-01";
String date2 = "2015-12-31";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.parse(date1).before(sdf.parse(date2)));

Comparing them like:

比较它们,例如:

if(date2 .compareTo(date1) > 0){
    System.out.println(" ---- ");
}

is not an ideal option (date1 and date2 are strings) as it loses the flexibility and can lead to its own share of problems.

不是一个理想的选择(date1 和 date2 是字符串),因为它失去了灵活性并可能导致其自身的问题。