7

Is it possible to calculate the weeks of one month in jodatime?

I need something like this:

Month: July

  • Week of Year 27; 1-7. July
  • Week of Year 28; 9-14. July
  • Week of Year 29; 16-21. July
  • Week of Year 30; 23-31. July

Month: August

  • Week of Year 31; 1-4. Aug
  • Week of Year 32; 6-11. Aug
  • Week of Year 33; 13-18. Aug
  • Week of Year 34; 20-25. Aug
  • Week of Year 35; 27-31. Aug

I know that i can get the week of Year in joda time like this:

new LocalDate().weekOfWeekYear()

But i don´t know how to get the related dates.

4

4 に答える 4

10

週の範囲を取得するには、週の最初と最後の日を指すオブジェクトを作成し、そこから月の日を取得します。

int weekOfYear = 32;

LocalDate firstDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(1);
LocalDate lastDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(7);

System.out.println("Week of Year "+weekOfYear+"; "+firstDay.toString("d MMM")+" - "+lastDay.toString("d MMM"));

次のように日を抽出することもできます。

int weekStart = firstDay.getDayOfMonth();
int weekEnd = lastDay.getDayOfMonth();

次に、同じ手法を使用して、1か月の週を取得することもできます。

int firstWeekInMonth = new LocalDate().withMonthOfYear(month).withDayOfMonth(1).getWeekOfYear();
int lastWeekInMonth = new LocalDate().withMonthOfYear(month).dayOfMonth().withMaximalValue().getWeekOfYear();

おそらく、開始日と終了日を月の範囲内にとどめるように制限することをお勧めします。そうしないと、「9月30日から5日」のようなものが得られる可能性があります。

于 2012-08-08T13:19:47.147 に答える
1

いくつかの変数の修正:

int weekOfYear = 32;

LocalDate firstDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(1);
LocalDate lastDay = new LocalDate().withWeekOfWeekyear(weekOfYear).withDayOfWeek(6);

int weekStart = firstDay.getDayOfMonth();    
int weekEnd = lastDay.getDayOfMonth();

System.out.println("Week of Year "+weekOfYear+"; "+weekStart+"-"+weekEnd+" "+month);
于 2013-03-14T18:40:08.960 に答える
1

My solution, considering the other answers

 public List<Pair<LocalDate, LocalDate>> getWeeksInMonth(LocalDate data) {

        int firstWeekInMonth = data.withDayOfMonth(1).getWeekOfWeekyear();
        int lastWeekInMonth = data.dayOfMonth().withMaximumValue().getWeekOfWeekyear();

        List<Pair<LocalDate, LocalDate>> weeks = new cicero.minhasfinancas.util.array.ArrayList<>();
        while (firstWeekInMonth <= lastWeekInMonth) {

            LocalDate firstDay = new LocalDate().withWeekOfWeekyear(firstWeekInMonth).withDayOfWeek(1);
            LocalDate lastDay = new LocalDate().withWeekOfWeekyear(firstWeekInMonth).withDayOfWeek(7);
            weeks.add(new Pair<>(firstDay, lastDay));
            firstWeekInMonth++;
        }

        return weeks;
    }

    public class Pair<V1, V2>{

        V1 first;
        V2 second;

        public Pair(V1 first, V2 second) {
            this.first = first;
            this.second = second;
        }

        public V1 getFirst() {
            return first;
        }

        public void setFirst(V1 first) {
            this.first = first;
        }

        public V2 getSecond() {
            return second;
        }

        public void setSecond(V2 second) {
            this.second = second;
        }
    }
于 2015-06-10T02:26:45.853 に答える
0

I think this is also work to find each weeks

String[] weekDays = {today.withDayOfWeek(DateTimeConstants.MONDAY).getDayOfMonth() +"",
            today.withDayOfWeek(DateTimeConstants.TUESDAY).getDayOfMonth()+ "",
            today.withDayOfWeek(DateTimeConstants.WEDNESDAY).getDayOfMonth() + "",
            today.withDayOfWeek(DateTimeConstants.THURSDAY).getDayOfMonth() + "",
            today.withDayOfWeek(DateTimeConstants.FRIDAY).getDayOfMonth() +"",
            today.withDayOfWeek(DateTimeConstants.SATURDAY).getDayOfMonth() +"",
            today.withDayOfWeek(DateTimeConstants.SUNDAY).getDayOfMonth() +""};
于 2015-12-01T14:01:44.163 に答える