Calculate the remaining time to date d - Android java.util

Android examples for java.util:Time

Description

Calculate the remaining time to date d

Demo Code


//package com.java2s;
import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static final int TEST_SECONDS = 1000 * 5;

    /**/*  w w w. ja v  a  2s . c  o  m*/
     * Calculate the remaining time to date d
     * @param d Date in "yyyy/MM/dd HH:mm:ss" format
     * @return remaining time in milliseconds
     */
    public static long remainTime(String d) {
        try {
            Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
                    .parse(d);
            long remaining = date.getTime() - System.currentTimeMillis();
            Log.d("remain", remaining + " ");
            return TEST_SECONDS;
            //  return remaining;
        } catch (ParseException e) {
            e.printStackTrace();
            return TEST_SECONDS;
        }
    }
}

Related Tutorials