比如最大的是 2021年11月 就要图片里面2021年11月之前的12个月分 (包括自己本身12月),的
代码如下,经过调试,完全正确:
public class Answer7716354 {
public static void main(String[] args) {
List<String> years = new ArrayList<>();
years.add("2017年03月");
years.add("2017年04月");
years.add("2017年05月");
years.add("2017年06月");
years.add("2017年07月");
years.add("2017年08月");
years.add("2017年09月");
years.add("2017年10月");
years.add("2020年10月");
years.add("2020年11月");
years.add("2020年12月");
years.add("2021年10月");
years.add("2021年11月");
years.add("2021年2月");
years.add("2021年3月");
years.add("2021年4月");
years.add("2021年5月");
years.add("2021年6月");
years.add("2021年7月");
years.add("2021年8月");
years.add("2021年9月");
List<YearMonth> list = years.stream().map(YearMonth::new).sorted().collect(Collectors.toList());
// 只要最大日期前12个
for(int i=list.size()-12; i<list.size(); ++i) {
System.out.println(list.get(i));
}
}
static class YearMonth implements Comparable<YearMonth> {
private static Pattern pattern = Pattern.compile("(\\d{4})年(\\d{1,2})月");
private String origin;
private int year;
private int month;
public YearMonth(String origin) {
this.origin = origin;
Matcher matcher = pattern.matcher(origin);
if(matcher.find()) {
this.year = Integer.parseInt(matcher.group(1));
this.month = Integer.parseInt(matcher.group(2));
}
}
@Override
public String toString() {
return origin;
}
/**
* 从小到大排序
* @param o the object to be compared.
* @return
*/
@Override
public int compareTo(YearMonth o) {
if(this.year > o.year) {
return 1;
}
if(this.year < o.year) {
return -1;
}
if(this.month>o.month) {
return 1;
}
if(this.month<o.month) {
return -1;
}
return 0;
}
}
}
运行结果如下:
2020年11月
2020年12月
2021年2月
2021年3月
2021年4月
2021年5月
2021年6月
2021年7月
2021年8月
2021年9月
2021年10月
2021年11月
如有帮助,请采纳,十分感谢!
你看一下,可以排序哈
import java.text.DateFormat;
import java.util.Collections;
import java.util.Comparator;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
List<String> list =new ArrayList<String>();
list.add("2014-09-04 10:34:41");
list.add("2013-08-04 13:42:19");
list.add("2014-09-04 16:46:49");
list.add("2010-01-04 12:32:00");
list.add("2004-04-04 10:34:41");
list.add("2009-05-14 23:42:19");
list.add("2014-12-04 06:08:49");
list.add("2010-01-24 01:32:00");
Collections.sort(list);
System.out.println(list.get(0));
}
}
写个排序的例子,你看下
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("2017年06月");
list.add("2017年07月");
list.add("2017年12月");
list.add("2017年08月");
list.add("2017年10月");
list.add("2017年09月");
//会出现这种月份前面不带0的数据
list.add("2017年2月");
list.add("2017年11月");
list.add("2018年02月");
list.add("2018年01月");
list.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//第一个数的年份
Integer year1 = Integer.parseInt(o1.substring(0, o1.indexOf("年")));
//第一个数的月份
Integer month1 = Integer.parseInt(o1.substring(o1.indexOf("年") + 1, o1.indexOf("月")));
//第二个数的年份
Integer year2 = Integer.parseInt(o2.substring(0, o2.indexOf("年")));
//第二个数的月份
Integer month2 = Integer.parseInt(o2.substring(o2.indexOf("年") + 1, o2.indexOf("月")));
if (year1.compareTo(year2) == 0) {
return month1.compareTo(month2);
} else {
return year1.compareTo(year2);
}
}
});
System.out.println(list);
}
}
List<String> lists = new ArrayList<>();
for (int i=20;i<22;i++){
for (int j=12;j>0;j--){
lists.add(String.format("20%s年%s月", i,j));
}
}
System.out.println(lists);
List<String> res = lists.stream().map(e -> {
if (e.length() != 8) {
return e.replace("年", "年0");
}
return e;
}).collect(Collectors.toList());
System.out.println(res);
Collections.sort(res,(a,b)-> b.compareTo(a));
System.out.println(res);
[2020年12月, 2020年11月, 2020年10月, 2020年9月, 2020年8月, 2020年7月, 2020年6月, 2020年5月, 2020年4月, 2020年3月, 2020年2月, 2020年1月, 2021年12月, 2021年11月, 2021年10月, 2021年9月, 2021年8月, 2021年7月, 2021年6月, 2021年5月, 2021年4月, 2021年3月, 2021年2月, 2021年1月]
[2020年12月, 2020年11月, 2020年10月, 2020年09月, 2020年08月, 2020年07月, 2020年06月, 2020年05月, 2020年04月, 2020年03月, 2020年02月, 2020年01月, 2021年12月, 2021年11月, 2021年10月, 2021年09月, 2021年08月, 2021年07月, 2021年06月, 2021年05月, 2021年04月, 2021年03月, 2021年02月, 2021年01月]
[2021年12月, 2021年11月, 2021年10月, 2021年09月, 2021年08月, 2021年07月, 2021年06月, 2021年05月, 2021年04月, 2021年03月, 2021年02月, 2021年01月, 2020年12月, 2020年11月, 2020年10月, 2020年09月, 2020年08月, 2020年07月, 2020年06月, 2020年05月, 2020年04月, 2020年03月, 2020年02月, 2020年01月]
用两种格式将数据转换称Date,再获取long类型数据,再进行自动排序,获取数据后,转换回去
倒序排了取12个,然后反转一下就是