1.通过SimpleDateFormat来获取时间
<span style="white-space:pre"> </span>/**
* 获取当前时间,含日期
* (24小时制)
*/
public void getcurrentdate1(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
System.out.println("当前时间(包含日期):"+format);
}
/**
* 获取当前时间,含日期
* (12小时制)
*/
public void getcurrentdate2(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = simpleDateFormat.format(new Date());
System.out.println("当前时间(包含日期):"+format);
}
可以截取自己所需的时间段,如:年、月、秒等等
test.getSpecialTime("ss");
<span style="white-space:pre"> </span>/**
* 获取自己想要的时间,如年、月、日、秒、小时
* 截取某一部分
* @param format 时间格式 如:"yyyy-MM"
*/
public void getSpecialTime(String format){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
String format2 = simpleDateFormat.format(date);
System.out.println("您所截取的时间为:"+format2);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format3 = simpleDateFormat1.format(date);
System.out.println("当前时间(包含日期):"+format3+" new Date(): "+date);
}
还可以获取指定时区的时间,返回值还是该时区的语言
test.getSpecialPositionTime(Locale.FRANCE);
<span style="white-space:pre"> </span>/**
* 获取指定时区的时间
* @param locale 时区
*/
public void getSpecialPositionTime(Locale locale){
DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
// DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.CHINA);
System.out.println("您所指定的区时为:"+dateTimeInstance.format(new Date()));
}
2.如何获取Android系统时间是24小时制还是12小时制,我用虚拟机测出来是null
/**
* 获取Android系统的时制
*/
public String getTimeSystem(){
ContentResolver cv = this.getContentResolver();
String stringTimeFormat = android.provider.Settings.System.getString(cv, android.provider.Settings.System.TIME_12_24);
System.out.println("TimeSystem "+stringTimeFormat);
time.setText(stringTimeFormat);
return stringTimeFormat;
// if(stringTimeFormat.equals("24")){
// Log.i("activity", "24");
// return stringTimeFormat;
// }else {
// return stringTimeFormat;
// }
}
3.通过Calender获取时间
<span style="white-space:pre"> </span>/**
* 通过calendar获取时间
*/
public void getByCalendar(){
Calendar instance = Calendar.getInstance();
int i = instance.get(Calendar.YEAR);
int j = instance.get(Calendar.MONTH);
int k = instance.get(Calendar.DAY_OF_MONTH);
int l = instance.get(Calendar.HOUR);
int l2 = instance.get(Calendar.HOUR_OF_DAY);
int m = instance.get(Calendar.MINUTE);
System.out.println("Calendar:"+i+" "+m);
}
注意:
Calendar.YEAR——年份
Calendar.MONTH——月份
Calendar.DATE——日期
Calendar.DAY_OF_MONTH——日期,和上面的字段意义完全相同
Calendar.HOUR——12小时制的小时
Calendar.HOUR_OF_DAY——24小时制的小时
Calendar.MINUTE——分钟
Calendar.SECOND——秒
Calendar.DAY_OF_WEEK——星期几
4.安卓特有的方法Time
public void getByTime(){
Time t = new Time();//or Time t = new Time("GMT+8");加上Time Zome资料
t.setToNow();//获取系统时间
int year = t.year;
int yearDay = t.yearDay;
int monthDay = t.monthDay;
int month = t.month;
int hour = t.hour;//0-23
int minute = t.minute;
int second = t.second;
System.out.println("year:"+year);
System.out.println("yearDay "+yearDay);
System.out.println("month "+month);
System.out.println("monthDay:"+monthDay);
System.out.println("hour:"+hour);
System.out.println("minute:"+minute);
System.out.println("second:"+second);
}