Skip to content Skip to sidebar Skip to footer

Android How To Make Calender Like Countdown Clock?

I am using countdowntimer in my application,in which I am showing date in simple dateformat but my countdowntimer is not showing , simple clock is working in that, I want to show m

Solution 1:

package com.example.diffrence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.test);


        Timer notificationtask = new Timer();

        notificationtask.schedule(new TimerTask() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        SimpleDateFormat simpleDateFormat = 
                                new SimpleDateFormat("dd/M/yyyy hh:mm:ss");
                     try {
                        Date date1 = simpleDateFormat.parse("10/10/2014 11:30:10");

                        String currentDateandTime = simpleDateFormat.format(new Date());
                        Date date2 = simpleDateFormat.parse(currentDateandTime);
                        txt.setText("time Remainig to date 10/10/2014 11:30:10    : " + printDifference(date1,date2));

                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    }
                });
            }
        }, 1000, 1000);




    }

     public String printDifference(Date startDate, Date endDate){

            //milliseconds
            long different = endDate.getTime() - startDate.getTime();

            System.out.println("startDate : " + startDate);
            System.out.println("endDate : "+ endDate);
            System.out.println("different : " + different);

            long secondsInMilli = 1000;
            long minutesInMilli = secondsInMilli * 60;
            long hoursInMilli = minutesInMilli * 60;
            long daysInMilli = hoursInMilli * 24;

            long elapsedDays = different / daysInMilli;
            different = different % daysInMilli;

            long elapsedHours = different / hoursInMilli;
            different = different % hoursInMilli;

            long elapsedMinutes = different / minutesInMilli;
            different = different % minutesInMilli;

            long elapsedSeconds = different / secondsInMilli;


            return  Math.abs(elapsedDays) + " days     " +
                    Math.abs(elapsedHours)+ " hours   " +
                            Math.abs(elapsedMinutes)+ " minute   " +
                                    Math.abs(elapsedSeconds) + " Seconds  ";

        }






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

}

Post a Comment for "Android How To Make Calender Like Countdown Clock?"