import org.junit.Test; import java.text.SimpleDateFormat;
import java.util.Date; /**
* @author DateJunit
* @Title: DateJunit
* @ProjectName DateJunit
* @Description: TODO
* @date 2018/8/117:41
*/
public class DateJunit {
@Test
public void test(){
try {
SimpleDateFormat sdft = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
String comparisonStr= "2018-07-31 20:56:56";
String StrOfComparison= "2018-08-01 18:09:52";
Date comparisonDate = sdft.parse(comparisonStr);
Date dateOfComparison = sdft.parse(StrOfComparison); //1.使用Date的compareTo()方法,1 大于;0 等于;-1 小于
System.out.println(comparisonDate.compareTo(dateOfComparison)); //2.使用时间戳直接比较大小
System.out.println(comparisonDate.getTime() > dateOfComparison.getTime()); //3.使用Date的before()、after()方法
//如果前者比后者小返回true,否则为false
System.out.println(comparisonDate.before(dateOfComparison));
//如果前者比后者大返回true,否则为false
System.out.println(comparisonDate.after(dateOfComparison));
}catch (Exception e){
e.printStackTrace();
} }
}