Timer + TimerTask implements digital clock

Task:

Done by myself:

Problem encountered:

14:50 it shows as 06:50, the time is displayed wrong, this is because the time zone is different.

After 8.0, the time zone is set, and there are some changes. You can’t directly set the time zone such as GMT + 8:00. You need to set the id of the time zone.

The id should be set to, for example, Beijing time: Asia/Shanghai, which is the time zone id of Beijing time

The problem that the calendar setting time zone in Android Studio is invalid can be solved by changing it to the following form:

Calendar cal=Calendar.getInstance();//Can get the current time
//Set the system time zone
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf. format(cal. getTime());

ActionBar:

s

ActionBar actionBar = getSupportActionBar(); //Get ActionBar
        actionBar.setTitle("TimerDemo01"); //Set the title

part1: Display HH:mm

public static String getDate1(){
        Calendar cal=Calendar.getInstance();//Can get the current time
        //Set the system time zone
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        /*SimpleDateFormat:
        A locale-dependent utility class for formatting and parsing dates.
         *Use this class to convert date to text, or convert text to date*/
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf. format(cal. getTime());
    }

part2AM/PM

public static String getDate2(){
        Calendar cal = Calendar. getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        Integer dateValue = Integer.valueOf(String.valueOf(cal.getTime()).substring(11,13));//Get the hour
        return dateValue>=12?"PM":"AM";
    }

part3: Display seconds

public static String getDate3(){
        Calendar cal = Calendar. getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("ss");
        return sdf. format(cal. getTime());
    }

part4: Display the day of the week

 public static String getDate4(){
        String[] weeks = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" "};
        Calendar cal = Calendar. getInstance();
        /* Get the current week Week returns 0-6;
        Represent Sunday-Saturday (so -1)*/
        int week_index = cal. get(Calendar. DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }

part5: Display year/month/day

public static String getDate5(){
        Calendar cal = Calendar. getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf. format(cal. getTime());
    }

Total code: TimerDemoActivity01

package com.example.helloandroid2;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;

public class TimerDemoActivity01 extends AppCompatActivity {

    private Timer timer;
    private TimerTask task;
    private TextView tv1,tv2,tv3,tv4,tv5;
    public static String getDate1(){
        Calendar cal=Calendar.getInstance();//Can get the current time
        //Set the system time zone
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        /*SimpleDateFormat:
        A locale-dependent utility class for formatting and parsing dates.
         *Use this class to convert date to text, or convert text to date*/
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf. format(cal. getTime());
    }
    public static String getDate2(){
        Calendar cal = Calendar. getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        Integer dateValue = Integer.valueOf(String.valueOf(cal.getTime()).substring(11,13));//Get the hour
        return dateValue>=12?"PM":"AM";
    }
    public static String getDate3(){
        Calendar cal = Calendar. getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("ss");
        return sdf. format(cal. getTime());
    }
    public static String getDate4(){
        String[] weeks = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" "};
        Calendar cal = Calendar. getInstance();
        /* Get the current week Week returns 0-6;
        Represent Sunday-Saturday (so -1)*/
        int week_index = cal. get(Calendar. DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }
    public static String getDate5(){
        Calendar cal = Calendar. getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf. format(cal. getTime());
    }

    public void init() {
        timer = new Timer();

        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);
        task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() { //Start another runOnUiThread thread in the scheduled task thread to update the UI
                    @Override
                    public void run() {
                        tv1.setText(getDate1());
                        tv2.setText(getDate2());
                        tv3.setText(getDate3());
                        tv4.setText(getDate4());
                        tv5.setText(getDate5());
                    }

                });//end runOnUiThread
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_demo01);

        init();

        timer.schedule(task,0,1000);

        ActionBar actionBar = getSupportActionBar(); //Get ActionBar
        actionBar.setTitle("TimerDemo01"); //Set the title
    }
}

activity_timer_domo01.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    tools:context=".TimerDemoActivity01">

    <TextView
        android:id="@ + id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20:19"
        android:textColor="@color/white"
        android:textSize="96sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.245"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.399" />

    <TextView
        android:id="@ + id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="27dp"
        android:layout_marginTop="248dp"
        android:layout_marginEnd="52dp"
        android:text="PM"
        android:textColor="@color/white"
        android:textSize="34sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@ + id/textView1"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ + id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="49"
        android:textColor="@color/white"
        android:textSize="48sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.306"
        app:layout_constraintStart_toEndOf="@ + id/textView1"
        app:layout_constraintTop_toBottomOf="@ + id/textView2" />

    <TextView
        android:id="@ + id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="76dp"
        android:text="Week 5"
        android:textColor="@color/white"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ + id/textView1"
        app:layout_constraintVertical_bias="0.027" />

    <TextView
        android:id="@ + id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2023/3/7"
        android:textColor="@color/white"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.631"
        app:layout_constraintStart_toEndOf="@ + id/textView4"
        app:layout_constraintTop_toBottomOf="@ + id/textView1"
        app:layout_constraintVertical_bias="0.027" />

</androidx.constraintlayout.widget.ConstraintLayout>