最近太忙了,加上春节年假,好久没有动手写东西了.总感觉好像少点什么东西.随便写点东西吧.总结一下各类时间的获取:
时间获取
1,获取格式:
2016年03月03日 08:23:14 (24小时制)
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
2,获取格式:
2016-03-03 08:23:14
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sDateFormat.format(new java.util.Date());
3,如果想获取当前的年月,则可以这样写(只获取时间或秒种一样):
2016-03
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
String date=sdf.format(new java.util.Date());
4,可以指定时区的时间
Thursday, March 3, 2016 8:38:12 AM CST—美国时间
DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.US);
System.out.println(df.format(new Date()));
使用Calendar
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int minute = c.get(Calendar.MINUTE)
System.out.println(year +"-"+ month +"-"+ day); ---- 2016-2-3
使用Time
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上时区信息。
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;
如何获取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");
}
时间获取的一些东西基本上差不多了,也该上班了,明天写时间转换的工具类吧.