Android 获取时间实例代码
Android获取时间实例代码
注意:
h:12小时制小时数
H:24小时制小时数
实例代码:
importjava.text.DateFormat; importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; /** *CreatedbyAdministratoron2017/5/8. */ publicclassGetTime{ publicstaticvoidmain(String[]args){ Datedate=newDate(); System.out.println(date);//MonMay0814:27:44CST2017 System.out.println(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss").format(date));//2017-05-0802:27:44 longmillis=System.currentTimeMillis(); System.out.println(millis);//1494224864479 System.out.println(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss").format(millis));//2017-05-0802:27:44 //yyyy-MM-ddEhh:mm:ss.sss //年-月-日星期时:分:秒.毫秒 System.out.println(newSimpleDateFormat("yyyy-MM-ddEhh:mm:ss.sss").format(date));//2017-05-08星期一02:27:44.044 System.out.println(newSimpleDateFormat("yyyy-MM-ddHH:mm:ss.sss").format(date));//2017-05-0814:27:44.044 System.out.println(newSimpleDateFormat("yyyy-MM-ddHH:mm:ss").format(date));//2017-05-0814:27:44 System.out.println(newSimpleDateFormat("yyyy-MM-ddHH:mm").format(date));//2017-05-0814:27 System.out.println(newSimpleDateFormat().format(date));//17-5-8下午2:27:默认 compareDataToNow("2017-05-0312:45:00"); try{ Datedate1=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss").parse("2017-05-0312:45:00"); compareToNowDate(date1); }catch(ParseExceptione){ e.printStackTrace(); } getWeek(); getTime1(); getTime2(); } staticvoidgetTime1(){ longtime=System.currentTimeMillis(); //longnow=android.os.SystemClock.uptimeMillis(); SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); Dated1=newDate(time); Stringt1=format.format(d1); System.out.println("SimpleDateFormat时间==="+t1);//2017-05-0812:44:10 SimpleDateFormatf4=newSimpleDateFormat("今天是"+"yyyy年MM月dd日Ekk点mm分"); System.out.println("f4======"+f4.format(newDate()));//今天是2017年05月08日星期一14点15分 SimpleDateFormatf3=newSimpleDateFormat("今天是"+"hh小时mm分钟"); System.out.println("f3======"+f3.format(newDate()));//今天是02小时15分钟 SimpleDateFormatf2=newSimpleDateFormat("今天是"+"kk点mm分钟"); System.out.println("f2======"+f2.format(newDate()));//今天是14点17分钟 } staticvoidgetTime2(){ Calendarcalendar=Calendar.getInstance(); Stringcreated=calendar.get(Calendar.YEAR)+"年" +(calendar.get(Calendar.MONTH)+1)+"月"//从0计算 +calendar.get(Calendar.DAY_OF_MONTH)+"日" +calendar.get(Calendar.HOUR_OF_DAY)+"时" +calendar.get(Calendar.MINUTE)+"分"+calendar.get(Calendar.SECOND)+"s"; System.out.println("Calendar时间===="+created);//时间:2017年5月8日12时33分24s } staticvoidgetWeek(){ Calendarcalendar=Calendar.getInstance(); intday=calendar.get(Calendar.DAY_OF_WEEK); Stringtoday=null; if(day==2){ today="Monday"; }elseif(day==3){ today="Tuesday"; }elseif(day==4){ today="Wednesday"; }elseif(day==5){ today="Thursday"; }elseif(day==6){ today="Friday"; }elseif(day==7){ today="Saturday"; }elseif(day==1){ today="Sunday"; } System.out.println("Todayis:-"+today);//Todayis:-Monday } //计算日期之间相隔几天: staticlongcompareDataToNow(Stringdate){ SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); DatepassDate,nowDate; longdiff=-100l,days=-100l; try{ passDate=sdf.parse(date); StringnowStr=sdf.format(newDate()); nowDate=sdf.parse(nowStr); diff=passDate.getTime()-nowDate.getTime();//long型的毫秒数 days=diff/(1000*60*60*24); System.out.println("相隔:"+days+"天"+"nowDate.getTime()====="+nowDate.getTime());//-5天 }catch(ParseExceptione){ e.printStackTrace(); } returndiff; } //计算日期之间相隔几天: staticlongcompareToNowDate(Datedate){ longdiff=-100l,days=-100l; DatenowDate=newDate(); diff=date.getTime()-nowDate.getTime();//long型的毫秒数 days=diff/(1000*60*60*24); System.out.println("相隔:"+days+"天"+"nowDate.getTime()====="+nowDate.getTime());//-5天 returndiff; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!