【知识分享】Java日期工具类

以下是一个示例的Java日期工具类,其中包含了常见的日期操作和格式化方法:

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateUtils {

private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

/**

* 获取当前日期时间

*

* @return 当前日期时间

*/

public static Date getCurrentDateTime() {

return new Date();

}

/**

* 格式化日期时间为字符串

*

* @param date 日期时间对象

* @param pattern 日期时间格式

* @return 格式化后的日期时间字符串

*/

public static String formatDateTime(Date date, String pattern) {

DateFormat dateFormat = new SimpleDateFormat(pattern);

return dateFormat.format(date);

}

/**

* 格式化日期时间为默认格式字符串(yyyy-MM-dd HH:mm:ss)

*

* @param date 日期时间对象

* @return 格式化后的日期时间字符串

*/

public static String formatDateTime(Date date) {

return formatDateTime(date, DEFAULT_PATTERN);

}

/**

* 将字符串解析为日期时间对象

*

* @param dateTimeStr 日期时间字符串

* @param pattern 日期时间格式

* @return 解析后的日期时间对象

* @throws ParseException 解析异常

*/

public static Date parseDateTime(String dateTimeStr, String pattern) throws ParseException {

DateFormat dateFormat = new SimpleDateFormat(pattern);

return dateFormat.parse(dateTimeStr);

}

/**

* 将字符串解析为日期时间对象(使用默认格式:yyyy-MM-dd HH:mm:ss)

*

* @param dateTimeStr 日期时间字符串

* @return 解析后的日期时间对象

* @throws ParseException 解析异常

*/

public static Date parseDateTime(String dateTimeStr) throws ParseException {

return parseDateTime(dateTimeStr, DEFAULT_PATTERN);

}

/**

* 获取指定日期的年份

*

* @param date 日期时间对象

* @return 年份

*/

public static int getYear(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

return calendar.get(Calendar.YEAR);

}

/**

* 获取指定日期的月份

*

* @param date 日期时间对象

* @return 月份(1-12)

*/

public static int getMonth(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

return calendar.get(Calendar.MONTH) + 1;

}

/**

* 获取指定日期的天数

*

* @param date 日期时间对象

* @return 天数

*/

public static int getDay(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

return calendar.get(Calendar.DAY_OF_MONTH);

}

/**

* 获取指定日期的小时

*

* @param date 日期时间对象

* @return 小时

*/

public static int getHour(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

return calendar.get(Calendar.HOUR_OF_DAY);

}

/**

* 获取指定日期的分钟

*

* @param date 日期时间对象

* @return 分钟

*/

public static int getMinute(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

return calendar.get(Calendar.MINUTE);

}

/**

* 获取指定日期的秒数

*

* @param date 日期时间对象

* @return 秒数

*/

public static int getSecond(Date date) {

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

return calendar.get(Calendar.SECOND);

}

}

这个示例的日期工具类提供了获取当前日期时间、格式化日期时间、解析日期时间、获取年份、月份、天数、小时、分钟和秒数等常用操作。您可以根据实际需求对其进行扩展和修改。

使用示例:

public class Main {

public static void main(String[] args) {

Date currentDateTime = DateUtils.getCurrentDateTime();

System.out.println("当前日期时间:" + DateUtils.formatDateTime(currentDateTime));

String dateTimeStr = "2023-12-01 15:30:00";

try {

Date parsedDateTime = DateUtils.parseDateTime(dateTimeStr);

System.out.println("解析后的日期时间:" + DateUtils.formatDateTime(parsedDateTime));

} catch (ParseException e) {

e.printStackTrace();

}

int year = DateUtils.getYear(currentDateTime);

int month = DateUtils.getMonth(currentDateTime);

int day = DateUtils.getDay(currentDateTime);

int hour = DateUtils.getHour(currentDateTime);

int minute = DateUtils.getMinute(currentDateTime);

int second = DateUtils.getSecond(currentDateTime);

System.out.println("当前日期时间:"

  • year + "年" + month + "月" + day + "日 "

  • hour + "时" + minute + "分" + second + "秒");

}

}

这个示例中,我们首先获取当前日期时间,并将其格式化为字符串进行输出。然后,使用解析方法将字符串解析为日期时间对象,并再次格式化输出。最后,使用获取方法获取当前日期时间的各个部分(年、月、日、小时、分钟、秒),并进行输出。

相关推荐
一辉ComeOn2 分钟前
【大数据高并发核心场景实战】缓存层 - 写缓存
java·大数据·redis·缓存
洲星河ZXH3 分钟前
Java,String类
java·开发语言
青衫码上行3 分钟前
【JavaWeb 学习 | 第16篇】JPS介绍和基本语法
java·学习·web·jsp
xcLeigh4 分钟前
【新】Rust入门:基础语法应用
开发语言·算法·rust
nono牛5 分钟前
C++ 语言全面教程 (基础入门)
java·jvm·c++
冬夜戏雪5 分钟前
【Java学习日记】【2025.12.2】【2/60】
java·开发语言·学习
小年糕是糕手8 分钟前
【C++同步练习】类和对象(一)
java·开发语言·javascript·数据结构·c++·算法·排序算法
txxzjmzlh8 分钟前
类和对象(下)
开发语言·c++
运维小文8 分钟前
Centos7部署.net8和升级libstdc++
开发语言·c++·.net
Zzzzzxl_8 分钟前
深入理解Java JVM中的垃圾回收器
java·jvm·编程·性能调优·垃圾回收