结果显示
![时间显示](https://i-blog.csdnimg.cn/blog_migrate/ab33d94ee136a0d188ca279309b7ce3e.jpeg)
设置时区
String timeZone = "GMT+08:00";
mHourMinuteTC.setTimeZone(timeZone);
mYearMonthDayWeekTC.setTimeZone(timeZone);
设置显示格式
mHourMinuteTC.setFormat24Hour("HH:mm");
mYearMonthDayWeekTC.setFormat24Hour("yyyy年MM月dd日 EEEE");
代码
src\main\res\layout-v17\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextClock
android:id="@+id/tc_h_m"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:format24Hour="HH:mm"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="50sp"/>
<TextClock
android:id="@+id/tc_y_m_d_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:format24Hour="yyyy年MM月dd日 EEEE"
android:textColor="@color/black"
android:textSize="30sp"/>
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextClock mHourMinuteTC;
private TextClock mYearMonthDayWeekTC;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setTime();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private void initView(){
mHourMinuteTC = (TextClock) findViewById(R.id.tc_h_m);
mYearMonthDayWeekTC = (TextClock) findViewById(R.id.tc_y_m_d_w);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private void setTime(){
String timeZone = "GMT+08:00";
mHourMinuteTC.setTimeZone(timeZone);
mHourMinuteTC.setFormat24Hour("HH:mm");
mYearMonthDayWeekTC.setTimeZone(timeZone);
mYearMonthDayWeekTC.setFormat24Hour("yyyy年MM月dd日 EEEE");
}
}