安卓开发实例:日期时间

实时显示本地日期时间
在这里插入图片描述

activity_date_time.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".DateTime">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#FFEBEE">
    <TextView
      android:text="显示当前日期时间"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:id="@+id/tv2"
    />
    <TextView
      android:text="TIME"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="30sp"
      android:id="@+id/tv_time"
      tools:ignore="HardcodedText,MissingConstraints"
    />
  </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

DateTime.java

package com.weijun901.show;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateTime extends AppCompatActivity {
  TextView txt_time;
  Handler handler;

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

    // 设置标题栏的文字
    getSupportActionBar().setTitle("日期时间");

    // 1.设置应用的默认时区
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); // 设置时区为亚洲北京
    // 2.设置应用的默认语言环境(可选)
    Locale.setDefault(Locale.CHINA); // 设置为中国
    // 3.显示日期和时间
    txt_time = findViewById(R.id.tv_time);
    // 4.创建一个 Handler 用于在主线程中更新时间
    handler = new Handler(Looper.getMainLooper());
    // 5.启动一个定时任务以每秒更新时间
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        updateCurrentTime();
        handler.postDelayed(this, 1000); // 每秒刷新一次
      }
    }, 1000); // 初始延迟1秒后开始运行
  }
  private void updateCurrentTime() {
    Date dt = new Date();
    // 6.获取当前时区
    TimeZone timeZone = TimeZone.getDefault();
    // 7.使用时区来格式化时间
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    dateFormat.setTimeZone(timeZone);
    String str_time = dateFormat.format(dt);
    txt_time.setText(str_time);
  }
  @Override
  protected void onDestroy() {
    super.onDestroy();
    // 8.移除定时任务以防止内存泄漏
    handler.removeCallbacksAndMessages(null);
  }

  public void toDateTime(View view) {
    Intent intent = new Intent(this, DateTime.class); // 替换为目标页面的类名
    startActivity(intent);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值