判斷字串時間大小
阿新 • • 發佈:2019-02-01
public static int comparedate(String Date1, String Date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date data1 = df.parse(Date1);
Date data2 = df.parse(Date2);
if (data1 .getTime()>data2 .getTime()) {
System.out .println("Date1在Date2後");
return 1;
} else if (data1.getTime()<data2.getTime()){
System.out.println("Date1在Date2前");
return -1;
} else {
System.out.println("Date1與Date2相等");
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}