private void updateCalendarLocale(Locale l) {
    int oldFirstDayOfWeek = calendar.getFirstDayOfWeek();
    setLocale(l);
    calendarComponent.setLocale(l);
    calendar = new GregorianCalendar(l);
    int newFirstDayOfWeek = calendar.getFirstDayOfWeek();

    // we are showing 1 week, and the first day of the week has changed
    // update start and end dates so that the same week is showing
    if (viewMode == Mode.WEEK && oldFirstDayOfWeek != newFirstDayOfWeek) {
      calendar.setTime(calendarComponent.getStartDate());
      calendar.add(java.util.Calendar.DAY_OF_WEEK, 2);
      // starting at the beginning of the week
      calendar.set(GregorianCalendar.DAY_OF_WEEK, newFirstDayOfWeek);
      Date start = calendar.getTime();

      // ending at the end of the week
      calendar.add(GregorianCalendar.DATE, 6);
      Date end = calendar.getTime();

      calendarComponent.setStartDate(start);
      calendarComponent.setEndDate(end);

      // Week days depend on locale so this must be refreshed
      setWeekendsHidden(hideWeekendsButton.getValue());
    }
  }
 private void rollWeek(int direction) {
   calendar.add(GregorianCalendar.WEEK_OF_YEAR, direction);
   calendar.set(GregorianCalendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
   resetCalendarTime(false);
   resetTime(true);
   calendar.add(GregorianCalendar.DATE, 6);
   calendarComponent.setEndDate(calendar.getTime());
 }
 private void setWeekendsHidden(boolean weekendsHidden) {
   if (weekendsHidden) {
     int firstToShow = (GregorianCalendar.MONDAY - calendar.getFirstDayOfWeek()) % 7;
     calendarComponent.setFirstVisibleDayOfWeek(firstToShow + 1);
     calendarComponent.setLastVisibleDayOfWeek(firstToShow + 5);
   } else {
     calendarComponent.setFirstVisibleDayOfWeek(1);
     calendarComponent.setLastVisibleDayOfWeek(7);
   }
 }
  private void updateCalendarTimeZone(Object timezoneId) {
    TimeZone tz = null;
    if (!DEFAULT_ITEMID.equals(timezoneId)) {
      tz = TimeZone.getTimeZone((String) timezoneId);
    }

    // remember the week that was showing, so we can re-set it later
    Date startDate = calendarComponent.getStartDate();
    calendar.setTime(startDate);
    int weekNumber = calendar.get(java.util.Calendar.WEEK_OF_YEAR);
    calendarComponent.setTimeZone(tz);
    calendar.setTimeZone(calendarComponent.getTimeZone());

    if (viewMode == Mode.WEEK) {
      calendar.set(java.util.Calendar.WEEK_OF_YEAR, weekNumber);
      calendar.set(java.util.Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

      calendarComponent.setStartDate(calendar.getTime());
      calendar.add(java.util.Calendar.DATE, 6);
      calendarComponent.setEndDate(calendar.getTime());
    }
  }
Example #5
0
  public static void main(String[] args) {
    // construct d as current date
    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);
    int year = d.get(Calendar.YEAR);

    System.out.println("\t" + month + "月\t" + year);

    // set d to start date of the month
    d.set(Calendar.DAY_OF_MONTH, 1);

    int weekday = d.get(Calendar.DAY_OF_WEEK);

    // get first day of week(Sunday in the U.S)
    int firstDayOfWeek = d.getFirstDayOfWeek();

    // determine the required indentation for the first line
    int indent = 0;
    while (weekday != firstDayOfWeek) {
      indent++;
      d.add(Calendar.DAY_OF_MONTH, -1);
      weekday = d.get(Calendar.DAY_OF_WEEK);
    }

    // print weekday names
    String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();

    do { // getShortWeekdays()方法生成的String型数组是以星期几(本质就是数值常量,星期日为1)
      // 为索引,测试得到weekdayNames[0]为空
      // 所以此循环可以直接用for each循环代替
      System.out.printf("%4s", weekdayNames[weekday]);
      d.add(Calendar.DAY_OF_MONTH, 1);
      weekday = d.get(Calendar.DAY_OF_WEEK);
    } while (weekday != firstDayOfWeek);
    System.out.println();

    for (int i = 1; i <= indent; i++) System.out.print("    ");

    d.set(Calendar.DAY_OF_MONTH, 1);
    do {
      // print day
      int day = d.get(Calendar.DAY_OF_MONTH);
      System.out.printf("%3d", day);

      // mark current day with *
      if (day == today) System.out.print("*");
      else System.out.print(" ");

      // advance d to the next day
      d.add(Calendar.DAY_OF_MONTH, 1);
      weekday = d.get(Calendar.DAY_OF_WEEK);

      // start a new line at the start of the week
      if (weekday == firstDayOfWeek) System.out.println();
    } while (d.get(Calendar.MONTH) == month);
    // the loop exits when d is day 1 of the next month

    // print final end of line if necessary
    if (weekday != firstDayOfWeek) System.out.println();
  }