📜  Java的.time.YearMonth类在Java中

📅  最后修改于: 2022-05-13 01:54:49.046000             🧑  作者: Mango

Java的.time.YearMonth类在Java中

Java YearMonth 类提供“年-月”格式的输出。这个类是一个不可变的类,这意味着它定义的对象一旦创建就永远不会改变它们的值。任何从年和月派生的字段,如季度,通常都可以获得。此类不存储或表示日期、时间或时区。例如,它不会显示值“November-2006-12:00”,而是会显示“2007-11”。它继承了thing类并实现了Comparable接口。

Java YearMonth 类实现了 Temporal、TemporalAdjuster、Comparable、Serializable

MethodDescription
adjustInto(Temporal temporal)This method adjusts the specified temporal object to have this year-month.
atDay(int dayOfMonth)This method combines this year-month with a day-of-month to create a LocalDate.
atEndOfMonth()This method returns a LocalDate at the end of the month.
compareTo(YearMonth other)This method compares this year-month to another year-month.
equals(Object obj)This method checks if this year-month is equal to another year-month.
format(DateTimeFormatter formatter)This method Formats this year-month using the specified formatter.
from(TemporalAccessor temporal)This method obtains an instance of YearMonth from a temporal object.
get(TemporalField field)This method gets the value of the specified field from this year-month as an int.
 getLong(TemporalField field)This method gets the value of the specified field from this year-month as along.
getMonth()This method gets the month-of-year field using the Month enum.
 getMonthValue()This method gets the month-of-year field from 1 to 12.
getYear()This method gets the year field.
hashCode()A hash code for this year-month.
isAfter(YearMonth other)This method checks if this year-month is after the specified year-month.
isBefore(YearMonth other)This method checks if this year-month is before the specified year-month.
isLeapYear()This method checks if the year is a leap year, according to the ISO proleptic calendar system rules.
 isSupported(TemporalField field)This method checks if the specified field is supported.
isSupported(TemporalUnit unit)This method checks if the specified unit is supported.
isValidDay(int dayOfMonth)This method checks if the day-of-month is valid for this year-month.
lengthOfMonth()This method returns the length of the month, taking account of the year.
lengthOfYear()This method returns the length of the year.
minus(long amountToSubtract, TemporalUnit unit)This method returns a copy of this year-month with the specified amount subtracted.
minus(TemporalAmount amountToSubtract)This method returns a copy of this year-month with the specified amount subtracted.
minusMonths(long monthsToSubtract)This method returns a copy of this YearMonth with the specified number of months subtracted.
minusYears(long yearsToSubtract)This method returns a copy of this YearMonth with the specified number of years subtracted.
now()This method obtains the current year-month from the system clock in the default time-zone.
now(Clock clock)This method obtains the current year-month from the specified clock.
now(ZoneId zone)This method obtains the current year-month from the system clock in the specified time-zone.
of(int year, int month)This method obtains an instance of YearMonth from a year and month.
of(int year, Month month)This method obtains an instance of YearMonth from a year and month.
parse(CharSequence text)This method obtains an instance of YearMonth from a text string such as 2007-12.
parse(CharSequence text, DateTimeFormatter formatter)This method obtains an instance of YearMonth from a text string using a specific formatter.
 plus(long amountToAdd, TemporalUnit unit)This method returns a copy of this year-month with the specified amount added.
 plus(TemporalAmount amountToAdd)This method returns a copy of this year-month with the specified amount added.
plusMonths(long monthsToAdd)This method returns a copy of this YearMonth with the specified number of months added.
plusYears(long yearsToAdd)This method returns a copy of this YearMonth with the specified number of years added.
query(TemporalQuery query)This method queries this year-month using the specified query.
range(TemporalField field)This method gets the range of valid values for the specified field.
toString()This method outputs this year-month as a String, such as 2007-12.
until(Temporal endExclusive, TemporalUnit unit)This method calculates the amount of time until another year-month in terms of the specified unit.
with(TemporalAdjuster adjuster)This method returns an adjusted copy of this year-month.
with(TemporalField field, long newValue)This method returns a copy of this year-month with the specified field set to a new value.
withMonth(int month)This method returns a copy of this YearMonth with the month-of-year altered.
withYear(int year)This method returns a copy of this YearMonth with the year altered.

下面是Java YearMonth 类的一些方法的实现:



1. plus():返回添加了所需数量的该年月的副本。

Java
// plus() Method implementation
import java.time.*;
public class Example {
    public static void main(String[] args)
    {
        YearMonth obj1 = YearMonth.now();
  
        // plus(Period.ofYears(int)) will add
        // no.of years to the existing year
        YearMonth obj2 = obj1.plus(Period.ofYears(0));
        System.out.println(obj2);
    }
}


Java
// minus() method implementation
import java.time.*;
public class Example {
    public static void main(String[] args)
    {
        YearMonth obj1 = YearMonth.now();
  
        //.minus(Period.ofYears(int)) will subtract
        // no. ofyears from the existing year
        YearMonth obj2 = obj1.minus(Period.ofYears(3));
        System.out.println(obj2);
    }
}


输出
2021-02

2. 减():返回减去所需数量的这一年月的副本。

Java

// minus() method implementation
import java.time.*;
public class Example {
    public static void main(String[] args)
    {
        YearMonth obj1 = YearMonth.now();
  
        //.minus(Period.ofYears(int)) will subtract
        // no. ofyears from the existing year
        YearMonth obj2 = obj1.minus(Period.ofYears(3));
        System.out.println(obj2);
    }
}
输出
2018-02