久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

java - 獲取給定月份每周的開始日期和結束日期

java - to get the start date and end date of each week for the given month(java - 獲取給定月份每周的開始日期和結束日期)
本文介紹了java - 獲取給定月份每周的開始日期和結束日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

以下是我用來計算給定月份的周開始日期和結束日期的代碼.假設周開始日是星期一,周末結束日是星期日.例如,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 日第二周 - 2013 年 1 月 7 日至 2013 年 1 月 13 日第三周 - 2013 年 1 月 14 日至 2013 年 1 月 20 日第四周 - 2013 年 1 月 21 日至 2013 年 1 月 27 日第五周 - 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;
      }

我仍在努力.誰能幫我解決這個問題?

I am still working on this. Can anyone please help me in fixing this?

推薦答案

我用下面的代碼得到答案

I get the answer with the following code

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 - 獲取給定月份每周的開始日期和結束日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 激情六月丁香婷婷 | a久久久久久 | 美女福利视频一区 | 久久久久久国产精品免费免费狐狸 | 国产精品一区二区av | 99re视频| 中文字幕国产精品 | 黄色大全免费看 | 国产午夜精品久久久 | 国产高清久久 | 国产91 在线播放 | www.伊人.com| 成人在线精品视频 | 男女国产网站 | 久久久久久久久国产 | 国产精品亚洲精品 | 成人在线中文字幕 | 欧美老少妇一级特黄一片 | 国产视频一二三区 | 一区二区精品视频 | 久草福利 | 亚洲毛片在线观看 | 日本在线免费视频 | 久草网址 | 在线观看中文字幕 | 久久亚洲一区二区三区四区 | 日韩一区二区三区视频 | 久草视频在线播放 | 夜夜操天天操 | 亚洲欧美综合网 | 97久久精品午夜一区二区 | 日韩国产在线观看 | 久久精品成人 | 久久久久久亚洲精品 | 日日日干干干 | 美女网站视频免费黄 | 亚洲免费在线视频 | 美女中文字幕视频 | 龙珠z国语版在线观看 | 九九伦理电影 | 国产一区二区久久久 |