You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
library_manage_system/src/javabean/CompareDate.java

26 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package javabean;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CompareDate {
// 定义一个静态方法show接收两个字符串参数Str1和Str2返回两个日期之间的天数差
public static long show(String Str1, String Str2) {
long between = 0; // 初始化between变量用于存储两个日期之间的毫秒数差
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 创建日期格式化对象,指定日期格式
try {
Date date1 = format.parse(Str1); // 将字符串Str1解析为Date对象date1
Date date2 = format.parse(Str2); // 将字符串Str2解析为Date对象date2
between = (date2.getTime() - date1.getTime()); // 计算两个日期之间的毫秒数差
System.out.println(date1); // 打印第一个日期
System.out.println(date2); // 打印第二个日期
} catch (ParseException e) {
// 捕获并处理ParseException异常
e.printStackTrace(); // 打印异常堆栈信息
}
long days = between / (24 * 60 * 60 * 1000); // 将毫秒数差转换为天数
return days; // 返回两个日期之间的天数差
}
}