最近项目有个需求就是根据传入的时间转换成季度查询数据,就是三个月查一次。下面时根据传入的时间进行季度输出。
import java.util.ArrayList;
import java.util.List;
/**
* @author dingwl
* @date 2019/10/23 16:24
*/
public class aa {
public static String year(int year,int month){ //获取当前日期的季度
String jd;
if(month>=1&&month<=3){
jd=year+"年"+"第一季度";
return jd;
}
else if(month>=4&&month<=6){
jd=year+"年"+"第二季度";
return jd;
}
else if(month>=7&&month<=9){
jd=year+"年"+"第三季度";
return jd;
}
else{
jd=year+"年"+"第四季度";
return jd;
}
}
public static void main(String[] args) {
int years=0, months=0 ,yeare=0,monthe=0;
String[] sts = {};
List<String> list = new ArrayList<>();
list.add("2018-01"); //开始日期只能为 1,4,7,10
list.add("2019-03"); //结束日期只能为 3,6,9,12
for(int i=0;i<list.size();i++){
if(i==0){ //获取第一个日期并根据“-”进行切割
String str = list.get(i);
sts = str.split("-"); //切割
for(int j=0;j<sts.length;j++){
if(j==0){
years =Integer.parseInt(sts[j]); // 把string类型转换成int类型
} else {
months = Integer.parseInt(sts[j]);
}
}
}else {
String strs = list.get(i);
sts = strs.split("-");
for(int j=0;j<sts.length;j++){
if(j==0){
yeare = Integer.parseInt(sts[j]);
} else {
monthe = Integer.parseInt(sts[j]);
}
}
}
}
int monthend=0;
int yue = (yeare-years)*12+(monthe-months)+1; //计算有多少个月份
for(int i = 0 ;i < yue/3;i++){ //计算有多少个季度
aa.year(years,months);//years months 根据这个两个算季度
System.out.println(aa.year(years,months));
// 这里就是真实项目里面接口调用传入(years,months)(years,monthend)
monthend = months+2; //每个季度结尾为开始+2
//传入当前的(years ,months) 和 (years,monthee)
if(monthend == 12){ //当结束月份为12 时就给开始月份赋值1 并且年份加1
months = 1;
years = years+1;
}else {
months+=3; //给开始月份+3
}
}
}
}
结果如下所示