Posted in

在 Java 中获取周、月或年的第一天和最后一天_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Java 8、TemporalAdjusters、日期操作、星期、年月

2. 总结:

本文介绍了在 Java 8 中使用 `TemporalAdjusters` 类获取星期、月、年的第一天和最后一天。通过示例代码展示了相应方法的使用,指出其为日期操作提供了强大灵活的方式,能简化 Java 应用中的日期相关操作。

3. 主要内容:

– 引入

– Java 8 的日期和时间 API 中的 `TemporalAdjusters` 类改进了日期处理方式。

– `TemporalAdjusters` 在 Java 8 日期时间 API 中的使用

– 提供了一系列预定义的调整器用于日期操作。

– 常见方法如 `firstDayOfMonth`、`lastDayOfMonth` 等。

– 星期的第一天和最后一天

– 结合 `DayOfWeek` 枚举和 `TemporalAdjusters` 类获取。

– 示例代码及输出。

– 月的第一天和最后一天

– 使用 `firstDayOfMonth` 和 `lastDayOfMonth` 方法。

– 示例代码及输出。

– 年的第一天和最后一天

– 借助 `firstDayOfYear` 和 `lastDayOfYear` 方法。

– 示例代码及输出。

– 结论

– `TemporalAdjusters` 类提供了强大灵活的日期操作方法,能简化 Java 应用中的相关操作。

思维导图:

文章地址:https://www.javacodegeeks.com/get-first-and-last-day-of-week-month-or-year-in-java.html

文章来源:javacodegeeks.com

作者:Yatin Batra

发布时间:2024/6/17 10:59

语言:英文

总字数:747字

预计阅读时间:3分钟

评分:86分

标签:日期时间,Java8


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

The introduction of the Date and Time API in Java 8 brought a significant improvement to how dates and times are handled in Java. One of the key components of this new API is the TemporalAdjusters class, which provides a variety of methods for date manipulation. This article explores how to use the TemporalAdjusters class to find the first and last days of the week, month, and year. Let us delve into understanding the various methods to adjust dates to a specific day of the week, month, or year.

1. Using TemporalAdjusters in Java 8 Date Time API

The TemporalAdjusters class in Java 8 provides a collection of pre-defined adjusters that can be used to manipulate dates. This class is part of the new Date and Time API introduced in Java 8. The TemporalAdjusters class provides a set of utility methods for date adjustments. These methods return instances of TemporalAdjuster which can be used with any class implementing the Temporal interface, such as LocalDate. Here are some commonly used methods:

  • firstDayOfMonth(): Adjusts the date to the first day of the current month.
  • lastDayOfMonth(): Adjusts the date to the last day of the current month.
  • firstDayOfNextMonth(): Adjusts the date to the first day of the next month.
  • firstDayOfYear(): Adjusts the date to the first day of the current year.
  • lastDayOfYear(): Adjusts the date to the last day of the current year.
  • firstInMonth(DayOfWeek dayOfWeek): Adjusts the date to the first occurrence of the specified day of the week in the current month.
  • lastInMonth(DayOfWeek dayOfWeek): Adjusts the date to the last occurrence of the specified day of the week in the current month.
  • next(DayOfWeek dayOfWeek): Adjusts the date to the next occurrence of the specified day of the week after the current date.
  • previous(DayOfWeek dayOfWeek): Adjusts the date to the previous occurrence of the specified day of the week before the current date.

2. First Day and Last Day of Week

To find the first and last day of the week, you can use the DayOfWeek enum along with the TemporalAdjusters class. Here is an example:

import java.time.DayOfWeek;import java.time.LocalDate;import java.time.temporal.TemporalAdjusters;public class FirstAndLastDayOfWeek {    public static void main(String[] args) {        LocalDate today = LocalDate.now();                LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));        LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));                System.out.println("Today: " + today);        System.out.println("First Day of Week: " + firstDayOfWeek);        System.out.println("Last Day of Week: " + lastDayOfWeek);    }}

Here’s a code breakdown:

  • LocalDate.now(): Gets the current date.
  • previousOrSame(DayOfWeek.MONDAY): Adjusts the date to the previous Monday or stays on the same day if it is Monday.
  • nextOrSame(DayOfWeek.SUNDAY): Adjusts the date to the next Sunday or stays on the same day if it is Sunday.

The code output is:

Today: 2024-06-07First Day of Week: 2024-06-03Last Day of Week: 2024-06-09

3. First Day and Last Day of Month

To find the first and last day of the month, you can use the firstDayOfMonth() and lastDayOfMonth() methods. Here is an example:

import java.time.LocalDate;import java.time.temporal.TemporalAdjusters;public class FirstAndLastDayOfMonth {    public static void main(String[] args) {        LocalDate today = LocalDate.now();                LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());                System.out.println("Today: " + today);        System.out.println("First Day of Month: " + firstDayOfMonth);        System.out.println("Last Day of Month: " + lastDayOfMonth);    }}

Here’s a code breakdown:

  • firstDayOfMonth(): Adjusts the date to the first day of the month.
  • lastDayOfMonth(): Adjusts the date to the last day of the month.

The code output is:

Today: 2024-06-07First Day of Month: 2024-06-01Last Day of Month: 2024-06-30

4. First Day and Last Day of Year

To find the first and last day of the year, you can use the firstDayOfYear() and lastDayOfYear() methods. Here is an example:

import java.time.LocalDate;import java.time.temporal.TemporalAdjusters;public class FirstAndLastDayOfYear {    public static void main(String[] args) {        LocalDate today = LocalDate.now();                LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear());        LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());                System.out.println("Today: " + today);        System.out.println("First Day of Year: " + firstDayOfYear);        System.out.println("Last Day of Year: " + lastDayOfYear);    }}

Here’s a code breakdown:

  • firstDayOfYear(): Adjusts the date to the first day of the year.
  • lastDayOfYear(): Adjusts the date to the last day of the year.

The code output is:

Today: 2024-06-07First Day of Year: 2024-01-01Last Day of Year: 2024-12-31

5. Conclusion

In conclusion, the TemporalAdjusters class in the Java 8 Date and Time API provides powerful and flexible methods for date manipulation. Whether you need to find the first or last day of a week, month, or year, TemporalAdjusters offers a straightforward and efficient way to perform these adjustments. Understanding and utilizing these methods can greatly simplify date-related operations in your Java applications.