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/DateTime.java

53 lines
2.5 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.SimpleDateFormat; // 导入SimpleDateFormat类用于格式化日期
import java.util.Calendar; // 导入Calendar类用于操作日期
import java.util.Date; // 导入Date类用于获取当前日期和时间
public class DateTime {
// 显示当前日期和时间,格式为"yyyy-MM-dd HH:mm:ss"
public static String show() {
SimpleDateFormat myFmt2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 创建日期格式化对象
Date now = new Date(); // 获取当前日期和时间
return myFmt2.format(now); // 返回格式化后的日期字符串
}
// 显示从当前日期开始n天后的日期和时间格式为"yyyy-MM-dd HH:mm:ss"
public static String show(int n) {
Date d = new Date(); // 获取当前日期和时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 创建日期格式化对象
String currdate = format.format(d); // 格式化当前日期
Calendar ca = Calendar.getInstance(); // 获取日历实例
ca.add(Calendar.DATE, n); // 在当前日期基础上增加n天
d = ca.getTime(); // 获取增加天数后的日期
String enddate = format.format(d); // 格式化增加天数后的日期
return enddate; // 返回格式化后的日期字符串
}
// 显示从当前日期开始n天后的日期格式为"yyyy-MM-dd"
public static String showDate(int n) {
Date d = new Date(); // 获取当前日期和时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 创建日期格式化对象
String currdate = format.format(d); // 格式化当前日期
Calendar ca = Calendar.getInstance(); // 获取日历实例
ca.add(Calendar.DATE, n); // 在当前日期基础上增加n天
d = ca.getTime(); // 获取增加天数后的日期
String enddate = format.format(d); // 格式化增加天数后的日期
return enddate; // 返回格式化后的日期字符串
}
// 显示从当前日期开始n天后的月日格式为"MM-dd"
public static String showMD(int n) {
Date d = new Date(); // 获取当前日期和时间
SimpleDateFormat format = new SimpleDateFormat("MM-dd"); // 创建日期格式化对象
String currdate = format.format(d); // 格式化当前日期
Calendar ca = Calendar.getInstance(); // 获取日历实例
ca.add(Calendar.DATE, n); // 在当前日期基础上增加n天
d = ca.getTime(); // 获取增加天数后的日期
String enddate = format.format(d); // 格式化增加天数后的日期
return enddate; // 返回格式化后的日期字符串
}
}