`
vip02
  • 浏览: 5947 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
最近访客 更多访客>>
社区版块
存档分类
最新评论

时间处理

阅读更多
写道
1.得到当前的时间 

Calendar cal = Calendar.getInstance(); 
String time=formatter.format(cal.getTime()); 
String strEnd=time.substring(0, 16);// 2007-10-30 09:30 

Calendar cal = Calendar.getInstance(); 
String time=formatter.format(cal.getTime()); 
String strEnd=time.substring(0, 16);// 2007-10-30 09:30 

2.得到前一天的时间 

Calendar c = Calendar.getInstance(); 
c.add(Calendar.DAY_OF_MONTH, -1); 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String mDateTime=formatter.format(c.getTime()); 
String strStart=mDateTime.substring(0, 16);//2007-10-29 09:30 

Calendar c = Calendar.getInstance(); 
c.add(Calendar.DAY_OF_MONTH, -1); 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String mDateTime=formatter.format(c.getTime()); 
String strStart=mDateTime.substring(0, 16);//2007-10-29 09:30 

3.得到前几天的时间 

Calendar c = Calendar.getInstance(); 
c.add(Calendar.DAY_OF_MONTH, -5);//得到前5 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String mDateTime=formatter.format(c.getTime()); 
String strStart=mDateTime.substring(0, 16);//2007-10-24 09:30 

Calendar c = Calendar.getInstance(); 
c.add(Calendar.DAY_OF_MONTH, -5);//得到前5 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String mDateTime=formatter.format(c.getTime()); 
String strStart=mDateTime.substring(0, 16);//2007-10-24 09:30 

4.得到当前时间的前几年的时间 


Calendar c = Calendar.getInstance(); 
c.add(Calendar.YEAR, -5);//得到前5年 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String mDateTime=formatter.format(c.getTime()); 
String strStart=mDateTime.substring(0, 16);//2002-10-24 09:30 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics