java.time常用方法汇总

java.time API 介绍

java.timeAPI 是从 Java 8 开始引入的一套强大的时间日期处理库,旨在替代老旧的 java.util.Datejava.util.Calendar。该 API 以更简洁、强大和灵活的方式处理日期、时间、时区以及时间间隔,适合各种全球化和时间计算的需求。

1.获取当前日期、时间、日期时间

提供了获取当前系统时间的多种方式,包括不含时间的日期、不含日期的时间、完整的日期时间,以及带时区的日期时间。

// LocalDate: 获取当前日期(不含时间)
LocalDate currentDate = LocalDate.now();

// LocalTime: 获取当前时间(不含日期)
LocalTime currentTime = LocalTime.now();

// LocalDateTime: 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();

// ZonedDateTime: 获取带时区的当前日期和时间
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();

// Instant: 获取当前的瞬时时间点(用于时间戳)
Instant currentInstant = Instant.now();
2.创建指定的日期、时间、日期时间

根据需求创建指定的日期、时间、带时区的日期时间,甚至可以直接通过时间戳生成时间点。日期必须对年份和月份有效,否则将引发DateTimeException异常。

// LocalDate: 创建指定的日期
LocalDate date = LocalDate.of(2024, 9, 23);  // 年, 月, 日

// LocalTime: 创建指定的时间
LocalTime time = LocalTime.of(14, 30, 0);    // 时, 分, 秒

// LocalDateTime: 创建指定的日期和时间
LocalDateTime dateTime = LocalDateTime.of(2024, 9, 23, 14, 30); // 年, 月, 日, 时, 分

// ZonedDateTime: 创建指定时区的日期和时间
ZonedDateTime zonedDateTime = ZonedDateTime.of(2024, 9, 23, 14, 30, 0, 0, ZoneId.of("Asia/Shanghai"));

// ZoneId: 获取指定时区
ZoneId zoneId = ZoneId.of("America/New_York");

// Instant: 创建指定的时间点
Instant instant = Instant.ofEpochSecond(1609459200L); // 秒级时间戳
3.解析字符串为日期、时间、日期时间

java.time 提供了从字符串解析日期、时间的能力,支持标准格式和自定义格式,非常适合从文本或文件中读取时间信息。

// LocalDate: 从字符串解析日期
LocalDate parsedDate = LocalDate.parse("2024-09-23");

// LocalTime: 从字符串解析时间
LocalTime parsedTime = LocalTime.parse("14:30:00");

// LocalDateTime: 从字符串解析日期和时间
LocalDateTime parsedDateTime = LocalDateTime.parse("2024-09-23T14:30:00");

// DateTimeFormatter: 使用自定义格式解析日期时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime formattedDateTime = LocalDateTime.parse("2024-09-23 14:30:00", formatter);
4.格式化日期、时间、日期时间为字符串
// LocalDate: 格式化日期为字符串
String dateString = currentDate.format(DateTimeFormatter.ISO_DATE); // 输出格式: 2024-09-23

// LocalTime: 格式化时间为字符串
String timeString = currentTime.format(DateTimeFormatter.ISO_TIME); // 输出格式: 14:30:00

// LocalDateTime: 格式化日期和时间为字符串
String dateTimeString = currentDateTime.format(DateTimeFormatter.ISO_DATE_TIME); // 输出格式: 2024-09-23T14:30:00

// DateTimeFormatter: 使用自定义格式
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedString = currentDateTime.format(customFormatter); // 输出格式: 2024-09-23 14:30:00  
5.日期、时间的加减运算
// LocalDate: 增加或减少日期
LocalDate newDate = currentDate.plusDays(5);   // 增加5天
LocalDate earlierDate = currentDate.minusMonths(2); // 减少2个月

// LocalTime: 增加或减少时间
LocalTime newTime = currentTime.plusHours(2);  // 增加2小时
LocalTime earlierTime = currentTime.minusMinutes(30); // 减少30分钟

// LocalDateTime: 增加或减少日期时间
LocalDateTime newDateTime = currentDateTime.plusWeeks(1); // 增加1周
LocalDateTime earlierDateTime = currentDateTime.minusDays(3); // 减少3天

// ZonedDateTime: 带时区的加减
ZonedDateTime newZonedDateTime = currentZonedDateTime.plusYears(1); // 增加1年
ZonedDateTime earlierZonedDateTime = currentZonedDateTime.minusHours(6); // 减少6小时

// Instant: 增加或减少秒数
Instant newInstant = currentInstant.plusSeconds(3600); // 增加1小时(3600秒)
6.日期、时间的比较
// LocalDate: 比较日期
boolean isBefore = currentDate.isBefore(LocalDate.of(2025, 1, 1)); // 判断当前日期是否在2025-01-01之前
boolean isAfter = currentDate.isAfter(LocalDate.of(2020, 1, 1));   // 判断当前日期是否在2020-01-01之后

// LocalTime: 比较时间
boolean isEarlier = currentTime.isBefore(LocalTime.of(16, 0)); // 判断当前时间是否在16:00之前

// LocalDateTime: 比较日期时间
boolean isLater = currentDateTime.isAfter(LocalDateTime.of(2023, 9, 23, 12, 0)); // 判断当前时间是否在给定时间之后

// ZonedDateTime: 比较带时区的日期时间
boolean zonedIsBefore = currentZonedDateTime.isBefore(ZonedDateTime.of(2024, 9, 23, 12, 0, 0, 0, ZoneId.of("Asia/Shanghai")));
7.获取年、月、日、时、分、秒

获取日期有关的值。

// LocalDate: 获取年、月、日
int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int day = currentDate.getDayOfMonth();

// LocalTime: 获取时、分、秒
int hour = currentTime.getHour();
int minute = currentTime.getMinute();
int second = currentTime.getSecond();

// LocalDateTime: 获取年、月、日、时、分、秒
int dateTimeYear = currentDateTime.getYear();
int dateTimeMonth = currentDateTime.getMonthValue();
int dateTimeDay = currentDateTime.getDayOfMonth();
int dateTimeHour = currentDateTime.getHour();
int dateTimeMinute = currentDateTime.getMinute();
int dateTimeSecond = currentDateTime.getSecond();

// ZonedDateTime: 获取年、月、日、时、分、秒、时区
ZoneId zone = currentZonedDateTime.getZone();
8.计算时间间隔
// Period: 计算两个日期之间的年、月、日差异
Period period = Period.between(LocalDate.of(2020, 1, 1), LocalDate.of(2024, 9, 23));
int yearsBetween = period.getYears();
int monthsBetween = period.getMonths();
int daysBetween = period.getDays();

// Duration: 计算两个时间或日期时间之间的时、分、秒差异
Duration duration = Duration.between(LocalTime.of(14, 0), LocalTime.of(16, 30));
long hoursBetween = duration.toHours();   // 获取小时差
long minutesBetween = duration.toMinutes(); // 获取分钟差
9.时区相关操作
// ZoneId: 获取时区
ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
ZoneId newYorkZone = ZoneId.of("America/New_York");

// ZonedDateTime: 转换时区,保持相同瞬时时间
ZonedDateTime newYorkTime = currentZonedDateTime.withZoneSameInstant(ZoneId.of("America/New_York"));

// 获取所有可用的时区ID
Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
10.判断闰年
// LocalDate: 判断是否是闰年
boolean isLeapYear = currentDate.isLeapYear();

转载自开思通智网:https://w3.opensnn.com/os/article/10001448

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值