java - 获取给定月份的每周的开始日期和结束日期 [英] java - to get the start date and end date of each week for the given month

查看:178
本文介绍了java - 获取给定月份的每周的开始日期和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用于计算给定月份的星期开始日期和结束日期的代码。
假设周开始日是MONDAY,周结束日是SUNDAY。
例如,2013年1月将有5周。
如果月份从星期日开始 - 忽略该日

Following is the code that I am using to calculate the week start date and end date for the given month. Assume week start day is MONDAY and week end day is SUNDAY. For example, JANUARY, 2013 will have 5 weeks. If the month starts with Sunday - ignore that day

2013年1月
第一周 - 2012年12月31日至2013年1月6日
第二周 - 07-jan-2013至13-jan-2013
第三周 - 14-jan-2013至20-jan-2013
第四周 - 21-jan-2013至27- jan-2013
第五周 - 2013年1月28日至2013年2月3日

January 2013 first week - 31-Dec-2012 to 06-Jan-2013 second week - 07-jan-2013 to 13-jan-2013 Third week - 14-jan-2013 to 20-jan-2013 fourth week - 21-jan-2013 to 27-jan-2013 fifth week - 28-jan-2013 to 03-Feb-2013

public static void main(String[] args) {
    List<List<String>> weekdates = getNumberOfWeeks(2013, Calendar.JULY);
    for(List<String> weekDatesLoop:weekdates){
        System.out.println("Start day: "+weekDatesLoop.get(0).toString());
        System.out.println("End day: "+weekDatesLoop.get(1).toString());
    }
  }
public static List<List<String>> getNumberOfWeeks(int year, int month) {
        System.out.println("Month Id: "+month);
        month = month-1;
        System.out.println("Month Id: " + month);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        List<List<String>> weekdates = new ArrayList<List<String>>();

        List<String> dates = new ArrayList<String>();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, 1);
        dates.add(format.format(c.getTime()));
        //int numOfWeeksInMonth = 1;
        while (c.get(Calendar.MONTH) == month) {
          //System.out.println(c.get(Calendar.DAY_OF_WEEK) );

          if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            dates.add(format.format(c.getTime()));
            weekdates.add(dates);
          }
          else if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            dates = new ArrayList<String>();
            dates.add(format.format(c.getTime()));
            //numOfWeeksInMonth++;
          }
          c.add(Calendar.DAY_OF_MONTH, 1);
        }
        if(dates.size() < 2){
          c.add(Calendar.DAY_OF_MONTH, -1);
          dates.add(format.format(c.getTime()));
          weekdates.add(dates);
        }
        System.out.println(weekdates);
        return weekdates;
      }

我还在做这件事。

推荐答案

我得到以下代码的答案

List<List<String>> getNumberOfWeeks(int year, int month) {
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        List<List<String>> weekdates = new ArrayList<List<String>>();
        List<String> dates;
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, 1);
        while (c.get(Calendar.MONTH) == month) {
                dates = new ArrayList<String>();
              while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
                c.add(Calendar.DAY_OF_MONTH, -1);
              }
              dates.add(format.format(c.getTime()));
              c.add(Calendar.DAY_OF_MONTH, 6);
              dates.add(format.format(c.getTime()));
              weekdates.add(dates);
              c.add(Calendar.DAY_OF_MONTH, 1);
        }
        System.out.println(weekdates);
        return weekdates;
      }

这篇关于java - 获取给定月份的每周的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆