/**
* 获取指定时间最近12月的年月(含当月)
*
*/
public static String[] getLatest12Month(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date parse = null;
try {
parse = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String[] months = new String[12];
Calendar cal = Calendar.getInstance();
cal.setTime(parse);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
// 加一行代码,否则3月重复
cal.set(Calendar.DATE,1);
for (int i = 0; i < 12; i++) {
months[11 - i] = cal.get(Calendar.YEAR) + "-" + fillZero(cal.get(Calendar.MONTH) + 1);
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
}
return months;
}
/**
* 格式化月份
*/
public static String fillZero(int i) {
String month = "";
if (i < 10) {
month = "0" + i;
} else {
month = String.valueOf(i);
}
return month;
}
本文地址:https://blog.csdn.net/gunicoin/article/details/109615143
您可能感兴趣的文章:
- 创建Gradle工程出现Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradleXX'.问题解决
- spring boot 错误:Check your ViewResolver setup
- java实现判断一个整数是几位数,并按照逆序输出
- SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embe
- Please, configure Web Facet first!idea报这错的解决办法!!
- [异常处理1]Error occurred during initialization of boot layer
- 解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 问题
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论