/**
* 获得指定日期的前一天
*
* @param dateStr
* @return
*/
public static String getSpecifiedDayBefore(String dateStr){
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
/**
* 获取指定日期所在周的第一天和最后一天
*
* @param dateStr
* @return
*/
public static String[] getDayBeforeAndAfter(String dateStr){
String[] days=new String[2];
Calendar cal = Calendar.getInstance();
try {
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int d = 0;
if(cal.get(Calendar.DAY_OF_WEEK)==1){
d = -6;
}else{
d = 2-cal.get(Calendar.DAY_OF_WEEK);
}
cal.add(Calendar.DAY_OF_WEEK, d);
//所在周开始日期
days[0]=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
//所在周结束日期
days[1]=new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
return days;
}
/**
* 获取指定日期的前一个月
*
* @param dateStr
* @return
*/
public static String getMonthBefore(String dateStr){
String month="";
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
month = new SimpleDateFormat("yyyy-MM").format(m);
return month;
}