java,js日期比较

 

js

var current_time = "2007-02-02 7:30";
var stop_time = "2007-01-31 8:30";
alert(CompareDate(current_time,stop_time));

function CompareDate(d1,d2)
{
  return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/"))));
}
 

java

public class test {

    public static void main(String args[]) {
       int i= compare_date("1995-11-12 15:21", "1999-12-11 09:59");
       System.out.println("i=="+i);
    }

    public static int compare_date(String DATE1, String DATE2) {
        
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}

 

你可能感兴趣的:(js,java)