// 调用方法 参数昨天:yesterday
今天:todaty
Date yesterday = this.getDate("yesterday");
/**
* 时间取值
* todaty 今天
* yesterday 昨天
*/
public Date getDate(String type){
Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
ca.setTime(new Date()); //设置时间为当前时间
ca.add(Calendar.DATE, -1); //年份减1
Date lastMonth = ca.getTime(); //结果
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date Date = null;
try {
switch (type) {
case "todaty": // 今天
Date = sdf.parse(sdf.format(new Date()));
break;
case "yesterday": // 昨天
Date = sdf.parse(sdf.format(lastMonth));
break;
default:
break;
}
} catch (ParseException e) {
e.printStackTrace();
}
return Date;
}
最终返回Date类型时间