最常用的呢,就是下面这种simpleDateFormat的方式代码如下:
long time = System.currentTimeMillis();
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EEE");
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("MM月dd日");
tv_year.setText(format.format(date));
Log.e("time", "time3=" + format.format(date));
format = new SimpleDateFormat("HH:mm");
tv_time.setText(format.format(date));
Log.e("time", "time4=" + format.format(date));
format = new SimpleDateFormat("EEEE");
tv_week.setText(format.format(date));
Log.e("time", "time5=" + format.format(date));
format = new SimpleDateFormat("E");
Log.e("time", "time6=" + format.format(date));
还可以通过以下代码获取Android系统时间是24小时制还是12小时制的:
ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("24"))
{
Log.i("activity","24");
}
最后还可以利用Calendar获取时间代码如下:
Calendar c = Calendar.getInstance();
//取得系统日期:
year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
//取得系统时间:
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
Calendar c = Calendar.getInstance();
//取得系统日期:
year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
//取得系统时间:
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
利用Time获取 代码如下:
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;