-2

I want to show countdown to a particular time in my android application by using the systems current. thanks in advance. I need to set a countdown timer that should tell how much time is left to "exact time " which is 12:30 in this code.

// start time
        String string13 = date + " 06:30:00";  
        Date time13 = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string13);
        Calendar calendar13 = Calendar.getInstance();
        calendar13.setTime(time13);

        // end time
        String string14 = date + " 13:30:00";  
        Date time14 = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string14);
        Calendar calendar14 = Calendar.getInstance();
        calendar14.setTime(time14);

        // exact time
        String string15 = date + " 12:30:00";
        Date Z = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string15);
        Calendar c15 = Calendar.getInstance();
        c15.setTime(Z); 
user3584316
  • 61
  • 10
  • 1
    Have you wrote any code ? – Lucifer May 05 '14 at 05:27
  • 1
    What have you done so far? What problem are you facing? – Apoorv May 05 '14 at 05:28
  • please share your code. We will help where u go wrong. – Tushar Narang May 05 '14 at 05:33
  • I want to get the current system time and compare with the two different timing example start time as 2:00:00AM and end as 6:00:00AM. if my current system time falls between these i have to execute my rest of the code and show the output in textview. I have down in this way and it is giving me desired output. now I want to set a countdown that will calculate the time left to 12:30. – user3584316 May 05 '14 at 05:37
  • String string13 = date + " 06:30:00"; Date time13 = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string13); Calendar calendar13 = Calendar.getInstance(); calendar13.setTime(time13); String string14 = date + " 13:30:00"; Date time14 = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string14); Calendar calendar14 = Calendar.getInstance(); calendar14.setTime(time14); String string15 = date + " 12:30:00"; Date Z = new SimpleDateFormat("MM-dd-yy HH:mm:ss").parse(string15); Calendar c15 = Calendar.getInstance(); c15.setTime(Z); – user3584316 May 05 '14 at 05:42
  • actually I am unable to attach my code it is giving me error of incorrect format. – user3584316 May 05 '14 at 05:54
  • http://stackoverflow.com/questions/23358974/compare-two-time-values-in-java/23359054?noredirect=1#comment35821019_23359054 you can check the code format from here – user3584316 May 05 '14 at 05:59

1 Answers1

0

Use below code to your goal.

startB = (Button) this.findViewById(R.id.button1);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) findViewById(R.id.textView1);


@Override
public void onClick(View v) {
    new CountDownTimer(30000, 1000) { // adjust the milli seconds here

        public void onTick(long millisUntilFinished) {
        text.setText(""+String.format("%d min, %d sec", 
                    TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                          TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
    }

    public void onFinish() {
       text.setText("done!");
    }
 }.start();

}

OR try below link
http://steve.odyfamily.com/?p=12

Mahdi-bagvand
  • 1,396
  • 19
  • 40
  • It is giving me minutes larger than 100 for e.g 200 mins. how to convert them in proper way and show in the textview. I have written these lines – user3584316 May 06 '14 at 09:46
  • TimeUnit.MILLISECONDS.toHours(millisUntilFinished), TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished) - TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours( millisUntilFinished)), TimeUnit.MILLISECONDS.toSeconds( millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished)))); – user3584316 May 06 '14 at 09:48