用java写一个日期工具类
时间: 2024-05-02 08:21:54 浏览: 97
以下是一个简单的Java日期工具类,包含常用的日期操作方法:
```
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
// 格式化日期
public static String formatDate(Date date, String pattern) {
DateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
}
// 获取当前日期
public static Date getCurrentDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
// 获取当前日期字符串
public static String getCurrentDateString(String pattern) {
Date currentDate = getCurrentDate();
return formatDate(currentDate, pattern);
}
// 获取给定日期的年份
public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
// 获取给定日期的月份
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}
// 获取给定日期的天数
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
// 获取给定日期的小时数
public static int getHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
// 获取给定日期的分钟数
public static int getMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
}
// 获取给定日期的秒数
public static int getSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
}
// 获取给定日期的毫秒数
public static long getMillis(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
// 获取给定日期是一周中的第几天
public static int getDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}
// 获取给定日期是一年中的第几天
public static int getDayOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_YEAR);
}
// 获取给定日期所在月份的天数
public static int getDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
// 在给定日期的基础上加上/减去指定的年数
public static Date addYears(Date date, int years) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, years);
return calendar.getTime();
}
// 在给定日期的基础上加上/减去指定的月数
public static Date addMonths(Date date, int months) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, months);
return calendar.getTime();
}
// 在给定日期的基础上加上/减去指定的天数
public static Date addDays(Date date, int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, days);
return calendar.getTime();
}
// 在给定日期的基础上加上/减去指定的小时数
public static Date addHours(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
// 在给定日期的基础上加上/减去指定的分钟数
public static Date addMinutes(Date date, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minutes);
return calendar.getTime();
}
// 在给定日期的基础上加上/减去指定的秒数
public static Date addSeconds(Date date, int seconds) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, seconds);
return calendar.getTime();
}
// 在给定日期的基础上加上/减去指定的毫秒数
public static Date addMillis(Date date, long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MILLISECOND, (int) millis);
return calendar.getTime();
}
}
```
阅读全文