Languages Around The World

Calendar Examples

Calendar for Default Time Zone

These C++ and C examples get a Calendar based on the default time zone and add days to a date.


C++

UErrorCode status = U_ZERO_ERROR;
GregorianCalendar* gc = new GregorianCalendar(status);
if (U_FAILURE(status)) {
      puts("Couldn't create GregorianCalendar");
      return;
      }
      // set up the date
      gc->set(2000, Calendar::FEBRUARY, 26);
      gc->set(Calendar::HOUR_OF_DAY, 23);
      gc->set(Calendar::MINUTE, 0);
      gc->set(Calendar::SECOND, 0);
      gc->set(Calendar::MILLISECOND, 0);
      // Iterate through the days and print it out.
      for (int32_t i = 0; i < 30; i++) {
          // print out the date.
          // You should use the DateFormat to properly format it
          printf("year: %d, month: %d (%d in the implementation), day: %d\n",
                  gc->get(Calendar::YEAR, status),
                  gc->get(Calendar::MONTH, status) + 1,
                  gc->get(Calendar::MONTH, status),
                  gc->get(Calendar::DATE, status));
          if (U_FAILURE(status))
          {
              puts("Calendar::get failed");
              return;
          }
          // Add a day to the date
          gc->add(Calendar::DATE, 1, status);
          if (U_FAILURE(status)) {
              puts("Calendar::add failed");
              return;
          }
      }
      delete gc;


C

UErrorCode status = U_ZERO_ERROR;
int32_t i;
UCalendar *cal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &status);
if (U_FAILURE(status)) {
      puts("Couldn't create GregorianCalendar");
      return;
      }
      // set up the date
      ucal_set(cal, UCAL_YEAR, 2000);
      ucal_set(cal, UCAL_MONTH, UCAL_FEBRUARY);   /* FEBRUARY */
      ucal_set(cal, UCAL_DATE, 26);
      ucal_set(cal, UCAL_HOUR_OF_DAY, 23);
      ucal_set(cal, UCAL_MINUTE, 0);
      ucal_set(cal, UCAL_SECOND, 0);
      ucal_set(cal, UCAL_MILLISECOND, 0);
      // Iterate through the days and print it out.
      for (i = 0; i < 30; i++) {
          // print out the date.
          // You should use the udat_* API to properly format it
          printf("year: %d, month: %d (%d in the implementation), day: %d\n",
              ucal_get(cal, UCAL_YEAR, &status),
              ucal_get(cal, UCAL_MONTH, &status) + 1,
              ucal_get(cal, UCAL_MONTH, &status),
              ucal_get(cal, UCAL_DATE, &status));
          if (U_FAILURE(status)) {
          puts("Calendar::get failed");
          return;
          }
          // Add a day to the date
          ucal_add(cal, UCAL_DATE, 1, &status);
          if (U_FAILURE(status))
          {
              puts("Calendar::add failed");
              return;
          }
      }
      ucal_close(cal);


Copyright (c) 2000 - 2005 IBM and Others - PDF Version - Feedback: http://icu.sourceforge.net/contacts.html

User Guide for ICU v3.4 Generated 2005-07-27.