JDK8, LocalDate求取一整月的日期。

       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]


 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章