String类型的日期比较,通过Data类提供的before()方法和after()
public class test1 {
public static void main(String[] args) throws ParseException {
Date d=new Date();
System.out.println(d);
String t1=new String(“2017-06-09”);
String t2=new String(“2017-05-08”);
Date d1=new SimpleDateFormat(“yyyy-MM-dd”).parse(t1);//把String字符日期转成Date类型
Date d2=new SimpleDateFormat(“yyyy-MM-dd”).parse(t2);
System.out.println(d1.before(d2));//t1日期是否在t2之前,是返回true,不是返回false
System.out.println(d1.after(d2));//t1日期是否在t2之后,是返回true,不是返回false
}
}
Date类型的日期比较
public class test2 {
public static void main(String[] args) throws ParseException {
Date d1=new Date();
test2 t=new test2();
System.out.println(t.test3());//多输出几遍,争取错开获取的时间
System.out.println(t.test3());
System.out.println(t.test3());
System.out.println(d1.before(t.test3()));//d1日期是否在d2之前,是返回true,不是返回false
System.out.println(d1.after(t.test3()));//d2日期是否在d2之后,是返回true,不是返回false
}
public Date test3(){
Date d2=new Date();
return d2;
}
}