Java 实现两个日期之间相差多少天
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * @author zfy * @version 1.0 * @date 2022/12/22 18:43 * @Description: */public class DateUtils {public static void main(String[] args) throws ParseException {String date1str = "2021/02/22";String date2str = "2021/02/22";int dayNum = daysBetween(date1str, date2str);System.out.println(dayNum);}/** * JAVA计算两个日期相差多少天 * @param date1str * @param date2str * @return */public static int daysBetween(String date1str,String date2str) throws ParseException {int count = 0;if("".equals(date1str) || date1str == null || "".equals(date2str) || date2str == null) {return count;}SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");Date date1 = format.parse(date1str);Date date2 = format.parse(date2str);count = Math.abs((int) ((date1.getTime() - date2.getTime()) / (1000*3600*24)));return count;}}
运行结果: