Java Date Compare compareTo(Date src, Date dest)

Here you can find the source of compareTo(Date src, Date dest)

Description

compare To

License

Open Source License

Declaration

public static int compareTo(Date src, Date dest) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

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

    public static int compareTo(Date src, Date dest) {
        Date d1 = parseDateTime(formatDateTime(src, DATETIME_PATTERN), DATETIME_PATTERN);
        Date d2 = parseDateTime(formatDateTime(dest, DATETIME_PATTERN), DATETIME_PATTERN);
        int i = d1.compareTo(d2);
        return i;
    }/*from  w w w .j  a v  a2s .  c om*/

    public static Date parseDateTime(String str) {
        return parseDateTime(str, DATETIME_PATTERN);
    }

    public static Date parseDateTime(String str, String pattern) {
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            d = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static String formatDateTime(Date d) {
        return formatDateTime(d, DATETIME_PATTERN);
    }

    public static String formatDateTime(Date d, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(d);
    }
}

Related

  1. compareSecond(java.util.Date date1, java.util.Date date2)
  2. compareStartDateEndDate(String startDate, String endDate)
  3. compareTime(Date date1, Date date2)
  4. compareTime(Date first, Date second)
  5. compareTimesOfDay(Date time1, Date time2)
  6. compareToCal(Date sDate, Date eDate)
  7. compareToMonthByDate(Date startDate, Date endDate)
  8. compareTwoDate(Date beforeDate, Date afterDate)
  9. compareYear(java.util.Date date1, java.util.Date date2)