TemporalAdjusters类的previousOrSame(DayOfWeek)方法用于返回先前或相同的day-of-week TemporalAdjuster对象,该对象可用于获取新的Date对象,该对象是先前或相同日期的具有相同匹配的day-of-week作为参数从任何对象传递应用此TempotralAdjuster的Date对象。
用法:
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek)
参数:此方法接受dayOfWeek,可以使用它来获取新的Date对象,该对象是先前的日期或具有相同的匹配day-of-week的同一日期。它不能为空。
返回值:此方法返回先前或相同的day-of-week TemporalAdjuster对象。
以下示例程序旨在说明TemporalAdjusters.previousOrSame()方法:
程序1:
// Java program to demonstrate
// TemporalAdjusters.previousOrSame()
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Get TemporalAdjuster with the
// previousOrSame() of month adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters
.previousOrSame(
DayOfWeek.FRIDAY);
// Using adjuster for local date-time
LocalDate localDate
= LocalDate.of(2020, 10, 30);
LocalDate previousOrSameDOW
= localDate.with(temporalAdjuster);
// Print
System.out.println(
"Previous or same date "
+ "having FRIDAY for localdate "
+ localDate + " is:"
+ previousOrSameDOW);
}
}
输出:
Previous or same date having FRIDAY for localdate 2020-10-30 is:2020-10-30
程序2:
// Java program to demonstrate
// TemporalAdjusters previousOrSame() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Get TemporalAdjuster with the
// previousOrSame() day of week adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters
.previousOrSame(
DayOfWeek.THURSDAY);
// Using adjuster for local date time
LocalDate localDate
= LocalDate.of(2018, 02, 23);
LocalDate previousOrSameDOW
= localDate.with(temporalAdjuster);
// Print
System.out.println(
"Previous or same date "
+ "having THURSDAY for localdate "
+ localDate + " is:"
+ previousOrSameDOW);
}
}
输出:
Previous or same date having THURSDAY for localdate 2018-02-23 is:2018-02-22
相关用法
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Clock withZone()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Map put()用法及代码示例
- Java Set add()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 TemporalAdjusters previousOrSame() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。