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)); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-14 17:16:56