Android获得当前系统时间有好几种方法,本文使用的是System.currentTimeMillis->Date+SimpleDateFormat,我认为这样的方式来展示时间很的灵活。
运行结果图:
源代码:
package com.example.shen.systemtime; import android.app.Activity; import android.os.Bundle; import android.util.Log; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); long time=System.currentTimeMillis(); Date date=new Date(time); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EEEE"); Log.e("time","time1="+format.format(date)); format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Log.e("time","time2="+format.format(date)); format=new SimpleDateFormat("yyyy/MM/dd"); Log.e("time","time3="+format.format(date)); format=new SimpleDateFormat("HH:mm:ss"); Log.e("time","time4="+format.format(date)); format=new SimpleDateFormat("EEEE"); Log.e("time","time5="+format.format(date)); format=new SimpleDateFormat("E"); Log.e("time","time6="+format.format(date)); } }