List获取近六个月dateList = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); for (int i = 0; i < 7; i++) { Date date = DateUtils.addDays(new Date(), -i); String formatDate = sdf.format(date); dateList.add(formatDate); }
1.不带年份的
ListresultList = new ArrayList (); Calendar cal = Calendar.getInstance(); //近六个月 cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1); //要先+1,才能把本月的算进去 for(int i=0; i<6; i++){ cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1); //逐次往前推1个月 resultList.add( (cal.get(Calendar.MONTH)+1 < 10 ? "0" + (cal.get(Calendar.MONTH)+1) : String.valueOf((cal.get(Calendar.MONTH)+1)))); }
2.带年份的
ListresultList = new ArrayList (); Calendar cal = Calendar.getInstance(); //近六个月 cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1); //要先+1,才能把本月的算进去 for(int i=0; i<6; i++){ cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1); //逐次往前推1个月 resultList.add(String.valueOf(cal.get(Calendar.YEAR)) +"-"+ (cal.get(Calendar.MONTH)+1 < 10 ? "0" + (cal.get(Calendar.MONTH)+1) : (cal.get(Calendar.MONTH)+1))); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)