1. 自然周中的起止时间
public static Map getWeek(){ Mapmap = new HashMap<>(); Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); String beginTime = format.format(c.getTime()); map.put("startDate", beginTime + " 00:00:00"); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); String endTime = format.format(c.getTime()); map.put("endDate", endTime + " 23:59:59"); return map; }
2. 获取当前月第一天 - 获取当前月最后一天
public static Map getMonth(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Mapmap = new HashMap<>(); Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 String beginTime = format.format(c.getTime()); map.put("startDate", beginTime + " 00:00:00"); Calendar ca = Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); String endTime = format.format(ca.getTime()); map.put("endDate", endTime + " 23:59:59"); return map; }
3. 获取某季度的第一天和最后一天
public static Integer getQuarter() { Calendar c = Calendar.getInstance(); Integer month = c.get(c.MONTH) + 1; Integer quarter = 0; if (month >= 1 && month <= 3) { quarter = 1; } else if (month >= 4 && month <= 6) { quarter = 2; } else if (month >= 7 && month <= 9) { quarter = 3; } else { quarter = 4; } return quarter; } public static Map getCurrQuarter(int num) { Mapmap = new HashMap<>(); String str = ""; // 设置本年的季 Calendar quarterCalendar = null; switch (num) { case 1: // 本年到现在经过了一个季度,在加上前4个季度 quarterCalendar = Calendar.getInstance(); quarterCalendar.set(Calendar.MONTH, 3); quarterCalendar.set(Calendar.DATE, 1); quarterCalendar.add(Calendar.DATE, -1); str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd"); map.clear(); map.put("startDate",str.substring(0, str.length() - 5) + "01-01" + " 00:00:00"); map.put("endDate",str + " 23:59:59"); break; case 2: // 本年到现在经过了二个季度,在加上前三个季度 quarterCalendar = Calendar.getInstance(); quarterCalendar.set(Calendar.MONTH, 6); quarterCalendar.set(Calendar.DATE, 1); quarterCalendar.add(Calendar.DATE, -1); str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd"); map.clear(); map.put("startDate",str.substring(0, str.length() - 5) + "04-01" + " 00:00:00"); map.put("endDate",str + " 23:59:59"); break; case 3:// 本年到现在经过了三个季度,在加上前二个季度 quarterCalendar = Calendar.getInstance(); quarterCalendar.set(Calendar.MONTH, 9); quarterCalendar.set(Calendar.DATE, 1); quarterCalendar.add(Calendar.DATE, -1); str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd"); map.clear(); map.put("startDate",str.substring(0, str.length() - 5) + "07-01" + " 00:00:00"); map.put("endDate",str + " 23:59:59"); break; case 4:// 本年到现在经过了四个季度,在加上前一个季度 quarterCalendar = Calendar.getInstance(); str = DateUtils.formatDate(quarterCalendar.getTime(), "yyyy-MM-dd"); map.clear(); map.put("startDate",str.substring(0, str.length() - 5) + "10-01" + " 00:00:00"); map.put("endDate",str.substring(0, str.length() - 5) + "12-31" + " 23:59:59"); break; } return map; }
4. 获取当年的第一天 - 获取当年的最后一天
public static Map getYear(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); Mapmap = new HashMap<>(); String str = sdf.format(new Date()); map.put("startDate",str + "-01-01" + " 00:00:00"); map.put("endDate",str+ "-12-31" + " 23:59:59"); map.put("year",str); return map; }
5. 获取某年某周的时间跨度
public static MapgetWeekRangeMap(int year, int week) { Map timeMap = new HashMap (); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天 calendar.setMinimalDaysInFirstWeek(4);//可以不用设置 int weekYear = calendar.get(Calendar.YEAR);//获得当前的年 calendar.setWeekDate(weekYear, week, 2);//获得指定年的第几周的开始日期 Date time = calendar.getTime(); String startTime = new SimpleDateFormat("yyyy-MM-dd").format(time); timeMap.put("startDate", startTime + " 00:00:00"); calendar.setWeekDate(weekYear, week, 1);//获得指定年的第几周的结束日期 time = calendar.getTime(); String endTime = new SimpleDateFormat("yyyy-MM-dd").format(time); timeMap.put("endDate", endTime + " 23:59:59"); timeMap.put("year",year); timeMap.put("number",week); return timeMap; }
6. 获取某年有多少周
public static int getYearWeekConut(int year) { int week = 52; try { MaptimeMap = getWeekRangeMap(year, 53); if (!CollectionUtils.isEmpty(timeMap)) { String startTime = timeMap.get("startTime").toString(); if (startTime.substring(0, 4).equals(year + "")) { //判断年度是否相符,如果相符说明有53个周。 week = 53; } } } catch (Exception e) { e.printStackTrace(); } return week; }
7. 获取某年所有周的时间跨度
public static Map> getYearWeekMap(int year) { int weeks = getYearWeekConut(year); Map > yearWeekMap = new HashMap >(); for (int i = 1; i <= weeks; i++) { Map timeMap = getWeekRangeMap(year, i); yearWeekMap.put(i + "", timeMap); } return yearWeekMap; }
8. 获取进七周时间,每一周的开始时间-结束时间(跨年)
public static List
9. 获取近七月的时间,每月开始时间-结束时间(跨年)
public static List> getSevenMonth(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); List > listMap = new ArrayList<>(); //获取前月的第一天 for (int i = 0; i >-7; i--) { Map map = new HashMap<>(); map.clear(); Calendar cal_1 = Calendar.getInstance();//获取当前日期 cal_1.add(Calendar.MONTH, i); cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 String firstDay = format.format(cal_1.getTime()); map.put("startDate",firstDay); //获取前月的最后一天 Calendar cale = Calendar.getInstance(); cale.add(Calendar.MONTH, i+1);//设置为1号,当前日期既为本月第一天 cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天 String lastDay = format.format(cale.getTime()); map.put("endDate",lastDay); map.put("year",lastDay.substring(0,7)); listMap.add(map); } return listMap; }
10.获取近4个季度的时间,每个季度的开始时间-结束时间(跨年)
public static List> getFourQuarter1() { List > listMap = new ArrayList<>(); Map map = new HashMap<>(); Date now = new Date(); for (int i = 0; i < 4; i++) { map.clear(); // System.out.println(getYear(now) + "Q" + (getMM(now) + 2) / 3); String aa = getYear(now) + "Q" + (getMM(now) + 2) / 3; listMap.add(getFourQuarter2(aa.substring(0, 4),aa.substring(5,6))); now = monthAddNum(now, -3); } return listMap; } public static Map getFourQuarter2(String year,String num){ Map map = new HashMap<>(); switch (num) { case "1": // 本年到现在经过了一个季度,在加上前4个季度 map.clear(); map.put("startDate",year + "-01-01" + " 00:00:00"); map.put("endDate",year + "-03-31" + " 23:59:59"); map.put("quarter",year + "-1"); break; case "2": // 本年到现在经过了二个季度,在加上前三个季度 map.clear(); map.put("startDate",year + "-04-01" + " 00:00:00"); map.put("endDate",year + "-06-30" + " 23:59:59"); map.put("quarter",year + "-2"); break; case "3":// 本年到现在经过了三个季度,在加上前二个季度 map.clear(); map.put("startDate",year + "-07-01" + " 00:00:00"); map.put("endDate",year + "-09-30" + " 23:59:59"); map.put("quarter",year + "-3"); break; case "4":// 本年到现在经过了四个季度,在加上前一个季度 map.clear(); map.put("startDate",year + "-10-01" + " 00:00:00"); map.put("endDate",year + "-12-31" + " 23:59:59"); map.put("quarter",year + "-4"); break; } return map; }
11. 获取日期所在的月份
public static int getMM(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; }
12. 获取日期所在的年
public static int getYear(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.YEAR); }
public static Date monthAddNum(Date time, Integer num){ Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.add(Calendar.MONTH, num); Date newTime = calendar.getTime(); return newTime; }
13. 获取当天时间
public static String getDate(){ SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd"); return sdf.format(new Date()); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)