Android Date Interval Get dateDiff(String startTime, String endTime)

Here you can find the source of dateDiff(String startTime, String endTime)

Description

date Diff

License

Open Source License

Declaration

public static String dateDiff(String startTime, String endTime) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static final String YMD_DASH_WITH_TIME = "yyyy-MM-dd HH:mm:ss";
    private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

    public static Long dateDiff(String startTime, String endTime,
            String format, String str) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {// www .j  a v  a  2 s  . c  o  m
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;
            hour = diff % nd / nh + day * 24;
            min = diff % nd % nh / nm + day * 24 * 60;
            sec = diff % nd % nh % nm / ns;
            if (str.equalsIgnoreCase("h")) {
                return hour;
            } else {
                return min;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (str.equalsIgnoreCase("h")) {
            return hour;
        } else {
            return min;
        }
    }

    public static String dateDiff(String startTime, String endTime) {
        String result = "";
        SimpleDateFormat sd = new SimpleDateFormat(YMD_DASH_WITH_TIME);
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        @SuppressWarnings("unused")
        long sec = 0;
        try {
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;
            hour = diff % nd / nh + day * 24;
            min = (hour * 60) + (diff % nd % nh / nm + day * 24 * 60);
            sec = diff % nd % nh % nm / ns;
            if (min > 60) {
                if (hour > 60) {
                    result = day + " ";
                } else {
                    result = hour + " ";
                }
            } else {
                result = min + " ";
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param source
     * @param pattern
     * @return Date
     */
    public static Date parse(String source, String pattern) {
        if (source == null) {
            return null;
        }
        Date date;
        try {
            date = getFormat(pattern).parse(source);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    public static DateFormat getFormat(String pattern) {
        DateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern);
            DFS.put(pattern, format);
        }
        return format;
    }
}

Related

  1. DaysBetween(Date beginDate, Date endDate)
  2. calculateDiffInDays(Date date1, Date date2)
  3. calculateDiffInHours(Date date1, Date date2)
  4. calculateDiffInMilliSeconds(Date date1, Date date2)
  5. calculateDiffInMinutes(Date date1, Date date2)
  6. dateDiff(String startTime, String endTime, String format, String str)
  7. dayDiff(Date date1, Date date2)
  8. dayOffset(Date date, int offset)
  9. daysBetweenDate(Date date1, Date date2)