您的位置:首页 > 编程语言 > Java开发

java获取不同时间的工具类

2017-04-24 14:22 246 查看
// @author
// 计算本周  上周  前三月  今年  去年 的时间段
// 组装时间  用于计算同比和环比的
private  Map<String, Object>  getcurrlastweek(Map<String, Object> map){
String isdata = (String) map.get("range");
boolean bool = false;
if(StringUtil.isBlank(isdata)|| "all".equals(isdata)){
return map;
}
Calendar cal = Calendar.getInstance(Locale.CHINA);
Calendar cal2 = Calendar.getInstance(Locale.CHINA);
//n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推
int n = 1;
String monday = null;
String sunday = null;
String yearbegin = null;
String yearend = null;
switch(isdata){
case "thisWeek":
bool = true;
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.add(Calendar.DATE, -1*7);
//当前时间,貌似多余,其实是为了所有可能的系统一致
cal.setTimeInMillis(System.currentTimeMillis());
//想周几,这里就传几Calendar.MONDAY(TUESDAY...)
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal2.setTime(cal.getTime());
cal2.add(Calendar.YEAR, -1);
yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());
cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.YEAR, -1);
yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
break;
case "lastWeek" :
bool = true;
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.add(Calendar.DATE, -2*7);
//当前时间,貌似多余,其实是为了所有可能的系统一致
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal2.setTime(cal.getTime());
cal2.add(Calendar.YEAR, -1);
yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());
cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.YEAR, -1);
yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
break ;
case "lastMonth":
cal.setTimeInMillis(System.currentTimeMillis());
int month = cal.get(Calendar.MONTH);
cal.set(Calendar.MONTH, month-2);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
sunday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal2.setTime(cal.getTime());
cal2.add(Calendar.YEAR, -1);
yearend= new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
monday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.YEAR, -1);
yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
break;
case "lastThreeMonth":
cal.setTimeInMillis(System.currentTimeMillis());
int month2 = cal.get(Calendar.MONTH);
cal.set(Calendar.MONTH, month2-6);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
monday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal2.setTime(cal.getTime());
cal2.add(Calendar.YEAR, -1);
yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());
int month3 = cal.get(Calendar.MONTH);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.MONTH, month3+3);
sunday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.YEAR, -1);
yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
break;
case "thisYear":
int currentYear = cal.get(Calendar.YEAR)-1;
cal.clear();
cal.set(Calendar.YEAR, currentYear);
Date currYearFirst = cal.getTime();
monday= new SimpleDateFormat("yyyy-MM-dd").format(currYearFirst);
cal2.setTime(cal.getTime());
cal2.add(Calendar.YEAR, -1);
// yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());
yearbegin = monday;
cal.clear();
cal.set(Calendar.YEAR, currentYear);
cal.roll(Calendar.DAY_OF_YEAR, -1);

93fb
Date currYearLast = cal.getTime();
sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.YEAR, -1);
//  yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
yearend = sunday;
break;
default:
break;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: