其他分享
首页 > 其他分享> > LocalDate 简单工具类(根据日期,获取近两周的相关日期)

LocalDate 简单工具类(根据日期,获取近两周的相关日期)

作者:互联网

 

package com.betawoo.admin.commons.utils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class WeekDateUtil {

    public static void main(String[] args) {
        System.out.println(getWeeksInfo("2019-02-02"));
    }

    /**
     * 根据日期,获取近两周的相关日期
     */
    public static Map getWeeksInfo(String dateStr){

        Map result = new HashMap();

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate ld = LocalDate.parse(dateStr,df);

        //当前这周一
        LocalDate dateStart = ld
                .with(DayOfWeek.MONDAY);
        //下周周日
        LocalDate dateEnd = ld.minusWeeks(-1)
                .with(DayOfWeek.SUNDAY);

        //近两周开始和结束的日期
        result.put("dateStart",dateStart.toString());
        result.put("dateEnd",dateEnd.toString());
        result.put("dateStartDate",dateStart);
        result.put("dateEndDate",dateEnd);

        //开始/结束的号数
        result.put("start",dateStart.getDayOfMonth());
        result.put("end",dateEnd.getDayOfMonth());

        //是否跨月
        boolean beyond = false;
        if(!dateStart.getMonth().equals(dateEnd.getMonth())){
            beyond = true;
            System.out.println("跨月");
            // 获取上一个月总天数
            result.put("lengthOfMonth",dateStart.lengthOfMonth());
        }else{
            System.out.println("跨月");
        }
        result.put("beyondMonth",beyond);
        return result;
    }
}

 

标签:dateStart,java,两周,日期,dateEnd,result,put,LocalDate
来源: https://blog.csdn.net/hgg923/article/details/94137436