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


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


TemporalAdjusters类的lastInMonth(DayOfWeek)方法用于返回TemporalAdjuster对象,该对象可用于获取新的Date对象,该对象是同一月的最后一个日期,并且具有匹配的DayOfWeek作为参数从任何Date对象传递此TempotralAdjuster应用于其上。

用法:

public static TemporalAdjuster lastInMonth(DayOfWeek dayOfWeek)

参数:此方法接受dayOfWeek,可用于获取新的Date对象,该对象是具有相同匹配的DayOfWeek的同一月的最后一个日期。



返回值:此方法返回月份中的最后一个调整器,而不是null。

以下示例程序旨在说明TemporalAdjusters.lastInMonth()方法:
程序1:

// Java program to demonstrate 
// TemporalAdjusters.lastInMonth() 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get TemporalAdjuster with 
        // the last in month adjuster 
        TemporalAdjuster temporalAdjuster 
            = TemporalAdjusters.lastInMonth( 
                DayOfWeek.SUNDAY); 
  
        // using adjuster for local date-time 
        LocalDate localDate 
            = LocalDate.of(1998, 10, 31); 
        LocalDate lastInMonth 
            = localDate.with(temporalAdjuster); 
  
        // print 
        System.out.println( 
            "last date in month having"
            + " sunday for localdate "
            + localDate + " is:"
            + lastInMonth); 
    } 
}
输出:
last date in month having sunday for localdate 1998-10-31 is:1998-10-25

程序2:

// Java program to demonstrate 
// TemporalAdjusters.lastInMonth() method 
  
import java.time.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // get TemporalAdjuster with 
        // the last in month adjuster 
        TemporalAdjuster temporalAdjuster 
            = TemporalAdjusters.lastInMonth( 
                DayOfWeek.TUESDAY); 
  
        // using adjuster for local date time 
        LocalDate localDate 
            = LocalDate.of(2029, 12, 11); 
        LocalDate lastInMonth 
            = localDate.with(temporalAdjuster); 
  
        // print 
        System.out.println( 
            "The last date in a month "
            + "having TUESDAY for localdate "
            + localDate + " is:"
            + lastInMonth); 
    } 
}
输出:
The last date in a month having TUESDAY for localdate 2029-12-11 is:2029-12-25

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




相关用法


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