Java Time Different timeDiff(Calendar calenndarCurrentTime, Calendar calenndarStartTime)

Here you can find the source of timeDiff(Calendar calenndarCurrentTime, Calendar calenndarStartTime)

Description

time Diff

License

Open Source License

Declaration

public static String timeDiff(Calendar calenndarCurrentTime, Calendar calenndarStartTime) 

Method Source Code

//package com.java2s;
/*// www  .  ja v a 2s  .c  o m
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

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

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static String timeDiff(String currentTime, String startTime) {
        if (currentTime == null || startTime == null) {
            return "00:00:00";
        }
        Calendar calenndarCurrentTime = toCalendar(currentTime);
        Calendar calenndarStartTime = toCalendar(startTime);
        return timeDiff(calenndarCurrentTime, calenndarStartTime);
    }

    public static String timeDiff(Calendar calenndarCurrentTime, Calendar calenndarStartTime) {

        long startTime = calenndarStartTime.getTimeInMillis();
        long currentTime = calenndarCurrentTime.getTimeInMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        long elapsed = currentTime - startTime;

        return dateFormat.format(new Date(elapsed));
    }

    /**
     * @param yyyymmddhhmmss ex. "20070101120500" , "20530101120500"
     * @return Calendar
     * @example Calendar c =
     * DateUtils.convertStringDateTimeToCalendar("20070101120500");
     */
    public static Calendar toCalendar(String yyyymmddhhmmss) {
        int year = Integer.parseInt(yyyymmddhhmmss.substring(0, 4));
        if (year > 2400) {
            year = year - 543;
        }
        int month = Integer.parseInt(yyyymmddhhmmss.substring(4, 6)) - 1;
        int day = Integer.parseInt(yyyymmddhhmmss.substring(6, 8));
        int hh = Integer.parseInt(yyyymmddhhmmss.substring(8, 10));
        int mm = Integer.parseInt(yyyymmddhhmmss.substring(10, 12));
        int ss = Integer.parseInt(yyyymmddhhmmss.substring(12, 14));
        Calendar date = Calendar.getInstance(Locale.US);
        date.set(year, month, day, hh, mm, ss);
        return date;
    }
}

Related

  1. getTimeDiffMin(String format, String startDate, String endDate)
  2. isDateDiff_2(String pattern, String time2, String time1)
  3. isLowerThanMinTimeDiff(Date lastUpdate, Date now, Long minTimeDiff)
  4. printTimeDiff(Long oldDate, Long newDate, String message)
  5. printTimeDiffNano(String message, long start)
  6. timeDiff(Date salida, Date llegada)