java比较时间

使用Date

before,after这两个可以简单比较时间先后,返回boolean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.text.ParseException;  
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeChecker {
public static void main(String[] args) throws InterruptedException, ParseException {

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

Date expiratDate = sdf.parse("2024-03-07");

Date nowDate = new Date();

System.out.println(expiratDate.before(nowDate));

}
}