全文目录:
开篇语
今天我要给大家分享一些自己日常学习到的一些知识点,并以文字的形式跟大家一起交流,互相学习,一个人虽可以走的更快,但一群人可以走的更远。
我是一名后端开发爱好者,工作日常接触到最多的就是Java语言啦,所以我都尽量抽业余时间把自己所学到所会的,通过文章的形式进行输出,希望以这种方式帮助到更多的初学者或者想入门的小伙伴们,同时也能对自己的技术进行沉淀,加以复盘,查缺补漏。
小伙伴们在批阅的过程中,如果觉得文章不错,欢迎点赞、收藏、关注哦。三连即是对作者我写作道路上最好的鼓励与支持!
前言
在 Java 8 中,Java 引入了全新的日期和时间 API(位于 java.time
包中)。这个 API 解决了旧版 Date
和 Calendar
类的设计缺陷,提供了更简洁、易用和功能强大的工具来处理日期和时间。今天,我们将深入探讨 LocalDate
、LocalTime
和 LocalDateTime
的使用,Period
和 Duration
类的操作,以及 DateTimeFormatter
的格式化功能。
一、LocalDate
、LocalTime
和 LocalDateTime
的使用
LocalDate
、LocalTime
和 LocalDateTime
是 java.time
包中的三个重要类,它们分别用于表示日期、时间和日期时间。它们是不可变的(immutable),因此线程安全。
1.1 LocalDate
类的使用
LocalDate
表示没有时区的日期,通常用于表示日历中的某一天。
now()
:获取当前日期。of(int year, int month, int dayOfMonth)
:通过指定年月日创建日期。parse(CharSequence text)
:通过字符串解析为日期。
代码示例:使用 LocalDate
获取当前日期和创建日期
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期:" + currentDate);
// 创建特定日期
LocalDate specificDate = LocalDate.of(2023, 5, 19);
System.out.println("特定日期:" + specificDate);
// 从字符串解析日期
LocalDate parsedDate = LocalDate.parse("2023-05-19");
System.out.println("解析日期:" + parsedDate);
}
}
在这个例子中,我们通过 LocalDate.now()
获取当前日期,使用 LocalDate.of()
创建特定日期,使用 LocalDate.parse()
将字符串转换为日期。
1.2 LocalTime
类的使用
LocalTime
用于表示没有时区的时间,通常用于表示一天中的某个时刻。
now()
:获取当前时间。of(int hour, int minute)
:通过指定小时和分钟创建时间。parse(CharSequence text)
:通过字符串解析为时间。
代码示例:使用 LocalTime
获取当前时间和创建时间
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
// 获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("当前时间:" + currentTime);
// 创建特定时间
LocalTime specificTime = LocalTime.of(14, 30);
System.out.println("特定时间:" + specificTime);
// 从字符串解析时间
LocalTime parsedTime = LocalTime.parse("14:30:00");
System.out.println("解析时间:" + parsedTime);
}
}
在这个例子中,我们使用 LocalTime.now()
获取当前时间,使用 LocalTime.of()
创建特定时间,使用 LocalTime.parse()
将字符串转换为时间。
1.3 LocalDateTime
类的使用
LocalDateTime
是 LocalDate
和 LocalTime
的组合,表示没有时区的日期和时间。
now()
:获取当前日期和时间。of(int year, int month, int dayOfMonth, int hour, int minute)
:通过指定年月日时分创建日期时间。parse(CharSequence text)
:通过字符串解析为日期时间。
代码示例:使用 LocalDateTime
获取当前日期和时间
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("当前日期和时间:" + currentDateTime);
// 创建特定日期和时间
LocalDateTime specificDateTime = LocalDateTime.of(2023, 5, 19, 14, 30);
System.out.println("特定日期和时间:" + specificDateTime);
// 从字符串解析日期和时间
LocalDateTime parsedDateTime = LocalDateTime.parse("2023-05-19T14:30:00");
System.out.println("解析日期和时间:" + parsedDateTime);
}
}
在此例中,我们通过 LocalDateTime.now()
获取当前日期和时间,使用 LocalDateTime.of()
创建特定日期和时间,使用 LocalDateTime.parse()
将字符串解析为日期时间。
二、Period
和 Duration
类的操作
Period
和 Duration
是用于表示时间间隔的类。它们分别表示日期和时间之间的间隔。
2.1 Period
类
Period
用于表示两个日期之间的差异,单位包括年、月和日。
between(LocalDate startDate, LocalDate endDate)
:计算两个LocalDate
对象之间的时间间隔。
代码示例:使用 Period
计算日期间隔
import java.time.LocalDate;
import java.time.Period;
public class PeriodExample {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2023, 5, 19);
// 计算日期之间的间隔
Period period = Period.between(startDate, endDate);
System.out.println("年:" + period.getYears());
System.out.println("月:" + period.getMonths());
System.out.println("日:" + period.getDays());
}
}
通过 Period.between()
方法,我们可以计算两个日期之间的间隔,并提取年、月、日的差异。
2.2 Duration
类
Duration
用于表示两个时间(或日期时间)之间的差异,单位包括秒和纳秒。
between(LocalTime startTime, LocalTime endTime)
:计算两个LocalTime
对象之间的时间间隔。
代码示例:使用 Duration
计算时间间隔
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.toHours());
System.out.println("分钟:" + duration.toMinutes());
}
}
通过 Duration.between()
方法,我们可以计算两个时间之间的间隔,并转换为小时、分钟等单位。
三、DateTimeFormatter
的格式化功能
DateTimeFormatter
是用于格式化和解析日期时间的类。它支持自定义格式和内置的标准格式。
3.1 使用内置的 DateTimeFormatter
格式化器
Java 提供了一些常用的内置格式化器,如 ISO_LOCAL_DATE
、ISO_LOCAL_TIME
和 ISO_LOCAL_DATE_TIME
等。
代码示例:使用内置格式化器格式化和解析日期时间
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
// 使用内置格式化器进行格式化
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String formattedDateTime = currentDateTime.format(formatter);
System.out.println("格式化后的日期和时间:" + formattedDateTime);
}
}
在这个例子中,我们使用了 DateTimeFormatter.ISO_LOCAL_DATE_TIME
格式化器,将 LocalDateTime
对象转换为字符串。
3.2 自定义格式化器
DateTimeFormatter
允许你创建自定义格式化器,通过 ofPattern(String pattern)
方法来指定日期时间格式。
代码示例:使用自定义格式化器格式化日期
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class CustomDateTimeFormatterExample {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
// 创建自定义格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
// 使用自定义格式化器格式化日期
String formattedDate = currentDate.format(formatter);
System.out.println("自定义格式化后的日期:" + formattedDate);
}
}
在这个例子中,我们创建了一个自定义格式化器 DateTimeFormatter.ofPattern("dd-MM-yyyy")
,并将 LocalDate
对象格式化为 dd-MM-yyyy
格式的字符串。
3.3 解析字符串为日期时间
DateTimeFormatter
还可以将符合格式的字符串解析为日期时间对象。
代码示例:解析字符串为日期时间
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ParseDateExample {
public static void main(String[] args) {
String dateString = "19-05-2023";
// 使用自定义格式化器解析字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(dateString, formatter);
System.out.println("解析后的日期:" + date);
}
}
在这个例子中,我们使用 DateTimeFormatter.parse()
方法将字符串 19-05-2023
解析为 LocalDate
对象。
总结:新的日期与时间 API 的优势
Java 8 引入的日期和时间 API java.time
解决了 Date
和 Calendar
类的一些问题,提供了更简洁、灵活的方式来处理日期和时间。具体而言:
LocalDate
用于表示没有时区的日期(年、月、日)。LocalTime
用于表示没有时区的时间(时、分、秒)。LocalDateTime
用于表示没有时区的日期和时间(年、月、日、时、分、秒)。
这些类不仅支持流式操作,还具备不可变性,适合多线程环境。此外,Period
和 Duration
提供了对日期和时间间隔的处理能力,DateTimeFormatter
提供了强大的格式化和解析功能。
通过掌握这些新工具,你能够更高效地处理 Java 中的日期和时间,提高代码的可读性和可维护性。
… …
文末
好啦,以上就是我这期的全部内容,如果有任何疑问,欢迎下方留言哦,咱们下期见。
… …
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!
wished for you successed !!!
⭐️若喜欢我,就请关注我叭。
⭐️若对您有用,就请点赞叭。
⭐️若有疑问,就请评论留言告诉我叭。
版权声明:本文由作者原创,转载请注明出处,谢谢支持!