Java Time Compare 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) throws ParseException 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static int compareTime(String time1, String time2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        Date date1 = sdf.parse(time1);
        Date date2 = sdf.parse(time2);
        long result = date1.getTime() - date2.getTime();
        if (result > 0) {
            return 1;
        } else if (result == 0) {
            return 0;
        } else {/*from   w w w  .  j a  v  a 2 s .c  o  m*/
            return -1;
        }
    }
}

Related

  1. compareStringTime(String t1, String t2, String parrten)
  2. compareTime(String time1, String time2, String pattern)