DateUtils

public static Map<String, Object> getPeriodTime(String statisticalTime){
Map<String, Object> map = new HashMap<String, Object>();

String startTime = DateUtil.formatWithDefault(startTime(statisticalTime));
String endTime = DateUtil.formatWithDefault(endTime(statisticalTime));
map.put("startTime", startTime);
map.put("endTime", endTime);

return map;

}


//开始时间
private static Date startTime(String statisticalTime){
Calendar cl = Calendar.getInstance();
if ("today".equals(statisticalTime)) {
cl.add(Calendar.DAY_OF_MONTH, 0);     
} else if ("yesterday".equals(statisticalTime)) { 
cl.add(Calendar.DAY_OF_MONTH, -1);
} else if ("week".equals(statisticalTime)) {
cl.add(Calendar.DAY_OF_MONTH, -1); 
            cl.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);      
} else if ("month".equals(statisticalTime)) {
cl.add(Calendar.MONTH, 0);     
cl.set(Calendar.DAY_OF_MONTH, 1);  
} else if ("lastweek".equals(statisticalTime)) {
cl.add(Calendar.WEEK_OF_YEAR, -2);   
            cl.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
} else if ("lastmonth".equals(statisticalTime)) {
cl.add(Calendar.MONTH, -1);
cl.set(Calendar.DAY_OF_MONTH,1); 
} else {
cl.add(Calendar.DAY_OF_MONTH, 0);
statisticalTime = "today";
}
return DateUtil.getDayStartTime(cl.getTime());
}

//结束时间
public static Date endTime(String statisticalTime) {
Calendar cl = Calendar.getInstance();
Date startDate = startTime(statisticalTime);  //开始时间
Date date = null;
if ("today".equals(statisticalTime)) {
date = DateUtil.getNextDayStartTime(startDate);
} else if ("yesterday".equals(statisticalTime)) { 
cl.add(Calendar.DAY_OF_MONTH, -1);
date = DateUtil.getNextDayStartTime(cl.getTime());
} else if ("week".equals(statisticalTime)) {        
cl.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            date = DateUtil.getNextDayStartTime(cl.getTime());
} else if ("month".equals(statisticalTime)) {
cl.add(Calendar.MONTH, 0);
cl.set(Calendar.DAY_OF_MONTH, 1);
cl.add(Calendar.DATE, cl.getActualMaximum(Calendar.DAY_OF_MONTH));
            date = cl.getTime();
} else if ("lastweek".equals(statisticalTime)) {
while (cl.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
           cl.add(Calendar.DAY_OF_WEEK, -1);
       }
int dayOfWeek=cl.get(Calendar.DAY_OF_WEEK)-1;
int offset=7-dayOfWeek;
cl.add(Calendar.DATE, offset-7);
            date = DateUtil.getNextDayStartTime(cl.getTime());
} else if ("lastmonth".equals(statisticalTime)) {
int year = 0; 
       int month = cl.get(Calendar.MONTH); //上个月月份  
       if (month == 0) { 
           year = cl.get(Calendar.YEAR) - 1; 
           month = 12; 
       } else { 
           year = cl.get(Calendar.YEAR); 
       } 
       int dayOfmonth =cl.getActualMaximum(Calendar.DAY_OF_MONTH); 
cl.set(year, month-1, dayOfmonth);
            date = DateUtil.getNextDayStartTime(cl.getTime());
} else{
date = DateUtil.getNextDayStartTime(startDate);
}
return date;

}


/**
* 获得昨天的时间

* @return 今天的时间
*/
public static Date getToday() {
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 0);
date = calendar.getTime();
Date yesterday=DateUtil.parse(DateUtil.formatWithDefault(date), "yyyy-MM-dd");

return yesterday;
}

/**
* 将字符串转换为时间格式
*/
public static Date FormatStringDate(String str){
Date date=null;  
   SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");  
   try {
date=formatter.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}  
   return date;

}


/**
* 根据方式统计method 1.按月.2按季度.3按年.4按时间段  返回时间段(开始日期到结束日期),默认当前月第一天到当前日期
* @param method 1.按月.2按季度.3按年.4按时间段  返回时间段
* @param startTime 开始时间
* @param endTime 结束时间
* @param season 季度
* @throws ParseException 
*/
public static Map<String, String> getDateRange(String method,String startTime,String endTime,String season) throws ParseException{
Calendar cal = Calendar.getInstance();
Date today = new Date();
cal.setTime(today);
SimpleDateFormat dateFomatter=new SimpleDateFormat("yyyy-MM-dd");  
SimpleDateFormat monthFomatter=new SimpleDateFormat("yyyy-MM");
SimpleDateFormat yearFomatter=new SimpleDateFormat("yyyy");  

Map<String, String> range = new HashMap<String, String>();
String startDate = "";
String endDate = "";
//按月
if("1".equals(method)){
startDate = startTime+"-01";
cal.setTime(monthFomatter.parse(startTime));
cal.add(Calendar.MONTH,1);
cal.set(Calendar.DAY_OF_MONTH,1);
endDate = dateFomatter.format(cal.getTime());
//按季度
}else if("2".equals(method)){
//设置开始时间为该季度的第一天
startDate = startTime+"-"+getSeasonFirstDate(season);
cal.setTime(monthFomatter.parse(startTime+"-"+getSeasonEndMonth(season)));
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH,1);
endDate = dateFomatter.format(cal.getTime());
//按年
}else if("3".equals(method)){
startDate = startTime+"-01-01";
cal.setTime(yearFomatter.parse(startTime));
cal.add(Calendar.YEAR,1);
cal.set(Calendar.MONTH,0);
cal.set(Calendar.DAY_OF_MONTH,1);
endDate = dateFomatter.format(cal.getTime());
//按时间段
}else if("4".equals(method)){
startDate = startTime;
endDate = endTime;
}else{
startDate = monthFomatter.format(today)+"-01";
endDate = dateFomatter.format(today);
}
range.put("startDate", startDate);
range.put("endDate", endDate);
return range;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值