##Android 日期類##html
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類型java
1.Calendar 轉化 Stringandroid
//獲取當前時間的具體狀況,如年,月,日,week,date,分,秒等 Calendar calendat = Calendar.getInstance(); //默認時區 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdf.format(calendar.getTime());
2.String 轉化Calendaride
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轉化Datethis
Calendar calendar = Calendar.getInstance(); java.util.Date date =calendar.getTime();
4.Long 轉換成時間unix
long timeLong = 1468307486765L; //GMT+08:00保存的 Tue Jul 12 2016 15:11:26 //一、手機默認時區轉換 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); calendar.setTimeInMillis(timeLong); String parsDateStr = sdf.format(calendar.getTime()); //二、指定時區轉換 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時間戳code
Calendar calendar = Calendar.getInstance();//獲取當前日曆對象 long unixTime = calendar.getTimeInMillis();//獲取當前時區下日期時間對應的時間戳 long unixTimeGMT = unixTime - TimeZone.getDefault().getRawOffset();//獲取標準格林尼治時間下日期時間對應的時間戳
Date類型orm
1.Date 轉化Stringhtm
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