获取某年N周变异量后年周列表以及某个周对应开始结束日期

逛别人博客改自己代码,我只是个代码搬运工,用到业务场景获取当年后4周的周数,以及选择其中某个周 获取周对应开始结束日期,通业务场景网上找代码麻烦,所以方便其他兄弟,贴出全部代码如下:

public class WeekUtils {

public static void main(String[] args)

{

System.out.println(getWeekAndYear("2020-06-16"));

        System.out.println(getStartDayOfWeekNo(2020,53));

        System.out.println(getEndDayOfWeekNo(2020,53));

        getWeeksAndYear(2020,25,1);

        getWeeksAndYear("2020-12-31",4);

        System.out.println(getDateByYearweek("20212"));

    }

/**

* 当前日期所在一年中的第几周

    * @param date

    * @return

    */

    public static MapgetWeekAndYear(String date) {

Map result =new HashMap();

        Calendar cal = Calendar.getInstance();

        //设置一周的开始,默认是周日,这里设置成星期一

        cal.setFirstDayOfWeek(Calendar.MONDAY);

        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat formatMon =new SimpleDateFormat("MM");

        SimpleDateFormat formatYear =new SimpleDateFormat("yyyy");

        Date d =null;

        try {

d = format.parse(date);

        }catch (ParseException e) {

e.printStackTrace();

        }

cal.setTime(d);

        int month = Integer.valueOf(formatMon.format(d));

        int year = Integer.valueOf(formatYear.format(d));

        int week = cal.get(Calendar.WEEK_OF_YEAR);

        result.put("week", week);

        if(week ==1 && month ==12){

result.put("year", year +1);

        }else{

result.put("year", year);

        }

return result;

    }

/**

* 某个日期所在年周 偏移量是指获取多少个年周

    * @param date

    * @param offset

    * @return

    */

    public static ListgetWeeksAndYear(String date, int offset) {

if(StringUtils.isBlank(date))

{

date = DateUtils.format(new Date());

        }

Calendar cal = Calendar.getInstance();

        //设置一周的开始,默认是周日,这里设置成星期一

        cal.setFirstDayOfWeek(Calendar.MONDAY);

        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat formatMon =new SimpleDateFormat("MM");

        SimpleDateFormat formatYear =new SimpleDateFormat("yyyy");

        Date d =null;

        try {

d = format.parse(date);

        }catch (ParseException e) {

e.printStackTrace();

        }

cal.setTime(d);

        int month = Integer.valueOf(formatMon.format(d));

        int year = Integer.valueOf(formatYear.format(d));

        int week = cal.get(Calendar.WEEK_OF_YEAR);

        if(week ==1 && month ==12){

year = year +1;

        }

Calendar cal1 =getCalendarFormYear(year);

        List ywList =new ArrayList<>();

        for(int i =1 ;i<= offset;i++)

{

cal1.set(Calendar.WEEK_OF_YEAR, week + i);

            int jsweek = cal1.get(Calendar.WEEK_OF_YEAR);

            int jsyear = cal1.get(Calendar.YEAR);

            //System.out.println("item:"+jsyear+""+jsweek);

            String sjyw = jsyear+""+jsweek;

            ywList.add(sjyw);

        }

return ywList;

    }

/**

* 根据年周查询对应的周一和周日的日期

    * @param yearweek

    * @return

    */

    public static ListgetDateByYearweek(String yearweek){

String year = yearweek.substring(0,4);

        String weekno = yearweek.substring(4,yearweek.length());

        List dateList =new ArrayList<>();

        String startDateStr =getStartDayOfWeekNo(Integer.parseInt(year),Integer.parseInt(weekno));

        String endDateStr =getEndDayOfWeekNo(Integer.parseInt(year),Integer.parseInt(weekno));

        dateList.add(startDateStr);

        dateList.add(endDateStr);

        return dateList;

    }

/**

* 获取某年某周后多少周是第几周

    * @param year

    * @param weekno

    * @param offset

    */

    public static void getWeeksAndYear(int year, int weekno, int offset) {

Calendar cal =getCalendarFormYear(year);

        cal.set(Calendar.WEEK_OF_YEAR, weekno + offset);

        int week = cal.get(Calendar.WEEK_OF_YEAR);

        int jsyear = cal.get(Calendar.YEAR);

        System.out.println(jsyear+""+week);

    }

/**

* get Calendar of given year

    * @param year

    * @return

    */

    private static CalendargetCalendarFormYear(int year){

Calendar cal = Calendar.getInstance();

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        cal.set(Calendar.YEAR, year);

        return cal;

    }

/**

* get start date of given week no of a year

    * @param year

    * @param weekNo

    * @return

    */

    public static StringgetStartDayOfWeekNo(int year,int weekNo){

Calendar cal =getCalendarFormYear(year);

        cal.set(Calendar.WEEK_OF_YEAR, weekNo);

        return cal.get(Calendar.YEAR) +"-" + (cal.get(Calendar.MONTH) +1) +"-" +

cal.get(Calendar.DAY_OF_MONTH);

    }

/**

* get the end day of given week no of a year.

    * @param year

    * @param weekNo

    * @return

    */

    public static StringgetEndDayOfWeekNo(int year,int weekNo){

Calendar cal =getCalendarFormYear(year);

        cal.set(Calendar.WEEK_OF_YEAR, weekNo);

        cal.add(Calendar.DAY_OF_WEEK, 6);

        return cal.get(Calendar.YEAR) +"-" + (cal.get(Calendar.MONTH) +1) +"-" +

cal.get(Calendar.DAY_OF_MONTH);

    }

你可能感兴趣的:(获取某年N周变异量后年周列表以及某个周对应开始结束日期)