Java Date Difference getDifference(String dateStr1, String dateStr2, char choice)

Here you can find the source of getDifference(String dateStr1, String dateStr2, char choice)

Description

Gets the difference between 2 dates in Seconds or Minutes or Hours or Days based on the choice given.

License

Open Source License

Declaration

public static long getDifference(String dateStr1, String dateStr2, char choice) 

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.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    private static final String STANDARD_DATE_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**/*from   w w  w. j  a  v  a2  s .  c om*/
     * Gets the difference between 2 dates in Seconds or Minutes or Hours or Days based on the choice given.
     * If no choice is given, by default, it returns the time difference in Milliseconds
     */
    public static long getDifference(String dateStr1, String dateStr2, char choice) {
        if (dateStr1 == null || dateStr2 == null)
            throw new RuntimeException("Invalid Input");
        DateFormat df = new SimpleDateFormat(STANDARD_DATE_FORMAT_STRING);

        Date date1 = null, date2 = null;
        try {
            date1 = df.parse(dateStr1);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("Invalid Date Format " + dateStr1);
        }
        try {
            date2 = df.parse(dateStr2);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("Invalid Date Format " + dateStr2);
        }

        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        cal1.setTime(date1);
        cal2.setTime(date2);

        long milis1 = cal1.getTimeInMillis();
        long milis2 = cal2.getTimeInMillis();
        long diffMillis = milis2 - milis1;
        long diffSeconds = diffMillis / 1000;
        long diffMinutes = diffMillis / (60 * 1000);
        long diffHours = diffMillis / (60 * 60 * 1000);
        long diffDays = diffMillis / (24 * 60 * 60 * 1000);

        switch (choice) {
        case 'S':
            return diffSeconds;
        case 's':
            return diffSeconds;
        case 'M':
            return diffMinutes;
        case 'm':
            return diffMinutes;
        case 'H':
            return diffHours;
        case 'h':
            return diffHours;
        case 'D':
            return diffDays;
        case 'd':
            return diffDays;
        default:
            return diffMillis;
        }
    }
}

Related

  1. getDiff(Date from, Date to)
  2. getDiffDate(String srcDate, String format, int diff)
  3. getDiffDays(Date from, Date to)
  4. getDiffDays2(Date one, Date two)
  5. getDifference(Date a, Date b)
  6. getDifferenceInDays(Calendar calendar0, Calendar calendar1)
  7. getDiffMon(Date dt, int idiff)
  8. getDiffMon(Date dt, int idiff)
  9. getDiffToString(Date date, int n, String pattern)