新的日期与时间API:告别 `Date` 和 `Calendar`,迎接更现代的日期与时间处理!
全文目录:
- 开篇语
- 前言
- 一、LocalDate、LocalTime、LocalDateTime 的使用
- 1.1 `LocalDate` 类
- 代码示例:使用 `LocalDate`
- 1.2 `LocalTime` 类
- 代码示例:使用 `LocalTime`
- 1.3 `LocalDateTime` 类
- 代码示例:使用 `LocalDateTime`
- 二、Period、Duration 类的操作
- 2.1 `Period` 类
- 代码示例:使用 `Period`
- 2.2 `Duration` 类
- 代码示例:使用 `Duration`
- 三、DateTimeFormatter 的格式化功能
- 3.1 使用内置的格式化器
- 代码示例:使用内置格式化器
- 3.2 自定义格式化器
- 代码示例:使用自定义格式化器
- 3.3 解析字符串为日期
- 代码示例:使用 `DateTimeFormatter` 解析字符串
- 总结:新的日期与时间 API 的优势
- 文末
开篇语
哈喽,各位小伙伴们,你们好呀,我是喵手。运营社区:C站/掘金/腾讯云/阿里云/华为云/51CTO;欢迎大家常来逛逛
今天我要给大家分享一些自己日常学习到的一些知识点,并以文字的形式跟大家一起交流,互相学习,一个人虽可以走的更快,但一群人可以走的更远。
我是一名后端开发爱好者,工作日常接触到最多的就是Java语言啦,所以我都尽量抽业余时间把自己所学到所会的,通过文章的形式进行输出,希望以这种方式帮助到更多的初学者或者想入门的小伙伴们,同时也能对自己的技术进行沉淀,加以复盘,查缺补漏。
小伙伴们在批阅的过程中,如果觉得文章不错,欢迎点赞、收藏、关注哦。三连即是对作者我写作道路上最好的鼓励与支持!
前言
在 Java 8 中,Java 引入了一个全新的日期与时间 API,称为 java.time。这个 API 解决了老旧的 Date
和 Calendar
类的一些问题,提供了更加简洁、可读和易于使用的类。今天我们将深入探讨 LocalDate、LocalTime、LocalDateTime 的使用,Period 和 Duration 类的操作,以及 DateTimeFormatter 的格式化功能。
一、LocalDate、LocalTime、LocalDateTime 的使用
LocalDate
、LocalTime
和 LocalDateTime
都属于 java.time
包,是 Java 新的日期和时间处理类。它们都表示没有时区的日期或时间,区别在于它们的功能侧重点。
1.1 LocalDate
类
LocalDate
用于表示没有时区的日期,通常用于表示日历中的某一天。
代码示例:使用 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);// 获取日期的各个字段int year = specificDate.getYear();int month = specificDate.getMonthValue();int day = specificDate.getDayOfMonth();System.out.println("年:" + year + " 月:" + month + " 日:" + day);}
}
LocalDate.now()
获取当前系统日期,LocalDate.of()
创建特定日期。你可以使用 getYear()
、getMonthValue()
和 getDayOfMonth()
来获取日期的各个部分。
1.2 LocalTime
类
LocalTime
用于表示没有时区的时间,通常用于表示一天中的某个时刻。
代码示例:使用 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);// 获取时间的小时和分钟int hour = specificTime.getHour();int minute = specificTime.getMinute();System.out.println("小时:" + hour + " 分钟:" + minute);}
}
LocalTime.now()
获取当前系统时间,LocalTime.of()
创建特定时间。你可以使用 getHour()
和 getMinute()
来获取时间的小时和分钟。
1.3 LocalDateTime
类
LocalDateTime
是 LocalDate
和 LocalTime
的组合,表示没有时区的日期和时间。
代码示例:使用 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.now()
获取当前的日期和时间,LocalDateTime.of()
创建特定的日期和时间。
二、Period、Duration 类的操作
Period
和 Duration
是 java.time
包中用来表示时间间隔的类。Period
用于表示日期之间的间隔,而 Duration
用于表示时间(小时、分钟、秒)之间的间隔。
2.1 Period
类
Period
用于表示两个日期之间的间隔,单位包括年、月、日。
代码示例:使用 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() + " 月:" + period.getMonths() + " 日:" + period.getDays());}
}
Period.between(startDate, endDate)
计算两个日期之间的时间间隔,返回一个 Period
对象,可以通过 getYears()
、getMonths()
和 getDays()
获取年、月、日。
2.2 Duration
类
Duration
用于表示两个时间或日期时间之间的间隔,单位包括秒和纳秒。
代码示例:使用 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() + " 分钟:" + duration.toMinutes());}
}
Duration.between(startTime, endTime)
计算两个时间之间的间隔,返回一个 Duration
对象,可以通过 toHours()
、toMinutes()
等方法获取时间间隔的小时、分钟等。
三、DateTimeFormatter 的格式化功能
DateTimeFormatter
是 java.time.format
包中的一个类,用于格式化和解析日期时间。它提供了丰富的格式化功能,支持自定义格式和内置的标准格式。
3.1 使用内置的格式化器
DateTimeFormatter
提供了一些常用的内置格式化器,例如 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
允许我们定义自定义的日期时间格式。
代码示例:使用自定义格式化器
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
对象。
3.3 解析字符串为日期
DateTimeFormatter
还可以用来解析字符串为日期或时间。
代码示例:使用 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
将一个字符串解析为 LocalDate
对象,格式为 dd-MM-yyyy
。
总结:新的日期与时间 API 的优势
通过 Java 8 引入的新的日期和时间 API,LocalDate
、LocalTime
、LocalDateTime
等类提供了更加直观和易用的接口,极大地改善了 Java 日期和时间处理的体验。Period
和 Duration
类使得时间间隔的处理更加简便,而 DateTimeFormatter
则为我们提供了强大的格式化和解析能力。
掌握这些新的日期和时间操作类,你的日期时间处理将变得更加清晰、简洁,并且易于维护。
… …
文末
好啦,以上就是我这期的全部内容,如果有任何疑问,欢迎下方留言哦,咱们下期见。
… …
学习不分先后,知识不分多少;事无巨细,当以虚心求教;三人行,必有我师焉!!!
wished for you successed !!!
⭐️若喜欢我,就请关注我叭。
⭐️若对您有用,就请点赞叭。
⭐️若有疑问,就请评论留言告诉我叭。
版权声明:本文由作者原创,转载请注明出处,谢谢支持!