这样的时间段一般用于大屏上的数据展示。
public String getKssj(String type){
String kssj = null;
Date d=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(d);
if("day".equals(type)){
kssj = df.format(d);
}else if("week".equals(type)){
c.add(Calendar.DATE, - 6);
Date date = c.getTime();
kssj = df.format(date);
}else if ("month".equals(type)) {
c.add(Calendar.MONTH, - 1);
c.add(Calendar.DATE, + 1);
Date date = c.getTime();
kssj = df.format(date);
}else if("year".equals(type)){
c.add(Calendar.YEAR, - 1);
c.add(Calendar.MONTH, + 1);
c.set(Calendar.DAY_OF_MONTH,1);
Date date = c.getTime();
kssj = df.format(date);
}
return kssj;
}
备注:代码中的计算数量。这样自定义的静态常量按系统定义来或者自己来定义都可以,只要计算正确的时间段即可!
public final static int DATE = 5;
public final static int MONTH = 2;
public final static int YEAR = 1;
public final static int DAY_OF_MONTH = 5;