在新项目里,尤其是银行项目中,常用一些时间函数,而这些函数往往是时间工具类中所没有的,或是不够全面的,不足以完全得符合自己的要求,所以根据自己的项目上传了几个自己手写的函数。此处使用LocalDate类代替Date类,不明白的可直接请教度娘。
根据yyyyMM字符串,获取月末时间(月初太简单,可直接拼接)
根据时间段,获取yyyyMM格式的月份集合
本次上传的参数string的日期格式为 yyyyMM,因为行方选择日期时只到月份
// 首先拼接月初日期(后面会经常调用,避免重复代码)
public static String getMonthStart(String month) {
try {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
StringBuffer dataStr = new StringBuffer(month);
dataStr.append("01");
LocalDate date = LocalDate.parse(dataStr, dateFormat);
return date.format(dateFormat);
} catch (Exception e) {
System.out.println("日期格式转换错误: " + e.getLocalizedMessage());
throw e; // 此处处理可根据代码内容打印日志
}
}
下面是判断是否同月
public static boolean isSameMonth(String month) {
// 先拼接为正常日期,再比较年月
String monthTmp = getMonthStart(month);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate date = LocalDate.parse(monthTmp, dateTimeFormatter);
Month giveMonth = date.getMonth();
int giveYear = date.getYear();
// 与当月对比,判断是否当前月份
LocalDate now = LocalDate.now();
Month atMonth = now.getMonth();
int atYear = now.getYear();
return giveMonth.equals(atMonth) && giveYear == atYear;
}
下面是获取月末日期,此处需要判断
public static String getMonthEnd(String month) {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
StringBuffer dataStr = new StringBuffer(month);
// 与当月对比,判断是否当前月份
boolean sameFlag = isSameMonth(month);
if (sameFlag) { // 如果同月直接返回当前时间
return LocalDate.now().format(dateFormat);
}
LocalDate atDate = LocalDate.parse(getMonthStart(month), dateFormat);
int atYear = atDate.getYear();
int atMonth = atDate.getMonthValue();
try {
// 根据月份拼接最后一天的日期
switch (atMonth) {
case 4:
case 6:
case 9:
case 11:
dataStr.append("30");
break;
case 2:
if (atYear % 4 == 0) {
dataStr.append("29");
} else {
dataStr.append("28");
}
break;
default:
dataStr.append("31");
}
LocalDate date = LocalDate.parse(dataStr);
return date.format(dateFormat);
} catch (Exception e) {
System.out.println("日期格式转换错误: " + e.getLocalizedMessage());
throw e;
}
}
这里的参数为LocalDate和排序规则来获取月份,1:由近及远,0:由远及近,可根据需求修改代码
public static List<String> getMonthList(LocalDate minTime, LocalDate maxTime, String sortType) {
List<LocalDate> monthListTmp = new ArrayList<>();
List<String > monthList = new ArrayList<>();
if (sortType.equals("1")) { // 由近及远
int minYear = minTime.getYear();
int maxYear = maxTime.getYear();
int minMonth = minTime.getMonthValue();
int maxMonth = maxTime.getMonthValue();
LocalDate localDateTmp = maxTime; // 临时日期
for (int i = maxYear; i > minYear-1; i--) { // 外层循环年份
if (i == maxYear) { // 内层循环最大年的月份
for (int j = maxMonth; j > 0; j--) {
monthListTmp.add(localDateTmp);
localDateTmp = localDateTmp.plusMonths(-1);
}
} else if (i == minYear) { // 内层循环最小年的月份
for (int j = 12; j > minMonth-1; j--) {
monthListTmp.add(localDateTmp);
localDateTmp = localDateTmp.plusMonths(-1);
}
} else {
for (int j = 12; j > 0; j--) { // 循环普通年的月份
monthListTmp.add(localDateTmp);
localDateTmp = localDateTmp.plusMonths(-1);
}
}
}
} else { // 由远及近
int minYear = minTime.getYear();
int maxYear = maxTime.getYear();
int minMonth = minTime.getMonthValue();
int maxMonth = maxTime.getMonthValue();
LocalDate localDateTmp = minTime; // 临时日期
for (int i = minYear; i < maxYear+1; i++) { // 外层循环年份
if (i == minYear) { // 内层循环最小年的月份
for (int j = minMonth; j < 13; j++) {
monthListTmp.add(localDateTmp);
localDateTmp = localDateTmp.plusMonths(1);
}
} else if (i == maxYear) { // 内层循环最大年的月份
for (int j = 1; j < maxMonth+1; j++) {
monthListTmp.add(localDateTmp);
localDateTmp = localDateTmp.plusMonths(1);
}
} else {
for (int j = 1; j < 13; j++) { // 循环普通年的月份
monthListTmp.add(localDateTmp);
localDateTmp = localDateTmp.plusMonths(1);
}
}
}
}
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM"); // 只取到月份
for (LocalDate date: monthListTmp) {
monthList.add(date.format(dateTimeFormatter));
}
return monthList;
}