如何获取Java中不完整周的第一天/最后一天

时间:2011-09-15 12:39:41

标签: java algorithm date calendar

感谢您阅读我。

我有一个问题,就是我需要知道一周的第一天/最后一天一个月和一年,所以:

public String getFirstDayOfWeekAndMonth(int year, int month, int week){
   Calendar weekCalendar = Calendar.getInstance();
   weekCalendar.clear();
   weekCalendar.set( Calendar.YEAR, year );
   weekCalendar.set( Calendar.MONTH, month-1); // zero-based
   weekCalendar.set( Calendar.WEEK_OF_YEAR, week );

   return... ?
}

例如,下一个日历:

Month    Week     M   T   W   T   F   S   S           FirstDay    LastDay
          1               1   2   3   4   5              1           5
          2       6   7   8   9   10  11  12             6           12
  1       3       13  14  15  16  17  18  19             13          19
          4       20  21  22  23  24  25  26             20          26
          5       27  28  29  30                         27          30

          5                       1   2   3              1           3
          6       4   5   6   7   8   9   10             4           10
  2       7       11  12  13  14  15  16  17             11          17
          8       18  19  20  21  22  23  24             18          24
          9       25  26  27  28                         25          28

          9                       1   2   3              1           3
  3       10      4   5   6   7   8   9   10             4           10
                     ...

我遇到了2个月内的问题(例如5和9)。你能帮帮我吗?

非常感谢你。

3 个答案:

答案 0 :(得分:1)

我建议使用精彩的Joda-Time库来满足您的所有时间需求(我还建议您在所有项目中进行标准导入)。

有了这个,一个(不是很干净,但仍在工作)的解决方案就是这样:

public String getFirstDayOfWeekAndMonth(int year, int month, int week) {
    DateTime firstDayOfMonth = new DateTime(year, month, 1, 0, 0, 0, 1);
    DateTime lastDayOfMonth = new DateTime(year, month, firstDayOfMonth.dayOfMonth().getMaximumValueOverall(), 0, 0, 0, 1);

    // european style (MON - SUN)
    DateTime firstOfWeek = new DateTime(year, 1, 1, 0, 0, 0, 1).plusWeeks(week - 1).withDayOfWeek(1);
    DateTime lastOfWeek = firstOfWeek.withDayOfWeek(7);

    int firstDay;
    int lastDay;
    if(firstOfWeek.isBefore(firstDayOfMonth))
        firstDay = firstDayOfMonth.getDayOfMonth();
    else
        firstDay = firstOfWeek.getDayOfMonth();

    if(lastOfWeek.isAfter(lastDayOfMonth))
        lastDay = lastDayOfMonth.getDayOfMonth();
    else
        lastDay = lastOfWeek.getDayOfMonth();

    String returner = String.format("%d - %d", firstDay, lastDay);

    return returner;
}

我不明白你为什么要从你的方法中返回一个String,但我想你有理由。我只是假设一种格式,如果你愿意,你当然可以改变它。

答案 1 :(得分:0)

也许是一个简单的逻辑测试?

一周的第一天:

If (resultDate.month < input.month){
     resultDate = firstDayOf(input.month);
}

以及一周的最后一天

If (resultDate.month > input.month){
    resultDate = lastDayOf(input.month);
}

答案 2 :(得分:0)

好吧,我开发了一个返回第一天和最后一天的方法,在任何其他库中都有:

private static int getFirstDayOfWeek(int year,int month,int week){
    return getFirstLastDayOfWeek(true,year,month,week);
}

private static int getLastDayOfWeek(int year,int month,int week){
    return getFirstLastDayOfWeek(false,year,month,week);
}

private static int getFirstLastDayOfWeek(boolean first, int year,int month,int week){
    int exit = 0;
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.WEEK_OF_YEAR, week);
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month-1);
    if (!first)
        calendar.add(Calendar.DAY_OF_MONTH, 6);
    // 1st day of the week
    Date date = calendar.getTime();
    // The month and the day of the 1st day of the week
    int theMonth = Integer.valueOf( getInstance().getStrDate(date,"MM") );

    if (theMonth!=month)
        exit = (first?1:new GregorianCalendar(year, month-1, 1).getActualMaximum(Calendar.DAY_OF_MONTH));
    else
        exit = Integer.valueOf( getInstance().getStrDate(date,"d") );
    return exit;
}