Android中獲取系統(tǒng)時間有多種方法,可分為Java中Calendar類獲取豺旬,java.util.date類實現(xiàn)讨跟,還有android中Time實現(xiàn)
現(xiàn)總結如下:
//一
long time=System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=new Date(time);
String t1=format.format(d1);
Log.e("msg", t1);
//二
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String t=format.format(new Date());
Log.e("msg", t);
//三
Calendar calendar = Calendar.getInstance();
String created = calendar.get(Calendar.YEAR) + "年"
+ (calendar.get(Calendar.MONTH)+1) + "月"http://從0計算
+ calendar.get(Calendar.DAY_OF_MONTH) + "日"
+ calendar.get(Calendar.HOUR_OF_DAY) + "時"
+ calendar.get(Calendar.MINUTE) + "分"+calendar.get(Calendar.SECOND)+"s";
Log.e("msg", created);
//四
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone資料。
t.setToNow(); // 取得系統(tǒng)時間。
String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second;
Log.e("msg", time);
最后說一下日期格式化年柠,日期格式化通常使用SimpleDateFormat類實現(xiàn),其中的日期格式不能夠自己隨意定義褪迟,主要有以下幾種形式:
SimpleDateFormat f1= new SimpleDateFormat(); //其中沒有些格式化參數(shù)彪杉,我們使用默認的日期格式。
System.out.println(f.formate(new Date()));
代碼輸出的日期格式為:12-3-22 下午4:36
SimpleDateFormat f4= new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk點mm分");//可根據(jù)不同樣式請求顯示不同日期格式牵咙,要顯示星期可以添加E參數(shù)
System.out.println(f4.format(new Date()));
代碼輸出的日期格式為:今天是2012年03月22日 星期四 16點46分
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println("Date to String "+formater.format(new Date()));
相近的常用形式還有 yyMMdd hh:mm:ss yyyy-MM-dd hh:mm:ss dd-MM-yyyy hh:mm:ss
有的時候通常還會需要把具體日期轉換為毫秒或者Timestamp形式派近,如下:
文本 - > Timestamp,日期 -> Timestamp
Timestamp t ;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try ...{
t = new Timestamp(format.parse("2007-07-19 00:00:00").getTime());
} catch (ParseException e) ...{
e.printStackTrace();
}
Timestamp t ;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
t = new Timestamp(new Date().getTime());
日期比較,轉換處理
public void compareToNowDate(Date date){
Date nowdate=new Date();
//format date pattern
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//convert to millions seconds
long time=DateToLong(StringToDate(formatter.format(nowdate)));
long serverTime=DateToLong(date);
//convert to seconds
long minTime=Math.abs(time-serverTime)/1000; Log.d(getLocalClassName(), "minTime= "+minTime);
}
private long DateToLong(Date time){
SimpleDateFormat st=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyyMMddHHmmss
return Long.parseLong(st.format(time));
}
private Date StringToDate(String s){
Date time=null;
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
time=sd.parse(s);
} catch (java.text.ParseException e) {
System.out.println("輸入的日期格式有誤洁桌!");
e.printStackTrace();
}
return time;
}
//計算日期之間相隔幾天:
public long compareDataToNow(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date passDate,nowDate;
long diff=-100l,days=-100l;
try {
passDate = sdf.parse(date);
String nowStr=sdf.format(new Date());
nowDate=sdf.parse(nowStr);
diff = passDate.getTime() - nowDate.getTime();
days = diff / (1000 * 60 * 60 * 24);
System.out.println( "相隔:"+days+"天");
} catch (ParseException e) {
e.printStackTrace();
}
return diff;
}