MonthDay是一个不变的日期时间对象,代表月和日的组合。其格式是:
–MM-dd(这里开头有两个横杠),格式不是我们常用的格式:MM-dd。
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
public class MonthDayTest {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2020,1,2);
MonthDay monthDay = MonthDay.now();
System.out.println("monthDay:"+monthDay);
Temporal temporal = monthDay.adjustInto(localDate);
System.out.println("LocalDate:"+localDate);
System.out.println("adjustInto:"+temporal);
System.out.println("-----------------------------------------");
LocalDate atYear = monthDay.atYear(2016);
System.out.println("atYear:"+atYear);
System.out.println("-----------------------------------------");
//compareTo注意点:比较的是两个年月中间查的月份数,并不是大于=1 相等=0 小于=-1
MonthDay monthDay2 = MonthDay.of(10, 10);
int i = monthDay.compareTo(monthDay2);
System.out.println("monthDay.compareTo(monthDay2) :"+ i);
int i2 = monthDay2.compareTo(monthDay);
System.out.println("monthDay2.compareTo(monthDay) :"+ i2);
System.out.println("-----------------------------------------");
//equals
System.out.println("monthDay.equals(monthDay2) :"+ monthDay.equals(monthDay2));
MonthDay monthDay3 = MonthDay.of(12, 7);
System.out.println("monthDay.equals(monthDay3) :"+ monthDay.equals(monthDay3));
System.out.println("-----------------------------------------");
//format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M月d日");
String format = monthDay.format(dtf);
System.out.println("format :"+ format);
System.out.println("-----------------------------------------");
MonthDay from = MonthDay.from(localDate);
System.out.println("from :"+ from);
System.out.println("-----------------------------------------");
//get
System.out.println("get Year :"+ monthDay.get(ChronoField.MONTH_OF_YEAR));
//get MonthDay 不支持 monthDay.get(ChronoField.MICRO_OF_DAY)
//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MicroOfDay
//System.out.println("get Month :"+ monthDay.get(ChronoField.MICRO_OF_DAY));
System.out.println("-----------------------------------------");
//getLong
System.out.println("getLong :"+ monthDay.getLong(ChronoField.MONTH_OF_YEAR));
System.out.println("getLong :"+ monthDay.getLong(ChronoField.DAY_OF_MONTH));
//monthDay 不支持 getLong(ChronoField.SECOND_OF_DAY)
//java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: SecondOfDay
//System.out.println("getLong :"+ monthDay.getLong(ChronoField.SECOND_OF_DAY));
System.out.println("-----------------------------------------");
//getMonth
System.out.println("getMonth :"+ monthDay.getMonth());
System.out.println("-----------------------------------------");
//isValidDay
//MonthDay.of(2021, 6) 报错:java.time.DateTimeException: Invalid value for MonthOfYear: 2021
MonthDay monthDay4 = MonthDay.of(12, 6);
boolean validDay = monthDay4.isValidYear(2016);
System.out.println("isValidDay :"+ validDay);
boolean validDay2 = monthDay4.isValidYear(2017);
System.out.println("isValidDay :"+ validDay2);
System.out.println("-----------------------------------------");
//parse 支持的默认格式:--MM-dd
System.out.println("parse :"+ MonthDay.parse("--06-06"));
//parse 不是默认格式:yyyy-MM,则报错
//System.out.println("parse :"+ YearMonth.parse("2020-6"));
System.out.println("parse :"+ MonthDay.parse("7月31日",dtf));
System.out.println("-----------------------------------------");
//range
System.out.println("range Month:"+ monthDay.range(ChronoField.MONTH_OF_YEAR));
System.out.println("-----------------------------------------");
//with
System.out.println("withYear :"+monthDay.with(Month.MARCH));
System.out.println("-----------------------------------------");
}
}
输出:
monthDay:--07-16
LocalDate:2020-01-02
adjustInto:2020-07-16
-----------------------------------------
atYear:2016-07-16
-----------------------------------------
monthDay.compareTo(monthDay2) :-3
monthDay2.compareTo(monthDay) :3
-----------------------------------------
monthDay.equals(monthDay2) :false
monthDay.equals(monthDay3) :false
-----------------------------------------
format :7月16日
-----------------------------------------
from :--01-02
-----------------------------------------
get Year :7
-----------------------------------------
getLong :7
getLong :16
-----------------------------------------
getMonth :JULY
-----------------------------------------
isValidDay :true
isValidDay :true
-----------------------------------------
parse :--06-06
parse :--07-31
-----------------------------------------
range Month:1 - 12
-----------------------------------------
withYear :--03-16
-----------------------------------------