Outcome display
Layout
The layout theme background color adopts #000000 (pure black), and the font color of each TextView adopts #FFFFFF (pure white).
Use five TextViews to implement hours: minutes, seconds, am, pm, Sunday, and specific dates.
Default layout
Constraint relationship
For the layout of several TextViews, we rely on a very practical method: Constraint(ConstraintLayout).
-
The default layout, relying on constraints to determine the position
-
Ability to flexibly position and resize interface elements
-
Without any nesting, reduce layout levels and optimize rendering performance
-
Can completely replace other layouts
Through the constraint relationship, component layout will become extremely easy~
Function realization
Use Timer + TimerTask to complete the digital clock, first create several Timer and TimerTask objects.
Timer htimer = new Timer() //Create an hour-minute timer TimerTask hourtask = new TimerTask(){//Create hour-division timing task--sub-thread public void run(){ ... } };
We use five TextViews in the layout, and we need to use the setText() function to implement the clock, so we need to create TextView objects in TimerTask.
TextView tv1 = (TextView) findViewById(R.id.hourshow);
And use findViewById to bind components based on id value.
How to get real-time time data? There are two nice date APIs for this.
Calendar class
First, we use the getInstance() function that comes with Calendar to get the instance.
//Get an instance of the Calendar class: private Calendar c = Calendar. getInstance();
Then we can use the get() function to obtain the data we want through the instance c, for example, to obtain the time and division here.
int hour = c. get(Calendar. HOUR_OF_DAY); int minute = c. get(Calendar. MINUTE);
Then the time has been captured by hour and minute, we only need to pass the time to TextView.
tv1.setText(String.format("d: d",hour,minute));
Note that the data obtained from the Calendar is all int type, and we need to convert it to String type before transmission. Similar to time division, several other data can be completed separately.
But there is an even better API.
SimpleDateFormat class
SimpleDateFormat is a class in Java for formatting dates and times. It provides a way to format dates and times in a specific style, making them better for interacting with a user interface or storing in a database.
We first create a SimpleDateFormat object dateFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("HH : mm", Locale.CHINA);
“HH : mm”: This parameter is a string, where HH and mm are parameters that come with the API, HH means to display two-digit hours, mm means to display two-digit minutes;
Locale.CHINA: This parameter is a Locale object, which specifies the formatted locale. Here we choose CHINA, which is the Chinese environment.
Next, we format the current time as a string in am-pm time format using the following statement:
String currentTime = dateFormat. format(new Date());
In Java, you can use new Date()
to create a Date
object representing the current time. When this statement is executed, it will call the system clock to get the current time and encapsulate it as a Date
object.
Here, we use the format() method of SimpleDateFormat to format a Date object (representing the current time) into a string. This method returns a String object containing a date and time string in the specified format.
Finally, the function can be realized by using the setText function through TextView, so it will not be demonstrated again.
The following are details corresponding to some parameters of the SimpleDateFormat class:
Letter | Date/Time Element | Indicates | Example |
---|---|---|---|
G | Era | Identifier | AD |
y | Year | Year | 1996; 96 |
M | Month of the year | Month | July; Jul;07 |
w | Week of the year Number | Number | 27 |
W | Number of weeks in the month | Number | 2 |
D | Number of days in the year | Number | 189 |
d | Number of days in the month | Number | 10 |
F | week of the month | Number | 3 |
E | Day of the week | Text | Tuesday; Tue |
a | Am/pm marker | Text | PM |
H | Hour of the day | Number (0-23) | 0 |
k | hour of the day | Number (1-24) | 24 |
K | hour in am/pm | Number (0-11) | 0 |
h | hour in am/pm | Number (1-12) | 12 |
m | minute of the hour | Number | 30 |
s | seconds in minutes | Number | 55 |
S | Milliseconds | Number | 978 |
z | Time Zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |