本文主要是介绍java previous_Java TemporalAdjusters previous()用法及代码示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
TemporalAdjusters类的previous(DayOfWeek)方法用于返回先前的DayOfWeek TemporalAdjuster对象,该对象可用于获取新的Date对象,该对象是与任何日期对象作为参数传递的,具有相同的DayOfWeek匹配的先前的日期。将应用此TempotralAdjuster。
用法:
public static TemporalAdjuster previous(DayOfWeek dayOfWeek)
参数:此方法接受dayOfWeek,可用于获取新的Date对象,该对象是具有相同匹配的DayOfWeek的前一个日期。
返回值:此方法返回星期几的前一天调节器。
以下示例程序旨在说明TemporalAdjusters.previous()方法:
程序1:
// 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);
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
程序2:
// 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);
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
这篇关于java previous_Java TemporalAdjusters previous()用法及代码示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!