Java Time Different getDiffTime(Date beforeTime, Date afterTime)

Here you can find the source of getDiffTime(Date beforeTime, Date afterTime)

Description

get Diff Time

License

Open Source License

Declaration

public static long getDiffTime(Date beforeTime, Date afterTime) 

Method Source Code

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

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**//from   ww w .  ja  va2s  .c o  m
     * format pattern : HH:mm:ss
     */
    public static final SimpleDateFormat FORMAT_HMS = new SimpleDateFormat("HH:mm:ss");

    public static long getDiffTime(Date beforeTime, Date afterTime) {
        try {
            String beforeTimeStr = FORMAT_HMS.format(beforeTime);
            String afterTimeStr = FORMAT_HMS.format(afterTime);
            Date bTime = parseTime(beforeTimeStr);
            Date aTime = parseTime(afterTimeStr);
            long diff = aTime.getTime() - bTime.getTime();
            return diff;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    static public String format(Date date, String formatText) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(formatText);
        return format.format(date);
    }

    /**
     * Parses text in 'HH:mm:ss' format to produce a time.
     * 
     * @param s
     *            the text
     * @return Date
     * @throws ParseException
     */
    static public Date parseTime(String s) throws ParseException {
        return FORMAT_HMS.parse(s);
    }
}

Related

  1. calculateExecutionTimeDiff(String startDate, String endDate)
  2. createDate(String strDate, int[] timeDiff, boolean plusDiff)
  3. diffTime(String sTime, String fTime, String Dateformat1)
  4. getDiffDateTime(int diff)
  5. getDifferent(final Date timeFrom, final Date timeTo)
  6. getDiffTime(long startTime)
  7. getDiffTime(String strDate, int idx)
  8. getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)
  9. getTimeDiff(String start, String end, String format, int timeDiff)