如何比较日期是否大于其他日期?
How to compare if a date is greater than other?
I am taking the current date in ymd
format as below :
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
I also have a date as String as below :
String futureDate = "2022-03-07"
How can I know if the current date is less than or greater than future date ?
I tried to use compareTo()
function, but was unable to compare the two dates.
要比较日期,您可以转换为 Date
并使用:
final Date other = simpleDateFormat.parse("2022-03-07");
final Date now = new Date();
if (other.after(now)) {
// In the future!
} else if(other.before(now)) {
// In the past!
} else {
// In the present!
}
I am taking the current date in
ymd
format as below :
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
I also have a date as String as below :
String futureDate = "2022-03-07"
How can I know if the current date is less than or greater than future date ?
I tried to use
compareTo()
function, but was unable to compare the two dates.
要比较日期,您可以转换为 Date
并使用:
final Date other = simpleDateFormat.parse("2022-03-07");
final Date now = new Date();
if (other.after(now)) {
// In the future!
} else if(other.before(now)) {
// In the past!
} else {
// In the present!
}