package com.example.date; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.ThemedSpinnerAdapter; import android.util.Log; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; /** * Created by admin on 2016/7/20. */ public class Test extends AppCompatActivity { private static final int SUCCESSFUI = 1; private TextView data; private TextView time1; private Runnable runnable; private Handler handler= new Handler() { @Override public void handleMessage(Message msg) { Log.d("=====更新时间",""+msg.obj.toString().length()); String time = msg.obj.toString(); String data1 = time.substring(0, 10); data.setText(data1); String data2 =time.substring(11,time.length()); time1.setText(data2); super.handleMessage(msg); } }; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); data = (TextView) findViewById(R.id.tv_data); time1 = (TextView) findViewById(R.id.tv_time); sendMessageToHandler(); } /** * =======获取当前系统时间======== */ private String getSystemTime() { Date mDate = new Date(System.currentTimeMillis()); final SimpleDateFormat mFormat = new SimpleDateFormat( "yyyy/MM/dd/HH:mm:ss"); mFormat.setTimeZone(TimeZone.getDefault()); String str = mFormat.format(mDate); Log.d("============", "" + str); return str; } /** * =======发送时间消息更新界面======== */ public void sendMessageToHandler() { runnable = new Runnable() { @Override public void run() { Message message = Message.obtain(); message.what = SUCCESSFUI; message.obj = getSystemTime(); handler.sendMessage(message); handler.postDelayed(this,1000); } }; handler.postDelayed(runnable, 1000); } }
android系统时间循环更新
最新推荐文章于 2023-02-01 16:46:45 发布