Home » Android » Showing current time in Android and updating it?

Showing current time in Android and updating it?

Posted by: admin June 15, 2020 Leave a comment


I want to display current time and keep it updating, in this format example: 09:41 Hour:Minute. Is it possible to do in TextView? I tried some ways but I’m not getting what I actually want.

How to&Answers:

Something like this should do the trick

final Handler someHandler = new Handler(getMainLooper());   
        someHandler.postDelayed(new Runnable() {
            public void run() {
                tvClock.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));
                someHandler.postDelayed(this, 1000);
        }, 10);

You should keep a reference to the handler and the runnable to cancel this when the Activity goes to pause and resume when it resumes. Make sure you remove all callbacks to handler and set it to null in onDestroy


Android has a view for this already.


You can use it directly in XML like so


It is min api 17, so you if need to go lower than that just look into


Worst case scenario you can subclass textview and steal the code from the textclock source. it should be fairly straightforward


  boolean run=true; //set it to false if you want to stop the timer
  Handler mHandler = new Handler();

 public void timer() {
        new Thread(new Runnable() {
            public void run() {
                while (run) {
                    try {
                        mHandler.post(new Runnable() {

                            public void run() {
                                Calendar c = Calendar.getInstance();
                                int min = c.get(Calendar.MINUTE);
                                int hour=c.get(Calendar.HOUR);
                    } catch (Exception e) {

in the onCreate call it like this



Why not use Java’s Timer and TimerTask? They’re very easy, reliable and simple to understand.

Here is an example:

public class ClockCounter extends TimerTask{

    private long time = System.currentTimeMillis();

    public void run(){
        time += 1000; //add 1 second to the time
        //convert ms time to viewable time and set MainActivity.text (textview) text to this.

    public long getTime(){ return time; }

public class MainActiviy extends Activity{

    public static TextView text;
    private Timer timer;

    public void onCreate(){
        //default code
        //set text view according to id
        timer = new Timer();
        timer.schedule(new ClockCounter(), 0, 1000); //schedule clock counter to repeat every 1 seconds (1000 ms)

I know there are some notes of what to do in there but any android developer with reasonable experience in Java should be able to figure out how to do those things.

I hope this helped!

To format the date to a viewable string, you can use the following code (also using only Java’s shipped APIs)

Date date = new Date();
DateFormat formatter = new SimpleDateFormat("HH:mm"); //Hourse:Minutes
String dateFormatted = formatter.format(date); //string representation


For getting time and date you can use Calender class

Calendar cal = Calendar.getInstance();
String time = ""+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);