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);
        }
    }

}

【3】效果显示

初始页面:

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

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

猜你喜欢

转载自blog.csdn.net/qq_38110571/article/details/81188347