This tutorial will show you how to get the difference between two dates as shown in figure. I used CountDownTimer class and made it’s object. With help of this object, we can set up timer. This service counts down to any date you choose, it is often used as a graduation countdown, retirement countdown or birthday countdown. By entering a date in the past, it can also count up, for example to see how long you have lived. I hope this tutorial will help you and if you have time then please write comment.
First of all, create New Android Project and open main activity, in this I have named mian activity as Timer. Initialize some global variable of our class as shown below.
Object of TextView like day, hour, min and sec are associated with number of days, hours, minutes and seconds left, respectively. Now, come to variables of integer like iDay, iHour, iMin and iSec, these are also associated with number of days, hours, minutes and seconds left, respectively. Make sure that you’ll not confuse between the object of TextView and it’s associated integers. Counter is object of class MyCount which extends CountDownTimer. There are another two Date objects startDate and endDate that corresponds to starting and ending date & time. Values of starting and ending date are come from the object of EditText start & end. I have set the Date format as shown below, you can change it into another format as you like.
final SimpleDateFormat outputFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Here, is a whole data of Timer.java activity.
{code}
public class MActivity extends Activity {
TextView day,hour,min,sec;
int iDay,iHour,iMin,iSec;
MyCount counter;
Date endDate = null;
Date startDate = null;
NumberFormat myFormat = NumberFormat.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
day=(TextView)findViewById(R.id.day);
hour=(TextView)findViewById(R.id.hour);
min=(TextView)findViewById(R.id.min);
sec=(TextView)findViewById(R.id.sec);
myFormat.setMinimumIntegerDigits(2);
final EditText end=(EditText)findViewById(R.id.end);
final EditText start=(EditText)findViewById(R.id.start);
Button btnStart=(Button)findViewById(R.id.btnstart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
endDate = outputFormat.parse(end.getText().toString());
startDate=outputFormat.parse(start.getText().toString());
long diffInMis= endDate.getTime() - startDate.getTime();
if(diffInMis<0){
Toast.makeText(getBaseContext(), "Please, Enter valid Time..."
,Toast.LENGTH_SHORT).show();
}
else{
long diff = TimeUnit.MILLISECONDS.toSeconds(diffInMis);
iDay=(int) (diff/(60*60*24));
long lday= (diff%(60*60*24));
iHour=(int)(lday/3600);
long lhour= (lday%(60*60));
iMin=(int)(lhour/60);
long lmin= (lhour%(60));
iSec=(int)(lmin);
day.setText(String.valueOf(iDay).toString()+" Day ");
hour.setText(String.valueOf(myFormat.format(iHour)).toString());
min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
sec.setText(":"+String.valueOf(myFormat.format(iSec)).toString());
counter = new MyCount(iSec*1000,1000);
counter.start();}
} catch (ParseException e) {
e.printStackTrace();
} }; });
Button btnStop=(Button)findViewById(R.id.btnstop);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
counter.cancel();
} }); }
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter = new MyCount(60000,1000);
counter.start();
iMin-=1;
if(iMin>-1)
min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
else{
iMin=59;
min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
iHour-=1;
if(iHour>-1)
hour.setText(String.valueOf(myFormat.format(iHour)).toString());
else{
iHour=11;
hour.setText(String.valueOf(myFormat.format(iHour)).toString());
iDay-=1;
if(iDay>-1)
day.setText(" "+String.valueOf(iDay).toString());
else{
day.setText("Time is over");
hour.setText("");
min.setText("");
sec.setText("");
counter.cancel();
}}}}
@Override
public void onTick(long millisUntilFinished) {
sec.setText(":"+String.valueOf(myFormat.format(millisUntilFinished/1000)));
}}}
For layout, main.xml would be like following to match the design as shown in figure.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="30dp"> <TextView android:id="@+id/day" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Day " android:textSize="36sp" /> <TextView android:id="@+id/hour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00" android:textSize="36sp" /> <TextView android:id="@+id/min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=":00" android:textSize="36sp" /> <TextView android:id="@+id/sec" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=":00" android:textSize="36sp" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter End Time : " /> <EditText android:id="@+id/end" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Start Time : " /> <EditText android:id="@+id/start" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="40dp" > <Button android:id="@+id/btnstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Counter" /> <Button android:id="@+id/btnstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" /> </LinearLayout> </LinearLayout>
Following are the links of other Post on Android.
Android : ContextMenu & SubMenu
Android : Connecting to MySQL using PHP
Android : Sliding Drawer
Android Countdown Timer
Getting started with Android Map View: Displaying a location by Marker
Simple Progress Bar in Android using Thread
Manage & Developed by Sptechnolab.com

2 comments
Viren Gujariya
August 31, 2011 at 3:36 pm (UTC 5.5) Link to this comment
So far i had seen excellent tutorials on this blog. it is really helpful for beginners. I would like to suggest that please post source code in proper format as this one is cumbersome and creates confusion.
[Translate]
shanmugham
October 16, 2011 at 5:12 am (UTC 5.5) Link to this comment
error in countdown clock …..
can i get this android application …
plz sent to kshan1988@gmail.com…
replay soon….
[Translate]