您的位置:首页 > 移动开发 > Android开发

Android手机开发:日期函数之判断某日期格式是否之前+计算某日期之前N天或之后N天的日期

2011-08-27 20:49 1281 查看
1. 判断某日期格式是否正确

/**
* 判断日期格式是否正确
*/
public static boolean IsDateFormat(String dataStr) {
boolean state = false;
try {
java.text.SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
dFormat.setLenient(false);
java.util.Date d = dFormat.parse(dataStr);
state = true;
} catch (ParseException e) {
e.printStackTrace();
state = false;
}
return state;
}


2. 计算某日期之前N天的日期

/**
* 计算date之前n天的日期
*/
public static Date getDateBefore(Date date, int n) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE, now.get(Calendar.DATE) - n);
return now.getTime();
}


3. 计算某日期之后N天的日期

/**
* 得到几天后的时间
*/
public static Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐