`
从此醉
  • 浏览: 1045744 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

java获取字符串格式日期向前或向后n天的日期

 
阅读更多
private void setTilteMessage(){
		BaseDao dao = new BaseDao();
		String titleData = dao.getLatestData();
		dao.closeDB();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = sdf.parse(titleData);
			Calendar   calendar   =   new   GregorianCalendar(); 
		    calendar.setTime(date); 
		    calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
		    date=calendar.getTime();   //这个时间就是日期往后推一天的结果 
		    String putDate = sdf.format(date); //增加一天后的日期
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

如上代码:

titleData是2013-10-25这种格式的日期

先转成Data类型,最后用Calendar进行日期的增加或减少

再转成string

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics