📜  Java8 YearMonth类

📅  最后修改于: 2020-10-01 06:38:08             🧑  作者: Mango

Java YearMonth类

Java YearMonth类是一个不可变的日期时间对象,它表示年份和月份的组合。它继承了Object类并实现Comparable接口。

Java YearMonth类声明

让我们看一下java.time.YearMonth类的声明。

public final class YearMonth extends Object 
implements Temporal, TemporalAdjuster, Comparable, Serializable

Java YearMonth的方法

Method Description
Temporal adjustInto(Temporal temporal) It is used to adjust the specified temporal object to have this year-month.
String format(DateTimeFormatter formatter) It is used to format this year-month using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this year-month as an int.
boolean isLeapYear() It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules.
static YearMonth now() It is used to obtain the current year-month from the system clock in the default time zone.
static YearMonth of(int year, int month) It is used to obtain an instance of YearMonth from a year and month.
YearMonth plus(TemporalAmount amountToAdd) It is used to return a copy of this year-month with the specified amount added.
YearMonthminus (TemporalAmount amountToSubtract) It is used to return a copy of this year-month with the specified amount subtracted.

Java YearMonth示例:now()

import java.time.YearMonth;
public class YearMonthExample1 {
public static void main(String[] args) {
  YearMonth ym = YearMonth.now();
  System.out.println(ym);
}
}

输出:

2017-01  

Java YearMonth示例:format()

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
public class YearMonthExample2 {
  public static void main(String[] args) {
    YearMonth ym = YearMonth.now();
    String s = ym.format(DateTimeFormatter.ofPattern("MM yyyy"));
    System.out.println(s);
  }
}

输出:

01 2017

Java YearMonth示例:get()

import java.time.YearMonth;
import java.time.temporal.ChronoField;
public class YearMonthExample3 {
  public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    long l1 = y.get(ChronoField.YEAR);
    System.out.println(l1);
    long l2 = y.get(ChronoField.MONTH_OF_YEAR);
    System.out.println(l2);
  }
}

输出:

2017
1

Java YearMonth示例:plus()

import java.time.*;
public class YearMonthExample4 {
  public static void main(String[] args) {
    YearMonth ym1 = YearMonth.now();
    YearMonth ym2 = ym1.plus(Period.ofYears(2));
    System.out.println(ym2);
  }
}

输出:

2019-01

Java YearMonth示例:minus()

import java.time.*;
public class YearMonthExample5 {
  public static void main(String[] args) {
    YearMonth ym1 = YearMonth.now();
    YearMonth ym2 = ym1.minus(Period.ofYears(2));
    System.out.println(ym2);
  }
}

输出:

2015-01