获取两个日期之间的所有月份
public static ArrayList<String> getMiddleMonth(String startTime, String endTime) throws ParseException {
ArrayList<String> result = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
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);
}
return result;
}
获取两个日期之间的日期
public static List<LocalDate> getMiddleDate(String startTime, String endTime){
LocalDate start = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate end = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
List<LocalDate> localDateList = new ArrayList<>();
long length = end.toEpochDay() - start.toEpochDay();
for (long i = length; i >= 0; i--) {
LocalDate localDate = end.minusDays(i);
localDateList.add(localDate);
}
return localDateList;
}
输入02月28日-03月05日
的日期格式计算出两个日期之间相隔的天数
/**
* 计算两个日期之间相隔的天数
* @param date 日期
* @return 相隔的天数
*/
public static int dayCompute(String date) {
String[] split = date.split("-");
String month_1 = "", month_2 = "", day_1 = "", day_2 = "";
int differenceDay = 0;
for (int u = 0; u < split.length; u++) {
String[] split_1 = split[u].split("月");
for (int i = 0; i < split_1.length; i++) {
if (u == 0) {
if (i == 0) {
month_1 = split_1[i];
} else {
if (split_1[i].contains("日")) {
String[] split_2 = split_1[i].split("日");
for (int o = 0; o < split_2.length; o++) {
day_1 = split_2[o];
}
}
}
} else {
if (i == 0) {
month_2 = split_1[i];
} else {
if (split_1[i].contains("日")) {
String[] split_2 = split_1[i].split("日");
for (int o = 0; o < split_2.length; o++) {
if (o == 0) {
day_2 = split_2[o];
}
}
}
}
}
}
}
if (month_1.equals(month_2) && !(month_1.equals("") && month_2.equals(""))) {
differenceDay = (Integer.parseInt(day_2) - Integer.parseInt(day_1)) + 1;
} else {
if (!(month_1.equals("") && month_2.equals(""))) {
int tempDay_1 = getDays(month_1);
int differenceDay_1 = tempDay_1 - Integer.parseInt(day_1) + 1;
differenceDay = differenceDay_1 + Integer.parseInt(day_2);
}
}
return differenceDay;
}
public static int getDays(String month) {
int day;
switch (month) {
case "01": case "03": case "05": case "07": case "08": case "10": case "12":
day = 31;
break;
case "04": case "06": case "09": case "11":
day = 30;
break;
case "02":
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
day = 29;
} else {
day = 28;
}
break;
default:
day = 0;
}
return day;
}
因业务需要查当月、上个月、上上个月的相关数据。
代码:
import java.text.SimpleDateFormat;
import java.util.*;
public class Test {
public static void main(String[] args){
Date date = new Date();
String endDate = dateReplace(date,-2);
System.out.println(endDate);
String startDate = stringReplace(endDate);
System.out.println(startDate);
}
static private String dateReplace(Date date,int addNumber){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
//2代表月份的意思,addNumber代表在当前的日期添加或减去一个月
gc.add(2, addNumber);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
return dateFormat.format(gc.getTime());
}
static private String stringReplace(String string){
StringBuilder stringBuilder = new StringBuilder(string);
return stringBuilder.replace(8,10,"01").toString();
}
}
结果:
当前日期加七天
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
long time = date.getTime(); // 得到指定日期的毫秒数
long day = 7 * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
time += day; // 相加得到新的毫秒数
Date date1 = new Date(time);
System.out.println(dateFormat.format(date));
System.out.println(dateFormat.format(date1));