(全文目录:)
开篇语
哈喽,各位小伙伴们,你们好呀,我是喵手。运营社区:C站/掘金/腾讯云/阿里云/华为云/51CTO;欢迎大家常来逛逛
今天我要给大家分享一些自己日常学习到的一些知识点,并以文字的形式跟大家一起交流,互相学习,一个人虽可以走的更快,但一群人可以走的更远。
我是一名后端开发爱好者,工作日常接触到最多的就是Java语言啦,所以我都尽量抽业余时间把自己所学到所会的,通过文章的形式进行输出,希望以这种方式帮助到更多的初学者或者想入门的小伙伴们,同时也能对自己的技术进行沉淀,加以复盘,查缺补漏。
小伙伴们在批阅的过程中,如果觉得文章不错,欢迎点赞、收藏、关注哦。三连即是对作者我写作道路上最好的鼓励与支持!
前序
在Java 8中,Java引入了全新的日期与时间API (java.time
),该API解决了旧版java.util.Date
和java.util.Calendar
类中的许多设计缺陷。新的日期与时间API是不可变的、线程安全的,并且更容易使用。这个API的引入使得我们可以更加高效和灵活地处理日期和时间。
今天,我们将重点介绍LocalDate
、LocalTime
、LocalDateTime
的使用,Period
和Duration
类的操作,最后还将探讨DateTimeFormatter
的格式化功能,帮助你更好地应对日期和时间相关的操作。
前言
在处理日期和时间时,很多开发者可能已经习惯了使用Date
和Calendar
类。但这些类在设计上有很多不足之处,尤其在进行复杂的日期计算时,它们的使用变得十分繁琐。为了解决这些问题,Java 8引入了全新的日期和时间API——java.time
,它具有更加简洁、直观的接口,并且具备强大的功能。
在这篇文章中,我们将介绍新的日期与时间API的核心类及其用法,包括LocalDate
、LocalTime
、LocalDateTime
,以及如何使用Period
和Duration
进行日期和时间的计算,最后讲解如何使用DateTimeFormatter
进行日期时间的格式化。
第一部分:LocalDate
、LocalTime
、LocalDateTime
的使用
1.1 LocalDate
:表示日期(无时间)
LocalDate
表示一个日期(年、月、日),没有时分秒部分。它适用于日期相关的操作,比如计算某一天、获取当前日期等。
示例:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate today = LocalDate.now();
System.out.println("Today: " + today); // 输出:例如:2025-05-19
// 创建特定日期
LocalDate specificDate = LocalDate.of(2023, 5, 19);
System.out.println("Specific Date: " + specificDate); // 输出:2023-05-19
// 获取日期的各个部分
System.out.println("Year: " + today.getYear()); // 输出:2025
System.out.println("Month: " + today.getMonth()); // 输出:MAY
System.out.println("Day of Month: " + today.getDayOfMonth()); // 输出:19
}
}
解释:
LocalDate.now()
获取当前日期。LocalDate.of(year, month, day)
创建一个指定日期。- 通过
getYear()
、getMonth()
、getDayOfMonth()
获取日期的各个部分。
1.2 LocalTime
:表示时间(无日期)
LocalTime
表示时间(时、分、秒、纳秒),不包含日期部分。它适用于时间相关的操作,比如计算某个时刻、获取当前时间等。
示例:
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
// 获取当前时间
LocalTime now = LocalTime.now();
System.out.println("Current Time: " + now); // 输出:例如:13:45:30.123456
// 创建特定时间
LocalTime specificTime = LocalTime.of(14, 30, 0);
System.out.println("Specific Time: " + specificTime); // 输出:14:30
// 获取时间的各个部分
System.out.println("Hour: " + now.getHour()); // 输出:13
System.out.println("Minute: " + now.getMinute()); // 输出:45
System.out.println("Second: " + now.getSecond()); // 输出:30
}
}
解释:
LocalTime.now()
获取当前时间。LocalTime.of(hour, minute, second)
创建一个指定时间。- 使用
getHour()
、getMinute()
、getSecond()
获取时间的各个部分。
1.3 LocalDateTime
:表示日期和时间
LocalDateTime
结合了LocalDate
和LocalTime
,表示完整的日期和时间,没有时区信息。它适用于同时需要日期和时间的场景。
示例:
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("Current DateTime: " + now); // 输出:例如:2025-05-19T13:45:30.123456
// 创建特定的日期和时间
LocalDateTime specificDateTime = LocalDateTime.of(2023, 5, 19, 14, 30);
System.out.println("Specific DateTime: " + specificDateTime); // 输出:2023-05-19T14:30
// 获取日期和时间的各个部分
System.out.println("Year: " + now.getYear()); // 输出:2025
System.out.println("Month: " + now.getMonth()); // 输出:MAY
System.out.println("Day: " + now.getDayOfMonth()); // 输出:19
System.out.println("Hour: " + now.getHour()); // 输出:13
}
}
解释:
LocalDateTime.now()
获取当前日期和时间。LocalDateTime.of(year, month, day, hour, minute)
创建一个指定日期和时间。- 通过
getYear()
、getMonth()
、getHour()
等方法获取日期时间的各个部分。
第二部分:Period
、Duration
类的操作
Period
和Duration
类用于表示两个日期/时间之间的差异。Period
用于日期的差异,Duration
用于时间的差异。
2.1 Period
类:表示日期间隔
Period
表示的是两个LocalDate
之间的日期差异,通常用于表示年、月、日之间的差异。
示例:
import java.time.LocalDate;
import java.time.Period;
public class PeriodExample {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2025, 5, 19);
// 计算日期差异
Period period = Period.between(startDate, endDate);
System.out.println("Period: " + period); // 输出:Period: P2Y4M18D
System.out.println("Years: " + period.getYears()); // 输出:2
System.out.println("Months: " + period.getMonths()); // 输出:4
System.out.println("Days: " + period.getDays()); // 输出:18
}
}
解释:
Period.between(startDate, endDate)
计算两个日期之间的差异,返回一个Period
对象。getYears()
、getMonths()
、getDays()
分别返回年、月、日的差异。
2.2 Duration
类:表示时间间隔
Duration
表示的是两个LocalTime
或LocalDateTime
之间的时间差异,通常用于表示秒和纳秒的差异。
示例:
import java.time.LocalTime;
import java.time.Duration;
public class DurationExample {
public static void main(String[] args) {
LocalTime startTime = LocalTime.of(14, 30);
LocalTime endTime = LocalTime.of(16, 45);
// 计算时间差异
Duration duration = Duration.between(startTime, endTime);
System.out.println("Duration: " + duration); // 输出:Duration: PT2H15M
System.out.println("Hours: " + duration.toHours()); // 输出:2
System.out.println("Minutes: " + duration.toMinutes()); // 输出:135
}
}
解释:
Duration.between(startTime, endTime)
计算两个时间之间的差异,返回一个Duration
对象。toHours()
、toMinutes()
分别返回时间差异的小时数和分钟数。
第三部分:DateTimeFormatter
的格式化功能
DateTimeFormatter
类用于将日期和时间格式化为字符串,或者将字符串解析为日期时间对象。它提供了强大的自定义格式化功能,支持多种日期时间格式。
3.1 格式化日期时间
使用DateTimeFormatter
将LocalDate
、LocalTime
、LocalDateTime
等格式化为指定的字符串格式。
示例:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
// 定义一个日期格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 格式化日期
String formattedDate = date.format(formatter);
System.out.println("Formatted Date: " + formattedDate); // 输出:2025-05-19
}
}
解释:
DateTimeFormatter.ofPattern("yyyy-MM-dd")
创建一个自定义格式化器。date.format(formatter)
将LocalDate
格式化为字符串。
3.2 解析字符串为日期时间对象
DateTimeFormatter
不仅可以格式化日期时间,也可以将符合特定格式的字符串解析为日期时间对象。
示例:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
String dateStr = "2025-05-19";
// 定义一个日期格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 解析字符串为日期对象
LocalDate date = LocalDate.parse(dateStr, formatter);
System.out.println("Parsed Date: " + date); // 输出:2025-05-19
}
}
解释:
DateTimeFormatter.ofPattern("yyyy-MM-dd")
创建一个自定义格式化器。LocalDate.parse(dateStr, formatter)
将字符串解析为LocalDate
对象。
总结
Java新的日期与时间API (java.time
) 提供了比旧版Date
和Calendar
更强大、灵活且易于使用的功能。通过使用LocalDate
、LocalTime
、LocalDateTime
,我们可以方便地处理日期和时间数据;Period
和Duration
帮助我们进行日期和时间的差异计算;DateTimeFormatter
则提供了强大的日期和时间格式化功能。
掌握这些新的API,可以让你在日期和时间的处理上更加得心应手,避免繁琐的日期计算,编写更加简洁和可读性更高的代码。
... ...
文末
好啦,以上就是我这期的全部内容,如果有任何疑问,欢迎下方留言哦,咱们下期见。
... ...
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!
wished for you successed !!!
⭐️若喜欢我,就请关注我叭。
⭐️若对您有用,就请点赞叭。 ⭐️若有疑问,就请评论留言告诉我叭。
版权声明:本文由作者原创,转载请注明出处,谢谢支持!