JDK1.8 日期工具类,获取时间:本周第一天日期;各月份最后一天日期(天数,可查去年);某年份的开始日期;

package com.cyrj.common.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class GetFirstDateUtil {
    
    

    /**
     * 获取当月的第一天
     * @return
     */
    public static final String getFirstMonthDay(){
    
    
        LocalDate localDate =   LocalDate.now();
        LocalDate with = localDate.with(TemporalAdjusters.firstDayOfMonth());
        String firstDay = with+" 00:00:00";
        return firstDay;
    }

    /**
     * 获取本周第一天
     * @param num 0本周,1下周,-1上周,依次类推
     * @return
     */
    public static final String getFirstWeekDay(int num){
    
    
        LocalDate localDate = LocalDate.now().plusWeeks(num);
        LocalDate with = localDate.with(DayOfWeek.MONDAY);
        String firstDay = with+" 00:00:00";
        return firstDay;
    }

    /**
     * 获取某月最后一天
     * @return
     * */
    public static final int getLastDayOfMonth(int month){
    
    
        LocalDate today = LocalDate.now().withMonth(month);
        //某月的最后一天
        LocalDate lastDay =today.with(TemporalAdjusters.lastDayOfMonth());
        return lastDay.getDayOfMonth();
    }

	/**
	 * 获取某月最后一天,可查去年(-1去年,0今年,1明年,以此类推)
	 * @return
	 * */
	public static final int getLastDayOfMonth(int month,int year){
    
    
	    LocalDate localDatew = LocalDate.now().plusYears(year).withMonth(month);
	    //某月的最后一天
	    LocalDate lastDay =localDatew.with(TemporalAdjusters.lastDayOfMonth());
	    return lastDay.getDayOfMonth();
	}

    /**
     * 获取某年的开始日期
     *
     * @param offset 0今年,1明年,-1去年,依次类推
     * @return
     */
    public static LocalDate getYearStart(int offset) {
    
    
        return  LocalDate.now().plusYears(offset).with(TemporalAdjusters.firstDayOfYear());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39055025/article/details/101022500
今日推荐