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


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); 
  
        // 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

程序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); 
  
        // 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://docs.oracle.com/javase/10/docs/api/java/time/temporal/TemporalAdjusters.html#previous(java.time.DayOfWeek)




相关用法


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