开发者

How obtain first/last day of an incomplete week in Java

and thanks for reading me.

I have a little problem that is that I need to Know the first/last day of a week in a month and a year, so:

public String getFirstDayOfWeekAndMonth(int year, int month, int week){
   Calendar weekCalenda开发者_如何转开发r = 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... ?
}

For example for the next calendar:

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
                     ...

I have problems with the weeks that are in 2 months (on example 5 and 9). Could you help me please?

Thank You very much.


I recommend using the brilliant Joda-Time library for all your timing needs (which I also recommend to make a standard import in all you projects).

With that, a (not very clean, yet working) solution would be this:

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

I don't quite get why you would want to return a String from your method, but I guess you have a reason. I just assumed a format, you can of course change it if you want.


Maybe a simple logical test?

For first day of the week:

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

and for the last day of the week

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


Well, I have develop a method that returns the first and the last day, wothin any other library:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜