android中系统日期时间的获取
16lz
2021-01-23
-
- importjava.text.SimpleDateFormat;
- SimpleDateFormatformatter=newSimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
- DatecurDate=newDate(System.currentTimeMillis());//获取当前时间
- Stringstr=formatter.format(curDate);
可以获取当前的年月时分,也可以分开写:
[java] view plain copy print ?- SimpleDateFormatsDateFormat=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
- Stringdate=sDateFormat.format(newjava.util.Date());
如果想获取当前的年月,则可以这样写(只获取时间或秒种一样):
Java代码 [java] view plain copy print ?- SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM");
- Stringdate=sdf.format(newjava.util.Date());
当然还有就是可以指定时区的时间(待):
[java] view plain copy print ?- df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA);
- System.out.println(df.format(newDate()));
- ContentResolvercv=this.getContentResolver();
- StringstrTimeFormat=android.provider.Settings.System.getString(cv,
- android.provider.Settings.System.TIME_12_24);
- if(strTimeFormat.equals("24"))
- {
- Log.i("activity","24");
- }
- Calendarc=Calendar.getInstance();
- 取得系统日期:year=c.get(Calendar.YEAR)
- month=c.grt(Calendar.MONTH)
- day=c.get(Calendar.DAY_OF_MONTH)
- 取得系统时间:hour=c.get(Calendar.HOUR_OF_DAY);
- minute=c.get(Calendar.MINUTE)
利用Calendar获取
[java]view plaincopyprint?
- Calendarc=Calendar.getInstance();
- 取得系统日期:year=c.get(Calendar.YEAR)
- month=c.grt(Calendar.MONTH)
- day=c.get(Calendar.DAY_OF_MONTH)
- 取得系统时间:hour=c.get(Calendar.HOUR_OF_DAY);
- minute=c.get(Calendar.MINUTE)
- Calendarc=Calendar.getInstance();
- 取得系统日期:year=c.get(Calendar.YEAR)
- month=c.grt(Calendar.MONTH)
- day=c.get(Calendar.DAY_OF_MONTH)
- 取得系统时间:hour=c.get(Calendar.HOUR_OF_DAY);
- minute=c.get(Calendar.MINUTE)
利用Time获取
[java] view plain copy print ?- Timet=newTime();//orTimet=newTime("GMT+8");加上TimeZone资料。
- t.setToNow();//取得系统时间。
- intyear=t.year;
- intmonth=t.month;
- intdate=t.monthDay;
- inthour=t.hour;//0-23
- intminute=t.minute;
- intsecond=t.second;
更多相关文章
- 系统关闭GPRS数据流量和 GPS的方法
- Android 日历方式显示的日期选择组件
- Android(4.4)音频系统之mediaserver服务启动
- android 获取设备上安装的非系统应用信息
- Android各代码层获取系统时间的方法
- Android rom开发:不显示系统的电池信息
- Android——调用系统相册
- android获取系统设置的铃声并播放
- android 时间滚动控件 底部弹出