title: How to get system time using setText in Android?
body: <p>I am new to Android development. Kindly help me out with this code.</p>
  
  <p>I want to access the system time in my counter, i.e. in <code>setText</code>. As for now I have used some random date and time but I want that to be changed to current date and time.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>package countdowntimer.android;
  
  
  import java.util.Date;
  import java.text.NumberFormat;
  import java.text.SimpleDateFormat;
  import java.util.concurrent.TimeUnit;
  import android.os.CountDownTimer;
  import android.os.SystemClock;
  import android.app.Activity;
  import android.net.ParseException;
  import android.os.Bundle;
  import android.provider.Settings.System;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  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);
  
              start.setText("2011-09-08-00:00:00");
  
  
              Button btnStart=(Button)findViewById(R.id.btnstart);
          btnStart.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View arg0) {
          try {
  
          try {
              endDate = outputFormat.parse(end.getText().toString());
              startDate=outputFormat.parse(start.getText().toString());
  
  
          } catch (java.text.ParseException e) {
              // TODO Auto-generated catch block
              /*Toast.makeText(getBaseContext(), ex.getMessage()
                       ,Toast.LENGTH_SHORT).show();*/
          }
  
  
          long diffInMis= endDate.getTime() - startDate.getTime();
          if(diffInMis&lt;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&gt;-1)
          min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
          else{
          iMin=59;
          min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
          iHour-=1;
          if(iHour&gt;-1)
          hour.setText(String.valueOf(myFormat.format(iHour)).toString());
          else{
          iHour=11;
          hour.setText(String.valueOf(myFormat.format(iHour)).toString());
          iDay-=1;
          if(iDay&gt;-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)));
          }}}
  </code></pre>