在jodatime中计算一个月的周数

时间:2012-08-08 13:04:20

标签: java jodatime

是否有可能在jodatime中计算一个月的周数?

我需要这样的东西:

月份:7月

  • 27年周; 1-7。七月
  • 28年周; 9-14。七月
  • 29年周; 16-21。七月
  • 30年周; 23-31。七月

月:8月

  • 31年周; 1-4。月
  • 32年周; 6-11。月
  • 第33年周; 13-18。月
  • 34年周; 20-25。月
  • 35年周; 27-31。月

我知道我可以像这样在joda时间获得一周的一周:

new LocalDate().weekOfWeekYear()

但我不知道如何获得相关日期。

4 个答案:

答案 0 :(得分:9)

要检索一周的范围,只需创建一个指向一周的第一天和最后一天的对象,然后从中提取月份。

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日”之类的内容。

答案 1 :(得分: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);

答案 2 :(得分: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;
        }
    }

答案 3 :(得分: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() +""};