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
相關用法
- Java TemporalAdjusters next()用法及代碼示例
- Java TemporalAdjusters nextOrSame()用法及代碼示例
- Java TemporalAdjusters firstInMonth()用法及代碼示例
- Java TemporalAdjusters firstDayOfNextYear()用法及代碼示例
- Java TemporalAdjusters firstDayOfNextMonth()用法及代碼示例
- Java TemporalAdjusters firstDayOfMonth()用法及代碼示例
- Java TemporalAdjusters previous()用法及代碼示例
- Java TemporalAdjusters firstDayOfYear()用法及代碼示例
- Java TemporalAdjusters lastDayOfMonth()用法及代碼示例
- Java TemporalAdjusters lastDayOfYear()用法及代碼示例
- Java TemporalAdjusters previousOrSame()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 TemporalAdjusters lastInMonth() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。