当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TemporalAdjusters previousOrSame()用法及代码示例


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

参考文献:https://docs.oracle.com/javase/10/docs/api/java/time/temporal/TemporalAdjusters.html#previousOrSame(java.time.DayOfWeek)




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 TemporalAdjusters previousOrSame() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。