JDK8中新增了很多時間類、日期類、時間日期類用於處理時間、日期。有興趣的小夥伴可以看《JDK8實戰》這本書,希望對各位有幫助。
本篇博客主要想各位講述如何使用LocalDate類獲取一整月的日期。我們知道,年分閏年、平年,月分大月、小月。閏年2月有29天,平年2月有28天,按照傳統的JDK去處理日期,寫起來非常的麻煩!JDK8幫我們很好的解決了這個問題。
代碼如下:
package com.date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
public class MonthDays {
public static void main(String[] args) {
test01();
System.out.println("---------------------- 分割線1 ----------------------");
getMonthDays(201902);
System.out.println("---------------------- 分割線2 ----------------------");
getMonthDays(202002);
System.out.println("---------------------- 分割線3 ----------------------");
getMonthDays(201903);
}
/**
* 獲取每月的所有日期,日期格式爲:yyyyMMdd
*
* @param billingCycle 賬期,例如:201910
* @return
*
* @author moon 2019/12/17 23:59
*/
public static List<Integer> getMonthDays(Integer billingCycle) {
List<Integer> monthDays = new ArrayList<Integer>();
int startNum = 1;
LocalDate localDate = LocalDate.parse(billingCycle + "01", DateTimeFormatter.ofPattern("yyyyMMdd"));
System.out.println("localDate爲:" + localDate);
int lastNum = localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
System.out.println("lastNum爲:" + lastNum);
Integer firstDay = new Integer(localDate.format(DateTimeFormatter.BASIC_ISO_DATE));
System.out.println("firstDay爲:" + firstDay);
// monthDays.add(firstDay);
for (int i = startNum; i <= lastNum; i++) {
monthDays.add(firstDay);
firstDay++;
}
System.out.println("monthDays爲:" + monthDays);
return monthDays;
}
/**
* 求取每月中的第一天,最後一天。
*
* @author moon 2019/12/17 23:55
*/
public static void test01() {
LocalDate localDate = LocalDate.parse("201902" + "01", DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate localDate1 = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate localDate2 = localDate.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("localDate1爲:" + localDate1);
System.out.println("localDate2爲:" + localDate2);
System.out.println(localDate.getDayOfMonth()); // getDayOfMonth是獲取當前日期中的天數,例如2019-02-14,獲取的值爲14.
System.out.println(localDate.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println(localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth());
}
}
運行結果如下,請仔細看日誌。
localDate1爲:2019-02-01
localDate2爲:2019-02-28
1
2019-02-28
28
---------------------- 分割線1 ----------------------
localDate爲:2019-02-01
lastNum爲:28
firstDay爲:20190201
monthDays爲:[20190201, 20190202, 20190203, 20190204, 20190205, 20190206, 20190207, 20190208, 20190209, 20190210, 20190211, 20190212, 20190213, 20190214, 20190215, 20190216, 20190217, 20190218, 20190219, 20190220, 20190221, 20190222, 20190223, 20190224, 20190225, 20190226, 20190227, 20190228]
---------------------- 分割線2 ----------------------
localDate爲:2020-02-01
lastNum爲:29
firstDay爲:20200201
monthDays爲:[20200201, 20200202, 20200203, 20200204, 20200205, 20200206, 20200207, 20200208, 20200209, 20200210, 20200211, 20200212, 20200213, 20200214, 20200215, 20200216, 20200217, 20200218, 20200219, 20200220, 20200221, 20200222, 20200223, 20200224, 20200225, 20200226, 20200227, 20200228, 20200229]
---------------------- 分割線3 ----------------------
localDate爲:2019-03-01
lastNum爲:31
firstDay爲:20190301
monthDays爲:[20190301, 20190302, 20190303, 20190304, 20190305, 20190306, 20190307, 20190308, 20190309, 20190310, 20190311, 20190312, 20190313, 20190314, 20190315, 20190316, 20190317, 20190318, 20190319, 20190320, 20190321, 20190322, 20190323, 20190324, 20190325, 20190326, 20190327, 20190328, 20190329, 20190330, 20190331]