Java SQL Time compareTime(String time1, String time2)

Here you can find the source of compareTime(String time1, String time2)

Description

compare Time

License

Apache License

Declaration

public static int compareTime(String time1, String time2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;
import java.text.*;

public class Main {

    public static int compareTime(String time1, String time2) {

        Date newDate1 = parseDate("1990-01-01 " + time1, "yyyy-MM-dd HH:mm:ss");
        Date newDate2 = parseDate("1990-01-01 " + time2, "yyyy-MM-dd HH:mm:ss");
        if (getMillis(newDate1) - getMillis(newDate2) > 0)
            return 1;
        else if (getMillis(newDate1) - getMillis(newDate2) < 0)
            return -1;
        return 0;
    }/*  w  ww . j  a v a  2s . c  om*/

    public static Date parseDate(java.sql.Date date) {
        return date;
    }

    public static java.util.Date parseDate(String dateStr, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static long getMillis(Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }
}

Related

  1. createTime(int hour, int minute, int second)
  2. dateTime2str(java.sql.Date date)
  3. dateTimeFormat(Date date,String pattern)
  4. DateTimeSpace(String starttime, String endtime)