java Date型时间类型比较大小

java Date型时间类型比较大小

compareTo

        String DateStr1 = "2011-10-1 10:20:16";
        String DateStr2 = "2011-10-07 15:50:35";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime1 = dateFormat.parse(DateStr1);
        Date dateTime2 = dateFormat.parse(DateStr2);
//        i=-1时候 左边小于右边
        int i = dateTime1.compareTo(dateTime2);
        System.out.println(i < 0); //输出true

before/after

//        判断左边比右边早
        System.out.println(dateTime1.before(dateTime2));
//        判断左边比右边晚
        System.out.println(dateTime1.after(dateTime2));
目录导航
  • java Date型时间类型比较大小
    • compareTo
    • before/after