Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * time1 > time2 ->return >1; time1=time2 >0;time1<time2 >-1
     * @param time1
     * @param time2
     * @return
     */
    public static int compareTime(String time1, String time2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = sdf.parse(time1);
            Date date2 = sdf.parse(time2);
            long ret = (date1.getTime() - date2.getTime());
            if (ret > 0)
                return 1;
            else if (ret < 0)
                return -1;
            else
                return 0;

        } catch (Exception e) {
        }
        return 0;
    }
}