YearMonth是一个不变的日期时间对象,表示一年和一个月的组合,例如:2021-07.
import java.time.LocalDate;
import java.time.Period;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
public class YearMonthTest {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2020,1,2);
System.out.println("-----------------------------------------");
YearMonth yearMonth = YearMonth.now();
System.out.println("yearMonth:"+yearMonth);
Temporal temporal = yearMonth.adjustInto(localDate);
System.out.println("LocalDate:"+localDate);
System.out.println("adjustInto:"+temporal);
System.out.println("-----------------------------------------");
LocalDate atDay = yearMonth.atDay(6);
System.out.println("atDay:"+atDay);
System.out.println("-----------------------------------------");
//atEndOfMonth
System.out.println("atEndOfMonth:"+yearMonth.atEndOfMonth());
System.out.println("-----------------------------------------");
//compareTo注意点:比较的是两个年月中间查的月份数,并不是大于=1 相等=0 小于=-1
YearMonth yearMonth2 = YearMonth.of(2021, 1);
int i = yearMonth.compareTo(yearMonth2);
System.out.println("yearMonth.compareTo(yearMonth2) :"+ i);
int i2 = yearMonth2.compareTo(yearMonth);
System.out.println("yearMonth2.compareTo(yearMonth) :"+ i2);
System.out.println("-----------------------------------------");
//equals
System.out.println("yearMonth.equals(yearMonth2) :"+ yearMonth.equals(yearMonth2));
YearMonth yearMonth3 = YearMonth.of(2021, 7);
System.out.println("yearMonth.equals(yearMonth3) :"+ yearMonth.equals(yearMonth3));
System.out.println("-----------------------------------------");
//format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy年M月");
String format = yearMonth.format(dtf);
System.out.println("format :"+ format);
System.out.println("-----------------------------------------");
YearMonth from = YearMonth.from(localDate);
System.out.println("from :"+ from);
System.out.println("-----------------------------------------");
//get
System.out.println("get Year :"+ yearMonth.get(ChronoField.YEAR));
System.out.println("get Month :"+ yearMonth.get(ChronoField.MONTH_OF_YEAR));
System.out.println("-----------------------------------------");
//getLong
System.out.println("getLong Year :"+ yearMonth.getLong(ChronoField.YEAR));
System.out.println("-----------------------------------------");
//getMonth
System.out.println("getMonth :"+ yearMonth.getMonth());
System.out.println("-----------------------------------------");
//isValidDay 验证指定月份是否支持日,比如6月是否支持31号,是否支持30号
YearMonth yearMonth4 = YearMonth.of(2021, 6);
boolean validDay = yearMonth4.isValidDay(31);
System.out.println("isValidDay :"+ validDay);
boolean validDay2 = yearMonth4.isValidDay(30);
System.out.println("isValidDay :"+ validDay2);
System.out.println("-----------------------------------------");
//minus
System.out.println(yearMonth + " minus : 3年 = "+ yearMonth.minus(3, ChronoUnit.YEARS));
System.out.println(yearMonth + " minusMonths : 3个月 = "+ yearMonth.minusMonths(3));
System.out.println("-----------------------------------------");
//parse 支持的默认格式:yyyy-MM
System.out.println("parse :"+ YearMonth.parse("2020-06"));
//parse 不是默认格式:yyyy-MM,则报错
//System.out.println("parse :"+ YearMonth.parse("2020-6"));
System.out.println("parse :"+ YearMonth.parse("21年7月",dtf));
System.out.println("-----------------------------------------");
Period periodMonth = Period.ofMonths(2);
YearMonth plus = yearMonth.plus(periodMonth);
System.out.println("plus 2个月:"+ plus);
//YearMonth 不支持与带有日的Period运算
//报错:Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Days
//Period period = Period.of(1, 2,3);
//YearMonth plus = yearMonth.plus(period);
//System.out.println("plus :"+ plus);
//YearMonth 不支持与Duration运算
//Duration duration = Duration.of(1, ChronoUnit.DAYS);
//YearMonth plus2 = yearMonth.plus(duration);
//System.out.println("plus :"+ plus2);
System.out.println("plus 200个月:"+ yearMonth.plusMonths(200));
System.out.println("-----------------------------------------");
//range
System.out.println("range Month:"+ yearMonth.range(ChronoField.MONTH_OF_YEAR));
System.out.println("-----------------------------------------");
//until
System.out.println(yearMonth);
System.out.println(localDate);
System.out.println("until:"+yearMonth.until(localDate, ChronoUnit.YEARS));
System.out.println("-----------------------------------------");
//with
System.out.println("withYear :"+yearMonth.withYear(10));
System.out.println("-----------------------------------------");
}
}
输出:
yearMonth:2021-07
LocalDate:2020-01-02
adjustInto:2021-07-02
-----------------------------------------
atDay:2021-07-06
-----------------------------------------
atEndOfMonth:2021-07-31
-----------------------------------------
yearMonth.compareTo(yearMonth2) :6
yearMonth2.compareTo(yearMonth) :-6
-----------------------------------------
yearMonth.equals(yearMonth2) :false
yearMonth.equals(yearMonth3) :true
-----------------------------------------
format :21年7月
-----------------------------------------
from :2020-01
-----------------------------------------
get Year :2021
get Month :7
-----------------------------------------
getLong Year :2021
-----------------------------------------
getMonth :JULY
-----------------------------------------
isValidDay :false
isValidDay :true
-----------------------------------------
2021-07 minus : 3年 = 2018-07
2021-07 minusMonths : 3个月 = 2021-04
-----------------------------------------
parse :2020-06
parse :2021-07
-----------------------------------------
plus 2个月:2021-09
plus 200个月:2038-03
-----------------------------------------
range Month:1 - 12
-----------------------------------------
2021-07
2020-01-02
until:-1
-----------------------------------------
withYear :0010-07
-----------------------------------------