一、api介紹
擷取目前系統日期
LocalDate now = LocalDate.now();
System.out.println(now);
結果:
2019-01-22
建立一個日期
建立日期為2013-09-10,參數為年、月、日
LocalDate date1 = LocalDate.of(2013, 9, 10);
System.out.println(date1);
結果:
2013-09-10
建立一個日期,1999年的第100天
LocalDate date2 = LocalDate.ofYearDay(1999, 100);
System.out.println(date2);
結果:
1999-04-10
建立一個日期,距離1970-1-1紀元10000天
LocalDate date3 = LocalDate.ofEpochDay(10000);
System.out.println(date3);
結果:
1997-05-19
将日期字元串轉為LocalDate
字元串格式嚴格參照yyyy-MM-dd
LocalDate parse1 = LocalDate.parse("2015-01-01");
//LocalDate parse2 = LocalDate.parse("2015-1-01");//報錯
//LocalDate parse3 = LocalDate.parse("2015-01-1");//報錯
//LocalDate parse4 = LocalDate.parse("15-01-1");//報錯
結果:
2015-01-01
擷取目前日期的年份
LocalDate now = LocalDate.now();
int year = now.getYear();
System.out.println(year);
結果:
2019
擷取目前日期的月份
LocalDate now = LocalDate.now();
int month = now.getMonthValue();
System.out.println(month);
結果:
1
擷取目前日期的月中天數
LocalDate now = LocalDate.now();
int day = now.getDayOfMonth();
System.out.println(day);
結果:
22
擷取目前日期的年中天數
LocalDate now = LocalDate.now();
int yearday = now.getDayOfYear();
System.out.println(yearday);
結果:恰巧是首月,和月中天數一天
22
目前日期是星期幾
LocalDate now = LocalDate.now();
DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.println(dayOfWeek);
結果:
TUESDAY
判斷這個日期的年份是不是閏年
LocalDate now = LocalDate.now();
boolean leapYear = now.isLeapYear();
System.out.println(leapYear);
結果:
false
這個日期的月份有多少天
LocalDate now = LocalDate.now();
int lengthOfMonth = now.lengthOfMonth();
System.out.println(lengthOfMonth);
結果:
31
這個日期的年份有多少天
LocalDate now = LocalDate.now();
int lengthOfYear = now.lengthOfYear();
System.out.println(lengthOfYear);
結果:
365
在目前日期上修改年份,傳回一個新的日期
LocalDate now = LocalDate.now();
LocalDate withYear = now.withYear(1999);
System.out.println(withYear);
結果:
1999-01-22
在目前日期上修改年份,傳回一個新的日期
LocalDate now = LocalDate.now();
LocalDate withYear = now.withYear(1999);
System.out.println(withYear);
結果:
1999-01-22
在目前日期上修改月份,傳回一個新的日期
LocalDate now = LocalDate.now();
LocalDate withMonth = now.withMonth(12);
System.out.println(withMonth);
結果:
2019-12-22
在目前日期上修改月份中的天數,傳回一個新的日期
LocalDate now = LocalDate.now();
LocalDate withDayOfMonth = now.withDayOfMonth(31);
System.out.println(withDayOfMonth);
結果:
2019-01-31
在目前日期上修改年份中的天數,傳回一個新的日期
LocalDate now = LocalDate.now();
LocalDate withDayOfYear = now.withDayOfYear(60);
System.out.println(withDayOfYear);
結果:
2019-03-01
日期的加法運算
在此日期基礎上加年份
LocalDate now = LocalDate.now();
LocalDate plusYears = now.plusYears(200);
System.out.println(plusYears);
結果:2019年加200年=2219
2219-01-22
在此日期基礎上加月份
LocalDate now = LocalDate.now();
LocalDate plusMonths = now.plusMonths(1);
System.out.println(plusMonths);
結果:2019-01-22加一個月
2019-02-22
在此日期上加天數
LocalDate now = LocalDate.now();
LocalDate plusDays = now.plusDays(8);
System.out.println(plusDays);
結果:2019-01-22加8天
2019-01-30
注意:我們在2019-01-30的基礎上加一個月呢,結果是什麼
LocalDate plusMonths2 = plusDays.plusMonths(1);
System.out.println(plusMonths2);
結果:是二月份的最後一天
2019-02-28
在日期的基礎上設定時間
設定小時和分鐘
LocalDate now = LocalDate.now();
LocalDateTime atTime = now.atTime(11, 11);
System.out.println(atTime);
結果:
2019-01-22T11:11
此日期的開始時間
LocalDate now = LocalDate.now();
LocalDateTime atStartOfDay = now.atStartOfDay();
System.out.println(atStartOfDay);
結果:
2019-01-22T00:00
距離紀元11970-01-01的天數
LocalDate now = LocalDate.now();
long epochDay = now.toEpochDay();
System.out.println(epochDay);
結果:
17918
二、利用LocalDate實作一個月曆
public static void main(String[] args) {
//利用localDate實作一個月曆
//擷取目前日期
LocalDate now = LocalDate.now();//2019-01-23
//擷取日期的年份,用于月曆标題
int year = now.getYear();
//擷取日期的月份,用于月曆标題
int month = now.getMonthValue();
System.out.println(year+"年-"+month+"月的月曆");//月曆标題
//擷取日期的月中日,用于定位月初日
int today = now.getDayOfMonth();
//擷取月初日期
LocalDate monthFristDay = now.minusDays(today-1);//2019-01-01
//System.out.println(monthFristDay);
//判斷月初日是星期幾
DayOfWeek dayOfWeek = monthFristDay.getDayOfWeek();
//輸出星期格式
System.out.println("MON TUE WED THU FRI SAT SUN");
//月初與星期格式對上
for(int i = 1;i<dayOfWeek.getValue();i++) {
System.out.print(" ");
}
//循環這個月日期
while(monthFristDay.getMonthValue() == month) {
//在月曆上輸出日期
System.out.printf("%3d",monthFristDay.getDayOfMonth());
//如果日期是當天就标*号
if(monthFristDay.getDayOfMonth() == today) {
System.out.print("*");
}else {
System.out.print(" ");
}
//如果日期是星期天,換行
if(monthFristDay.getDayOfWeek().getValue() ==7) {
System.out.println();
}
monthFristDay= monthFristDay.plusDays(1);
}
}
結果: