package com.yx.dwdb.manage.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Auther: hsd
* @Date: 2022/8/9 13:01
* @Description:
*/
public class DateBetweenUtils {
/*季度*/
public static List<String> getQuarterBetween(String startTime, String endTime) {
List<String> monthList = new ArrayList<>();
Set<String> months = new HashSet<String>();
try {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(startTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
for (String string : result) {
Date date = sdf.parse(string);
DateFormat format1 = new SimpleDateFormat("MM");
DateFormat format = new SimpleDateFormat("yyyy");
String year = format.format(date);
String month = format1.format(date);
int parseInt = Integer.parseInt(month);
if (parseInt >= 1 && parseInt <= 3) {
month = "1";
} else if (parseInt >= 4 && parseInt <= 6) {
month = "2";
} else if (parseInt >= 7 && parseInt <= 9) {
month = "3";
} else {
month = "4";
}
months.add(year + "年" + month + "季度");
}
for (String mm: months ) {
monthList.add(mm);
}
Collections.sort(monthList);
return monthList;
} catch (Exception e) {
e.printStackTrace();
}
return monthList;
}
/*月份*/
public static List<String> getMonthBetween(String startTime, String endTime) {
List<String> monthList = new ArrayList<>();
try {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(startTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
for (String string : result) {
Date date = sdf.parse(string);
DateFormat format = new SimpleDateFormat("yyyy-MM");
String month = format.format(date);
monthList.add(month);
}
Collections.sort(monthList);
return monthList;
} catch (Exception e) {
e.printStackTrace();
}
return monthList;
}
public static List<String> getYearBetween(String startTime, String endTime) {
List<String> yearList = new ArrayList<>();
try {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(startTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
Set<String> years = new HashSet<String>();
for (String string : result) {
Date date = sdf.parse(string);
DateFormat format = new SimpleDateFormat("yyyy");
String yearS = format.format(date);
years.add(yearS);
}
for (String yy: years ) {
yearList.add(yy);
}
Collections.sort(yearList);
return yearList;
} catch (Exception e) {
e.printStackTrace();
}
return yearList;
}
}
还没有评论,来说两句吧...