你的位置:首页 > 操作系统

[操作系统]android 获得系统时间


<RelativeLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <!-- 显示网络状态的标签控件 -->  <TextView    android:id="@+id/myyl"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:textSize="20dp" /></RelativeLayout>

package com.example.yanlei.yl;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.format.DateFormat;import android.widget.TextView;import java.text.SimpleDateFormat;import java.util.Calendar;public class MainActivity extends AppCompatActivity {  private TextView pTextView;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    pTextView = (TextView) findViewById(R.id.myyl);    SimpleDateFormat sDateFormat;    String date;    if (DateFormat.is24HourFormat(getApplicationContext())) {      sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      date = sDateFormat.format(new java.util.Date());    } else {      sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");      date = sDateFormat.format(new java.util.Date()) + getPeroidText();    }    pTextView.setText("时间:" + date);  }  private CharSequence getPeroidText() {    String am_pm = Calendar.getInstance().getTime().getHours() >= 12 ? "PM" : "AM";    return am_pm;  }}