-1

i have a textview having a specific date time string as this format dd/MM/yyyy HH:mm:ss. I need to create a thread that increment number of second in the textview every one second so its update : minutes, day, month year.... how to create this handler ?

so the text view will updated every 1 sec according to first time displayed in the text view example if the textview display : 06/03/2017 01:20:00 it will be updated as below every second
06/03/2017 01:20:01
06/03/2017 01:20:02
06/03/2017 01:20:03
06/03/2017 01:20:04
06/03/2017 01:20:05
06/03/2017 01:20:06
.
.
.
.
.
06/03/2017 01:21:00

Amalo
  • 772
  • 3
  • 13
  • 32

3 Answers3

2

You can achieve from

int timeinminutes=1;

timer = new CountDownTimer(timeinminutes*21000, 1000) 
{

    TextView jeutimer = (TextView) findViewById(R.id.jeu_timer);

     public void onTick(long millisUntilFinished) 
     {
         long scnds=0;
         scnds=(millisUntilFinished/1000);
         jeutimer.setText( "" + scnds);        // Add your date here..
     }


     public void onFinish() 
     {

     }

}.start();

Now you will be able to restart it whenever you want with:

timer.start();

And stop it with:

timer.cancel();

Edited You can also use this ..

txtView.setText(parseDate("06/03/2017 01:20:00","dd/MM/yyyy hh:mm:ss","dd/MM/yyyy hh:mm:ss"));

public static String parseDate(String Date, String CurrentPattern, String OutputPattern) {

    SimpleDateFormat sdf = new SimpleDateFormat(CurrentPattern, Locale.getDefault());

    try {
        Date startDate = increaseDate(sdf.parse(Date));
        sdf.applyPattern(OutputPattern);
        return sdf.format(startDate);
    } catch (Exception e) {
        return "";
    }
}

public static Date increaseDate(Date origDate) {

    final Calendar calendar = Calendar.getInstance();

    calendar.setTime(origDate);
    calendar.add(Calendar.SECONDS, +1);
    Date newDate = calendar.getTime();
    return newDate;
}
Chirag.T
  • 746
  • 1
  • 6
  • 18
  • i want to start from the date displayed in the text view, i have this date returned by a web service – Amalo Mar 06 '17 at 11:34
  • at first launch, the text view must display this date :06/03/2017 01:20:00. so i want to start increment seconds starting from this date – Amalo Mar 06 '17 at 11:37
  • ohhk then first time you can set as usual and second time you want to increase from previous date.. right..? – Chirag.T Mar 06 '17 at 11:40
  • if you achieve your goal then please tick to right my answer.. @Amalo – Chirag.T Mar 07 '17 at 05:50
0

TextClock added in API Level 17.

Daniel Stancu
  • 52
  • 2
  • 8
0

you can use handler for this, have a look at this chronometer source

Farid
  • 1,024
  • 9
  • 16