Java Second Compare compareIgnoreSecond(Date date, Date anotherDate)

Here you can find the source of compareIgnoreSecond(Date date, Date anotherDate)

Description

compare Ignore Second

License

Open Source License

Declaration

public static int compareIgnoreSecond(Date date, Date anotherDate) 

Method Source Code


//package com.java2s;

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

public class Main {

    public static int compareIgnoreSecond(Date date, Date anotherDate) {
        if (date == null) {
            date = new Date();
        }//from  www  .j ava2  s  . c o m

        if (anotherDate == null) {
            anotherDate = new Date();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        date = cal.getTime();

        cal.setTime(anotherDate);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        anotherDate = cal.getTime();

        return date.compareTo(anotherDate);
    }

    public static String getTime(Date date) {
        if (date == null) {
            return null;
        }

        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(date);
    }
}

Related

  1. compareDate(Date firstDate, Date secondDate)
  2. getSecondBetweenDate(Date d1, Date d2)
  3. getSecondCa(Date d1, Date d2)
  4. getSecondsOfTowDiffDate(String p_startDate, String p_endDate)
  5. secondBetween(String date)