1. 程式人生 > >判斷字串時間大小

判斷字串時間大小

    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; }