专业的JAVA编程教程与资源

网站首页 > java教程 正文

现代与传统日期时间处理工具概览(时间 日期)

temp10 2024-11-11 13:53:39 java教程 9 ℃ 0 评论

一: 农历日期-ChineseDate

ChineseDate的Java类,它专门用于处理农历(阴历)相关的日期信息。通过这个类,用户可以获取到农历日期的多种信息,包括生肖、天干地支和传统节日等。ChineseDate类提供了两种构造方法,一种接受农历年月日的参数,另一种接受公历日期的参数。

基本使用方法如下:

现代与传统日期时间处理工具概览(时间 日期)

  • 通过公历日期创建ChineseDate对象,并获取农历月份名称(如“正月”)、农历日期(如“初一”)、天干地支(如“庚子”)、生肖(如“鼠”)和传统节日(如“春节”)。
  • 通过公历日期创建ChineseDate对象,使用getCyclicalYMD方法获取对应的天干地支信息(如“庚子年甲申月癸卯日”)。
    /**
     * @desc:日期选择农历正月更加具有代表性
     * @param args @Return: void
     * @author: jones
     * @date: 2024/4/4 19:53
     */

    // 通过农历构建
    Console.log("==========================通过农历构建============================");
    ChineseDate chineseDate = new ChineseDate(2024, 3, 4);
    Console.log(chineseDate);

    // 通过公历构建
    Console.log("==========================通过公历构建============================");
    ChineseDate chineseDate2 = new ChineseDate(DateUtil.parseDate("2024-03-04"));
    Console.log(chineseDate2);

    Console.log("==========================以下测试============================");
    // 通过公历构建
    ChineseDate date = new ChineseDate(DateUtil.parseDate("2024-03-04"));
    // 一月
    String chineseMonth = date.getChineseMonth();
    Console.log("chineseMonth:" + chineseMonth);
    // 正月
    String chineseMonthName = date.getChineseMonthName();
    Console.log("chineseMonthName:" + chineseMonthName);
    // 廿四
    String chineseDay = date.getChineseDay();
    Console.log("chineseDay:" + chineseDay);
    // 甲辰
    String cyclical = date.getCyclical();
    Console.log("cyclical:" + cyclical);
    // 生肖:龙
    String chineseZodiac = date.getChineseZodiac();
    Console.log("chineseZodiac:" + chineseZodiac);
    // 传统节日(部分支持,逗号分隔):春节
    String festivals = date.getFestivals();
    Console.log("festivals:" + festivals);
    // 甲辰龙年 正月廿四
    String stringNormal = date.toString();
    Console.log("stringNormal:" + stringNormal);

    // 获取天干地支
    String cyclicalYMD = date.getCyclicalYMD();
    Console.log("cyclicalYMD:" + cyclicalYMD);

二:LocalDateTime工具-LocalDateTimeUtil

LocalDateTimeUtil的Java工具类,它对JDK 8及以上版本的日期API进行了封装,提供了一系列的日期操作方法,包括解析、格式化和转换等。

主要功能如下:

  • 日期转换:将字符串或Date对象转换为LocalDateTime,或者将时间戳转换为LocalDateTime
/** 日期转换 */
String dateStr = "2024-04-04T19:23:56";
DateTime dt = DateUtil.parse(dateStr);
// Date对象转换为LocalDateTime
LocalDateTime of = LocalDateTimeUtil.of(dt);
Console.log("of1:" + of);
// 时间戳转换为LocalDateTime(不设置时区以国际时区为准)
of = LocalDateTimeUtil.ofUTC(dt.getTime());
Console.log("of2:" + of);
  • 日期字符串解析:解析ISO格式的时间字符串或自定义格式的时间字符串,并支持将日期时间字符串解析为LocalDate
  • 日期格式化:将LocalDateTime对象格式化为指定格式的字符串。
  • 日期偏移:对LocalDateTime对象进行增加或减少时间的操作,如增加一天。
/** 日期字符串解析 */
// 解析ISO时间
LocalDateTime localDateTime = LocalDateTimeUtil.parse("2024-04-04T19:23:56");
Console.log("parse1:" + localDateTime);

// 解析自定义格式时间
localDateTime = LocalDateTimeUtil.parse("2024-04-04", DatePattern.NORM_DATE_PATTERN);
Console.log("parse2:" + localDateTime);

LocalDate localDate = LocalDateTimeUtil.parseDate("2020-01-23");

// 解析日期时间为LocalDate,时间部分舍弃
localDate = LocalDateTimeUtil.parseDate("2024-04-04T19:23:56", DateTimeFormatter.ISO_DATE_TIME);
Console.log("parseDate:" + localDate);
/** 日期偏移 */
final LocalDateTime localDateTime2 = LocalDateTimeUtil.parse("2024-04-04T19:23:56");
// 增加一天
// 2024-04-05T19:23:56
LocalDateTime offset = LocalDateTimeUtil.offset(localDateTime2, 1, ChronoUnit.DAYS);
Console.log("offset1:" + offset);
// 2024-04-03T00:00
offset = LocalDateTimeUtil.offset(localDateTime, -1, ChronoUnit.DAYS);
Console.log("offset2:" + offset);
  • 计算时间间隔:计算两个LocalDateTime对象之间的时间间隔,例如天数。
  • 一天的开始和结束:获取LocalDateTime对象所在日期的开始(午夜)和结束(晚上11:59:59.999999999)时间点。
/** 计算时间间隔 */
LocalDateTime start = LocalDateTimeUtil.parse("2023-04-04T00:00:00");
LocalDateTime end = LocalDateTimeUtil.parse("2024-04-04T00:00:00");
Duration between = LocalDateTimeUtil.between(start, end);

// 366
long days = between.toDays();
Console.log("between1:" + days);

LocalDateTime localDateTime3 = LocalDateTimeUtil.parse("2024-04-04T19:23:56");

// 024-04-04T00:00
LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(localDateTime3);
Console.log("beginOfDay:" + beginOfDay);
// 2024-04-04T23:59:59.999999999
LocalDateTime endOfDay = LocalDateTimeUtil.endOfDay(localDateTime3);
Console.log("endOfDay:" + endOfDay);

这两个工具类为Java开发者在处理日期和时间方面提供了便利,使得相关的操作更加简洁和高效。ChineseDate专注于农历日期的特性,而LocalDateTimeUtil则提供了更通用的日期时间操作。可以根据自身需求选择合适的工具类来简化代码和逻辑。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表