JAVA日期相关操作
-
计算两个日期相差的天数
/** * 计算两个日期相差的 天数 * @param smdate String类型初始时间 * @param bdate String类型截至时间 * @return Integer */ public static Integer dayCompare(String smdate, String bdate) throws Exception { Date start = sdf.parse(smdate); Date end = sdf.parse(bdate); Calendar cal = Calendar.getInstance(); cal.setTime(start); long time1 = cal.getTimeInMillis(); cal.setTime(end); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); }
-
计算两个日期之间的所有日期
/** * @param startDay 开始时间 * @param endDay 结束时间 * @return List<String> * @author bigfaceYang * 获取两个日期之间天数 * @date 2021/11/11 16:09 */ public static List<String> getDays(String startDay, String endDay) throws Exception { List<String> list = new ArrayList<>(); Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(startDay)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), min.get(Calendar.DATE), 0, 0, 0); max.setTime(sdf.parse(endDay)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), max.get(Calendar.DATE), 23, 59, 59); Calendar curr = min; while (curr.before(max)) { list.add(sdf.format(curr.getTime())); curr.add(Calendar.DATE, 1); } return list; }
运行结果截图:
- 获取当年所有的月份(yyyy-MM)
/** * 获取当年所有月 * * @return */ public static String[] getYearFullMonth() { String[] array = new String[12]; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, Integer.parseInt(year.format(new Date()))); cal.set(Calendar.MONTH, 0); for (int i = 0; i < 12; i++) { array[i] = sdf.format(cal.getTime()); cal.add(Calendar.MONTH, 1); } return array; }
- 两个月份之间的月份(yyyy-MM)
/** * @author bigfaceYang * 获取两个日期间年月集合 * @param minDate 最小时间 2023-01 * @param maxDate 最大时间 2024-05 * @return 日期集合 格式为 年-月 * @throws Exception */ public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception { ArrayList<String> result = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); 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; }
- 根据年份获取年份下所有月份
/**
* 根据年份获取年份下所有月份
* @param year
* @return
*/
public static String[] getYearFullMonth(String year){
String[] array = new String[12];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,Integer.parseInt(year));
cal.set(Calendar.MONTH, 0);
for(int i=0;i<12;i++){
array[i] = sdf.format(cal.getTime());
cal.add(Calendar.MONTH, 1);
}
return array;
}
运行结果:
- 获取N个月之前的数据
/**
* @author wmw
* 获取当前N个月之前或之后的月份 当num=-1 代表前一个月的数据 num=1 代表后一个月的数据
* @date 2022/9/14 15:27
* @return null
*/
public static String getLastMonth(Integer num){
SimpleDateFormat month = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH,num);
Date time = c.getTime();
return month.format(time);
}
@SneakyThrows
public static void main(String[] args) {
String lastMonth = getLastMonth(2);
System.out.println(lastMonth);
//运行结果为:2023-10
}
未完持续更新中。。。