Java Second Compare getSecondCa(Date d1, Date d2)

Here you can find the source of getSecondCa(Date d1, Date d2)

Description

get Second Ca

License

Apache License

Declaration

public static long getSecondCa(Date d1, Date d2) 

Method Source Code


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

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static long getSecondCa(Date d1, Date d2) {
        long quot = Math.abs(d1.getTime() - d2.getTime());
        quot = quot / 1000;/*w  w w. j a v a 2 s  .  c om*/
        return quot;
    }

    public static long getSecondCa(String d1, String d2, String format) {
        return getSecondCa(toDate(d1, format), toDate(d2, format));
    }

    public static long getTime(String s) {
        return parseMysql(s).getTime();
    }

    public static Date toDate(String str) {
        if (null == str || str.trim().isEmpty()) {
            return null;
        }
        try {
            DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return fmt.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date toDate(String dateString, String format) {
        try {
            DateFormat fmt = new SimpleDateFormat(format);
            return fmt.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseMysql(String s) {
        try {
            DateFormat fmtTemp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return fmtTemp.parse(s);
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. compareDate(Date firstDate, Date secondDate)
  2. compareIgnoreSecond(Date date, Date anotherDate)
  3. getSecondBetweenDate(Date d1, Date d2)
  4. getSecondsOfTowDiffDate(String p_startDate, String p_endDate)
  5. secondBetween(String date)
  6. secondOfDate(Date d1, Date d2)
  7. setSeconds(Date date, int amount)