📌  相关文章
📜  Java的.time.temporal.TemporalAdjusters类在Java中

📅  最后修改于: 2022-05-13 01:55:50.510000             🧑  作者: Mango

Java的.time.temporal.TemporalAdjusters类在Java中

Java的TemporalAdjusters 类提供了 Adjusters,它是修改时间对象的关键工具。示例包括将日期设置为“本月的第二个星期六”或“下周二”的调整器,或者将日期设置为该月的最后一天的调整器。

TemporalAdjuster 可以通过两种方式使用。第一种是直接调用接口上的方法。第二种是使用 Temporal.with(TemporalAdjuster):

下面两种使用TemporalAdjuster的方式是等价的,但是推荐使用第二种方式,因为它更简洁易读

temporal = thisAdjuster.adjustInto(temporal);
temporal = temporal.with(thisAdjuster);

方法

MethodDescription 
dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek)This method is used to return the day-of-week in month adjuster, which returns a new date object within the same month with the ordinal day-of-week.
firstDayOfMonth()This method is used to return the “first day of month” adjuster, which returns a new date set to the first day of the current month.
firstDayOfNextMonth()This method is used to return the “first day of next month” adjuster, which returns a new date set to the first day of the next month.
firstDayOfNextYear()This method is used to return the “first day of next year” adjuster, which returns a new date set to the first day of the next year.
firstDayOfYear()This method is used to return the “first day of year” adjuster, which returns a new date set to the first day of the current year.
firstInMonth(DayOfWeek dayOfWeek)This method is used to return the first in month adjuster, which returns a new date in the same month with the first matching day-of-week.
lastDayOfMonth()This method is used to return the “last day of month” adjuster, which returns a new date set to the last day of the current month.
lastDayOfYear()This method is used to return the “last day of year” adjuster, which returns a new date set to the last day of the current year.
lastInMonth(DayOfWeek dayOfWeek)This method is used to return the last in month adjuster, which returns a new date in the same month with the last matching day-of-week.
next(DayOfWeek dayOfWeek)This method is used to return the next day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted.
nextOrSame(DayOfWeek dayOfWeek)This method is used to return the next-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted unless it is already on that day in which case the same object is returned.
ofDateAdjuster(UnaryOperator dateBasedAdjuster)This method is used to obtain a TemporalAdjuster that wraps a date adjuster.
previous(DayOfWeek dayOfWeek)This method is used to return the previous day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted.
previousOrSame(DayOfWeek dayOfWeek)This method is used to return the previous-or-same day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week before the date being adjusted unless it is already on that day in which case the same object is returned.

Java
// Implementation of TemporalAdjuster Class Output will
// be different at the time of execution for different
// days. All the dates in the output will be with respect
// to the current date of executing the program
 
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
 
public class TemporalAdjusterExample {
    public static void main(String args[])
    {
        TemporalAdjusterExample gfg
            = new TemporalAdjusterExample();
        gfg.testAdjusters();
    }
 
    public void testAdjusters()
    {
        // to get the current date
        LocalDate date1 = LocalDate.now();
        System.out.println("Today's date is: " + date1);
 
        // to get the next monday
        LocalDate nextTuesday = date1.with(
            TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("Next Monday is on : "
                           + nextTuesday);
 
        // to get the second saturday of next month
        LocalDate firstInYear = LocalDate.of(
            date1.getYear(), date1.getMonth(), 1);
 
        LocalDate secondSaturday
            = firstInYear
                  .with(TemporalAdjusters.nextOrSame(
                      DayOfWeek.SATURDAY))
                  .with(TemporalAdjusters.next(
                      DayOfWeek.SATURDAY));
       
        // print date of second Saturday of next month
        System.out.println("Second saturday is on : "
                           + secondSaturday);
    }
}


输出:

Today's date is: 2021-02-24
Next Monday is on : 2021-03-01
Second saturday is on : 2021-02-13