2012-08-08 60 views
7

是否可以計算jodatime中的一個月的週數?在jodatime中計算一個月的週數

我需要的是這樣的:

:7月27年的

  • 周; 1-7。 7月
  • 28周的一週; 9-14。 7月
  • 29周的一週; 16-21。 7月
  • 30周的一週; 23-31。七月

月:當年8月31

  • 周; 1-4。 Aug
  • 32周的第一週; 6-11。 Aug
  • 33周的第一週; 13-18。 Aug
  • 34周的一週; 20-25。 Aug
  • 35周的一週; 27-31。八月

我知道,我可以得到新年的一週約達時間是這樣的:

new LocalDate().weekOfWeekYear()

但我不知道如何獲得相關的日期。

回答

8

要檢索一週的範圍,只需創建一個指向一週的第一天和最後一天的對象,然後從該對象中抽出一天中的某一天。

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(); 

然後,您可以用同樣的方法在一個月內檢索周爲好。

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

也許你可能要限制的開始和結束日期停留在每月的範圍內,否則你可能會得到像'30 - 9月5日」。

+0

這不僅不能回答這個問題(雖然它提供了很好的靈感),但它包含2個錯誤:邏輯表明weekEnd意味着被派生出來從lastDay,這是未使用的,不是第一天;此外,答案指的是未定義的變量「月」。我試圖解決公然(但可能令人困惑)的錯誤被脾氣暴躁的評論者拒絕。 – Lambart 2014-05-04 00:52:33

+0

@蘭巴特的確你是對的。我修復了錯誤並添加了一段,希望能更好地回答這個問題。不要讓編輯讓你失望:) – 2014-05-05 09:08:45

+0

很好的編輯,並感謝您的鼓勵。 :) – Lambart 2014-05-05 18:25:57

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); 
1

我的解決辦法,考慮到其他的答案

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; 
     } 
    } 
0

我想這也是工作,以找到每個星期

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() +""};