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; }
相关文章推荐
- Java获取各种常用时间方法(工具类)
- 【java】获取一年里某季、某月、某周的第一天及最后一天时间(日期工具类)
- IT忍者神龟之Java获取各种常用时间方法(工具类)
- java获取不同时区的时间字符串
- java获取各种时间 工具类
- java中获取不同时间格式的方法
- java中获取不同时间格式的方法
- JAVA获取不同格式的系统时间(时间为String类型)
- java Calendar 日期获取工具类,昨日开始时间,昨日结束时间,当前周周一日期,当前时间上月第一天,获取当前年第一天
- java中获取不同时间格式的方法!及时间戳转化为时间格式
- Android开发之java代码中获取当前系统的时间工具类
- Java生成随机数工具类,进制之间的转换工具类,获取指定时间,时间格式转换工具类
- Java生成随机数工具类,进制之间的转换工具类,获取指定时间,时间格式转换工具类
- Java后端与数据库中获取时间间隔的不同方法
- Java获取时间 时间计算 转换时间工具类
- JAVA中常使用的获取时间方法和设置不同时间
- [置顶] Android开发之java代码中获取当前系统的时间工具类
- JAVA 时间差距,两个时间相差多少天,时,分,秒;获取当天时间零点;获取及天钱的时间 (时间工具类)
- java中获取不同时间格式的方法
- Java获取当前时间+中国时区工具类