##Android 日期类##
Unix时间戳在计算上最为方便和灵活,效率也高;而Date和Calendar则在一些具体的日期计算上更为便利。
Date和Calendar自动根据手机所设置的时区来调整时间戳的,也就是该时区真实的时间戳
SimpleDateFormat可设置地区和时区,而是获取指定时区的格式化时间
使用TimeZone.getDefault().getRawOffset()方法来得到Calendar和Date的时间差
Date nowDateStr = new Date(); // 默认时区
Calendar calendar = Calendar.getInstance(); //默认时区
Log.d(TAG, "calendar long = " + calendar.getTimeInMillis());
Log.d(TAG, " date long = " + nowDateStr.getTime());
Log.d(TAG, "calendar date = " + calendar.getTime());
Log.d(TAG, " date = " + nowDateStr);
Calendar在不手动设置时区时,是与系统默认时区相关的。
在手动修改时区后,不能使用calendar.getTime方法来直接获取Date日期,因为此时的日期与setTime时的值相同
想要正确获取修改时区后的时间,应该通过calendar的get方法。
Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("GMT-08:00")); // 指定时区
// 获取的还是默认时区
Log.d(TAG, "calendar1 long = " + calendar1.getTimeInMillis());
Log.d(TAG, "calendar1 date = " + calendar1.getTime());
// 获取的是指定时区
Log.d(TAG, "calendar HOUR_OF_DAY = " + calendar1.get(Calendar.HOUR_OF_DAY));
Log.d(TAG, "calendar MINUTE = " + calendar1.get(Calendar.MINUTE));
Log.d(TAG, "calendar SECOND = " + calendar1.get(Calendar.SECOND));
Log.d(TAG, "calendar MILLISECOND = " + calendar1.get(Calendar.MILLISECOND));
int offset = TimeZone.getDefault().getRawOffset(); // 与标准时间戳(GMT+00:00)差
Calendar类型
1.Calendar 转化 String
//获取当前时间的具体情况,如年,月,日,week,date,分,秒等
Calendar calendat = Calendar.getInstance(); //默认时区
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
2.String 转化Calendar
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
3.Calendar转化Date
Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();
4.Long 转换成时间
long timeLong = 1468307486765L; //GMT+08:00保存的 Tue Jul 12 2016 15:11:26
//1、手机默认时区转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
calendar.setTimeInMillis(timeLong);
String parsDateStr = sdf.format(calendar.getTime());
//2、指定时区转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00")); // 格式化为指定时区
String parsDateStr = sdf.format(calendar.getTime());
5.Calendar转化Unix时间戳
Calendar calendar = Calendar.getInstance();//获取当前日历对象
long unixTime = calendar.getTimeInMillis();//获取当前时区下日期时间对应的时间戳
long unixTimeGMT = unixTime - TimeZone.getDefault().getRawOffset();//获取标准格林尼治时间下日期时间对应的时间戳
Date类型
1.Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
2.String 转化Date
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse(str);
3.Date 转化Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());
4.Date 转化Unix时间戳
Date date = new Date();//获取当前日期对象
unixTimeGMT = unixTime = date.getTimeInMillis();//获取当前时区下日期时间对应的时间戳
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
String dateString = "2010-12-26 03:36:25";//设定具有指定格式的日期字符串
unixTimeGMT = unixTime = format.format(date);//获取当前时区下日期时间对应的时间戳
获取当前Android的年月日时分秒的时间
Android的文件有建议用Time代替Calendar。用Time对CPU的负荷会较小。(取出时间只有24小时模式)
1)
Time time = new Time("GMT+8"); // 或 Time t=new Time();
time.setToNow();
int year = time.year;
int month = time.month;
int day = time.monthDay;
int minute = time.minute;
int hour = time.hour;
int sec = time.second;
2)
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 hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
获取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");
}
参考:http://www.2cto.com/kf/201207/139551.html , http://fjfj910.iteye.com/blog/1202219