1. String convert to Date
DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
try {
Date date = (Date) formatter.parse(processDate);
} catch (ParseException e) {
e.printStackTrace();
}
2.Date convert to String
DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Date tmpDate = new Date();
String strDate = formatter.format(tmpDate) ;
3.Calendar Date 几个用法
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); //date :任何一个Date
calendar.set(Calendar.DAY_OF_MONTH, 1); // date 这个月的第一天
Date firstDayOfMonth = calendar.getTime(); //得到第一天的日期
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//date这个月总天数
//get the day of week by the date , 该date是所在星期内的第几天, sunday 是1, saturday 是7
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
//以date为基准,找它之后的第几天是几号, 例如,9月1号后的第0天是9月1号,9月1号后的第一天是9月2号,9月1号后的第2天是9月3号,依次类推, daySeek 就是第几天
public static Date seekDateByDay(Date date,int daySeek){
if(date == null){
return null;
}
Calendar seekedDate = Calendar.getInstance();
seekedDate.setTime(date);
seekedDate.set(Calendar.DAY_OF_MONTH, seekedDate.get(Calendar.DAY_OF_MONTH)+daySeek);
return seekedDate.getTime();
}