java.time.Month ENUM的adjustInto()方法是Java中的内置函数,该函数接受一个Temporal对象,该对象指定一个日期,并返回与输入相同的可观察类型的新Temporal对象,并用此month-of-year替换为月份。
方法声明:
public Temporal adjustInto(Temporal temporal)
用法:
Temporal newLocalDate = Month.ANYMONTH.adjustInto(Temporal temporal)
参数:此方法以时间为参数,其中:
- temporal–是要调整的指定日期。
- ANYMONTH–是要调整日期的指定月份,例如1月,2月等。
- newLocalDate–是修改日期。
返回值:该函数返回调整后的Temporal对象,该对象是根据指定的Month调整的日期。
异常:
- DateTimeException:如果无法进行调整,则此方法将引发此异常。
- ArithmeticException注意:如果发生数字溢出,则抛出此异常。
以下示例程序旨在说明上述方法:
程序1:
import java.time.*;
import java.time.Month;
import java.time.temporal.Temporal;
class DayOfWeekExample {
public static void main(String[] args)
{
// Set a Local Date whose month is found
LocalDate localDate1
= LocalDate.of(1947, Month.AUGUST, 15);
// Find the month from the Local Date
Month monthOfYear1
= Month.from(localDate1);
// Printing the Local Date
System.out.println(localDate1
+ " which is "
+ monthOfYear1.name());
// Adjust the month to JANUARY from AUGUST
Temporal localDate2
= Month.JANUARY
.adjustInto(localDate1);
// Find the day from the new Local date
Month monthOfYear2
= Month.from(localDate2);
// Printing the new Local Date
System.out.println(localDate2
+ " which is "
+ monthOfYear2.name());
}
}
输出:
1947-08-15 which is AUGUST 1947-01-15 which is JANUARY
程序2:
import java.time.*;
import java.time.Month;
import java.time.temporal.Temporal;
class DayOfWeekExample {
public static void main(String[] args)
{
// Set a Local Date whose month is found
LocalDate localDate1
= LocalDate.of(2019, Month.MARCH, 18);
// Find the month from the Local Date
Month monthOfYear1
= Month.from(localDate1);
// Printing the Local Date
System.out.println(localDate1
+ " which is "
+ monthOfYear1.name());
// Adjust the month to December from March
Temporal localDate2
= Month.DECEMBER
.adjustInto(localDate1);
// Find the day from the new Local date
Month monthOfYear2
= Month.from(localDate2);
// Printing the new Local Date
System.out.println(localDate2
+ " which is "
+ monthOfYear2.name());
}
}
输出:
2019-03-18 which is MARCH 2019-12-18 which is DECEMBER
相关用法
- Java Year atMonth(Month month)用法及代码示例
- Java LocalDate adjustInto()用法及代码示例
- Java Instant adjustInto()用法及代码示例
- Java LocalTime adjustInto()用法及代码示例
- Java ChronoLocalDate adjustInto()用法及代码示例
- Java DayOfWeek adjustInto()用法及代码示例
- Java OffsetTime adjustInto()用法及代码示例
- Java OffsetDateTime adjustInto()用法及代码示例
- Java Year adjustInto()用法及代码示例
- Java ChronoLocalDateTime adjustInto()用法及代码示例
- Java MonthDay adjustInto()用法及代码示例
- Java ZoneOffset adjustInto(Temporal)用法及代码示例
- Java Month from()用法及代码示例
- Java Month get()用法及代码示例
- Java Month of()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Month adjustInto() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。