Java中的 TemporalAdjusters previous() 方法和示例
TemporalAdjusters 类的previous(DayOfWeek)方法用于返回先前的 DayOfWeek TemporalAdjuster 对象,该对象可用于获取新的 Date 对象,该对象是具有与从任何 Date 对象传递的参数相同的匹配 DayOfWeek 的先前日期应用了这个 TempotralAdjuster。
句法:
public static TemporalAdjuster previous(DayOfWeek dayOfWeek)
参数:此方法接受dayOfWeek可用于获取新的 Date 对象,该对象是具有相同匹配 DayOfWeek 的前一个日期。
返回值:该方法返回星期调整器的前一天。
下面的程序说明了 TemporalAdjusters.previous() 方法:
方案一:
// Java program to demonstrate
// TemporalAdjusters.previous()
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// get TemporalAdjuster with the
// previous in month adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters.previous(
DayOfWeek.WEDNESDAY);
// using adjuster for local date-time
LocalDate localDate
= LocalDate.of(1998, 10, 31);
LocalDate previousDOW
= localDate.with(temporalAdjuster);
// print
System.out.println(
"Previous day of the week "
+ "having WEDNESDAY for localdate "
+ localDate + " is: "
+ previousDOW);
}
}
输出:
Previous day of the week having WEDNESDAY for localdate 1998-10-31 is: 1998-10-28
方案二:
// Java program to demonstrate
// TemporalAdjusters.previous() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// get TemporalAdjuster with the
// previous day of week adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters.previous(
DayOfWeek.THURSDAY);
// using adjuster for local date time
LocalDate localDate
= LocalDate.of(2029, 12, 11);
LocalDate previousDOW
= localDate.with(temporalAdjuster);
// print
System.out.println(
"previous day of the week "
+ "having THURSDAY for localdate "
+ localDate + " is: "
+ previousDOW);
}
}
输出:
previous day of the week having THURSDAY for localdate 2029-12-11 is: 2029-12-06
参考:https: Java Java.time.DayOfWeek)