Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:11:44+00:00 2026-05-25T14:11:44+00:00

I am new to Android development. Kindly help me out with this code. I

  • 0

I am new to Android development. Kindly help me out with this code.

I want to access the system time in my counter, i.e. in setText. As for now I have used some random date and time but I want that to be changed to current date and time.

Here is the 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<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)));
        }}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:11:44+00:00Added an answer on May 25, 2026 at 2:11 pm

    You can access the system time with

    Calendar.getInstance();
    int hh = cal.get(Calendar.HOUR_OF_DAY);//or
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    String aux[] = sdf.format(cal.getTime()).split("/");//process the array from here
    

    Note that you must

    import java.util.Calendar;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to android development and needs help here.I want to create a layout
I'm brand new to Android development and right now I am building a simple
I'm new in android development and I am trying out the WebView example in
im new to android development so please forgive me if this is an easy
Hi I am new into android development and I am having trouble into this
I a new to Android development, so this is kind of a basic question.
I'm new to Android development and it's been a long time since I've done
I'm very new to Android development and can't find the answer to this. I
I am new to Android development. I am trying to use the following code
I am very new to Android development, so I appologise in advance if this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.