Android studio 显示手机当前时间

因为要做一个时间判断,所以查了下怎么显示当前时间

最简单的是SimpleDateFormat这个它自带的,还可以自定义格式,在使用它的时间要记得引用头文件

import java.text.SimpleDateFormat;

这是核心语句(str就是根据你设置的格式显示出来的时间):

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
//获取当前时间
String str = formatter.format(curDate);

做了一个简单的就是一个textview,一个button,点击按钮以后,时间显示在textview中。

【1】主界面XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context="com.example.administrator.test.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="250dp"
        android:layout_height="47dp"
        android:autoLink="phone"
        android:gravity="center"
        android:hint="time"
        android:textColor="#F87461"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.313"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.295" />


    <Button
        android:id="@+id/button_send"
        android:layout_width="143dp"
        android:layout_height="48dp"
        android:text="send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.41"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.457" />

</android.support.constraint.ConstraintLayout>

【2】他的activity(头文件不贴了,把最开始提到的加上,其他的根据自身代码需要了):

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

    private void getId() {
        Button button_send = findViewById(R.id.button_send);
        button_send.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_send:
                TextView tv = findViewById(R.id.tv);
                @SuppressLint("SimpleDateFormat")
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                Date curDate = new Date(System.currentTimeMillis());
                //获取当前时间
                String str = formatter.format(curDate);
                tv.setText(str);
                break;
            default:
                break;
        }
    }
}

【3】效果显示

初始页面:

点击按钮后(这个send我忘了改....):

可以看到和右上方的时间是一致的

  • 8
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值