返回两个时间段内的季度
参考文章:https://www.cnblogs.com/caoyingjielxq/p/9426972.html
public static Date parse(String string, String patern){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patern); try { Date parse = simpleDateFormat.parse(string); return parse; } catch (ParseException e) { e.printStackTrace(); } return null; }
/**
* 返回两个时间段内,季度的字符串
* @param minDate
* @param maxDate
* @return
*/
public static List<String> getMonthBetweenDates(String minDate, String maxDate){ ArrayList<String> result = new ArrayList<String>(); //HashSet<String> strings = new HashSet<>(); HashMap<String, Integer> map = new HashMap<>(); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(parse(minDate,"yyyy-MM-dd")); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(parse(maxDate,"yyyy-MM-dd")); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; int i = 1; while (curr.before(max)) { //result.add(format(curr.getTime(),"yyyy-MM-dd")); String season1 = getSeason1(curr.getTime()); map.put(season1, i); curr.add(Calendar.MONTH, 1); i++; } ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); for (Map.Entry<String, Integer> entry : entries){ result.add(entry.getKey()); } return result; } /** * * 1 第一季度 2 第二季度 3 第三季度 4 第四季度 * * @param date * @return */ public static String getSeason1(Date date) { int season = 0; String seasonString = ""; Calendar c = Calendar.getInstance(); c.setTime(date); int month = c.get(Calendar.MONTH); int year = c.get(Calendar.YEAR); switch (month) { case Calendar.JANUARY: case Calendar.FEBRUARY: case Calendar.MARCH: season = 1; seasonString = year + "年第一季度"; break; case Calendar.APRIL: case Calendar.MAY: case Calendar.JUNE: season = 2; seasonString = year + "年第二季度"; break; case Calendar.JULY: case Calendar.AUGUST: case Calendar.SEPTEMBER: season = 3; seasonString = year + "年第三季度"; break; case Calendar.OCTOBER: case Calendar.NOVEMBER: case Calendar.DECEMBER: season = 4; seasonString = year + "年第四季度"; break; default: break; } return seasonString; }
------------恢复内容结束------------