get Date Between Time - Android java.util

Android examples for java.util:Time

Description

get Date Between Time

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.StringTokenizer;

import android.util.Log;

public class Main {
    public static long getDateBetweenTime(Date fromDate, Date toDate) {
        long result = 0;

        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

        String from_date = format.format(fromDate);
        String to_date = format.format(toDate);

        double fromVal = 0;
        double toVal = 0;

        StringTokenizer fromString = new StringTokenizer(from_date, ":");
        StringTokenizer toString = new StringTokenizer(to_date, ":");

        if (fromString.hasMoreTokens()) {
            fromVal += Integer.parseInt(fromString.nextToken()) * 60 * 60;
            toVal += Integer.parseInt(toString.nextToken()) * 60 * 60;
        }/*from  w w w  .  j  a v  a 2s  .  c om*/

        if (fromString.hasMoreTokens()) {
            fromVal += Integer.parseInt(fromString.nextToken()) * 60;
            toVal += Integer.parseInt(toString.nextToken()) * 60;
        }

        if (fromString.hasMoreTokens()) {
            fromVal += Integer.parseInt(fromString.nextToken());
            toVal += Integer.parseInt(toString.nextToken());
        }

        Log.d("glotrek", "from : " + fromVal);
        Log.d("glotrek", "toVal : " + toVal);

        if (fromVal > toVal) {
            result = (long) (fromVal - toVal);
        } else {
            result = (long) (toVal - fromVal);
        }

        Log.d("glotrek", "result : " + result);

        return result;
    }
}

Related Tutorials