创建一个countDown计时器来显示2个日期之间的差异

时间:2014-04-29 08:24:37

标签: android datetime countdown

我有一个Android applivation,显示2个日期之间的差异。  活动显示固定时间而不是倒计时。

所以我想以 countDown计时器的形式显示2日期之间的差异 包括日,小时,分钟,秒

任何人都可以帮我改变我的代码以获得countDownTimer吗?

FirstActivity.java

package com.devleb.expandablelistdemo3;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class FirstActivity extends Activity {

    // private DatabaseHelper dbHelper = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        getDiffDate();


    }

    public void listTeams(View v) {
        Intent in = new Intent(getBaseContext(), MainActivity.class);
        startActivity(in);
    }

    public void listGroups(View v) {
        Intent in = new Intent(getBaseContext(), GroupList.class);
        startActivity(in);
    }

    public void DisplaySched(View v) {

        Intent in = new Intent(getBaseContext(), MatchScheduleList.class);
        startActivity(in);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first, menu);
        return true;
    }

    public void getDiffDate() {

        String dateStop = "06/12/2014 23:00:00";

        // HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        Date d1 = new Date();

        Date d2 = null;

        try {

            d2 = format.parse(dateStop);

            // in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.print(diffDays + " days, ");
            System.out.print(diffHours + " hours, ");
            System.out.print(diffMinutes + " minutes, ");
            System.out.print(diffSeconds + " seconds.");

            TextView txtDate = (TextView) findViewById(R.id.txtDifDate);
            String resultDate = diffDays + "days " + diffHours + "Hours "
                    + diffMinutes + "Min " + diffSeconds + "S";
            txtDate.setText(resultDate);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

0 个答案:

没有答案