12e5b6d6dSopenharmony_ci---
22e5b6d6dSopenharmony_cilayout: default
32e5b6d6dSopenharmony_cititle: Calendar Examples
42e5b6d6dSopenharmony_cinav_order: 2
52e5b6d6dSopenharmony_ciparent: Date/Time
62e5b6d6dSopenharmony_ci---
72e5b6d6dSopenharmony_ci<!--
82e5b6d6dSopenharmony_ci© 2020 and later: Unicode, Inc. and others.
92e5b6d6dSopenharmony_ciLicense & terms of use: http://www.unicode.org/copyright.html
102e5b6d6dSopenharmony_ci-->
112e5b6d6dSopenharmony_ci
122e5b6d6dSopenharmony_ci# Calendar Examples
132e5b6d6dSopenharmony_ci{: .no_toc }
142e5b6d6dSopenharmony_ci
152e5b6d6dSopenharmony_ci## Contents
162e5b6d6dSopenharmony_ci{: .no_toc .text-delta }
172e5b6d6dSopenharmony_ci
182e5b6d6dSopenharmony_ci1. TOC
192e5b6d6dSopenharmony_ci{:toc}
202e5b6d6dSopenharmony_ci
212e5b6d6dSopenharmony_ci---
222e5b6d6dSopenharmony_ci
232e5b6d6dSopenharmony_ci## Calendar for Default Time Zone
242e5b6d6dSopenharmony_ci
252e5b6d6dSopenharmony_ciThese C++, C , and Java examples get a Calendar based on the default time zone
262e5b6d6dSopenharmony_ciand add days to a date.
272e5b6d6dSopenharmony_ci
282e5b6d6dSopenharmony_ci**C++**
292e5b6d6dSopenharmony_ci
302e5b6d6dSopenharmony_ci```c++
312e5b6d6dSopenharmony_ciUErrorCode status = U_ZERO_ERROR;
322e5b6d6dSopenharmony_ciGregorianCalendar* gc = new GregorianCalendar(status);
332e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
342e5b6d6dSopenharmony_ci    puts("Couldn't create GregorianCalendar");
352e5b6d6dSopenharmony_ci    return;
362e5b6d6dSopenharmony_ci}
372e5b6d6dSopenharmony_ci// set up the date
382e5b6d6dSopenharmony_cigc->set(2000, Calendar::FEBRUARY, 26);
392e5b6d6dSopenharmony_cigc->set(Calendar::HOUR_OF_DAY, 23);
402e5b6d6dSopenharmony_cigc->set(Calendar::MINUTE, 0);
412e5b6d6dSopenharmony_cigc->set(Calendar::SECOND, 0);
422e5b6d6dSopenharmony_cigc->set(Calendar::MILLISECOND, 0);
432e5b6d6dSopenharmony_ci// Iterate through the days and print it out.
442e5b6d6dSopenharmony_cifor (int32_t i = 0; i < 30; i++) {
452e5b6d6dSopenharmony_ci    // print out the date.  
462e5b6d6dSopenharmony_ci    // You should use the DateFormat to properly format it
472e5b6d6dSopenharmony_ci    printf("year: %d, month: %d (%d in the implementation), day: %d\n",
482e5b6d6dSopenharmony_ci    gc->get(Calendar::YEAR, status),
492e5b6d6dSopenharmony_ci    gc->get(Calendar::MONTH, status) + 1,
502e5b6d6dSopenharmony_ci    gc->get(Calendar::MONTH, status),
512e5b6d6dSopenharmony_ci    gc->get(Calendar::DATE, status));
522e5b6d6dSopenharmony_ci    if (U_FAILURE(status)) {
532e5b6d6dSopenharmony_ci        puts("Calendar::get failed");
542e5b6d6dSopenharmony_ci        return;
552e5b6d6dSopenharmony_ci    }
562e5b6d6dSopenharmony_ci    // Add a day to the date
572e5b6d6dSopenharmony_ci    gc->add(Calendar::DATE, 1, status);
582e5b6d6dSopenharmony_ci    if (U_FAILURE(status)) {
592e5b6d6dSopenharmony_ci        puts("Calendar::add failed");
602e5b6d6dSopenharmony_ci        return;
612e5b6d6dSopenharmony_ci    }
622e5b6d6dSopenharmony_ci}
632e5b6d6dSopenharmony_cidelete gc;
642e5b6d6dSopenharmony_ci```
652e5b6d6dSopenharmony_ci
662e5b6d6dSopenharmony_ci**C**
672e5b6d6dSopenharmony_ci
682e5b6d6dSopenharmony_ci```c
692e5b6d6dSopenharmony_ciUErrorCode status = U_ZERO_ERROR;
702e5b6d6dSopenharmony_ciint32_t i;
712e5b6d6dSopenharmony_ciUCalendar* cal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &status);
722e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
732e5b6d6dSopenharmony_ci    puts("Couldn't create GregorianCalendar");
742e5b6d6dSopenharmony_ci    return;
752e5b6d6dSopenharmony_ci}
762e5b6d6dSopenharmony_ci// set up the date
772e5b6d6dSopenharmony_ciucal_set(cal, UCAL_YEAR, 2000);
782e5b6d6dSopenharmony_ciucal_set(cal, UCAL_MONTH, UCAL_FEBRUARY); /* FEBRUARY */
792e5b6d6dSopenharmony_ciucal_set(cal, UCAL_DATE, 26);
802e5b6d6dSopenharmony_ciucal_set(cal, UCAL_HOUR_OF_DAY, 23);
812e5b6d6dSopenharmony_ciucal_set(cal, UCAL_MINUTE, 0);
822e5b6d6dSopenharmony_ciucal_set(cal, UCAL_SECOND, 0);
832e5b6d6dSopenharmony_ciucal_set(cal, UCAL_MILLISECOND, 0);
842e5b6d6dSopenharmony_ci// Iterate through the days and print it out.
852e5b6d6dSopenharmony_cifor (i = 0; i < 30; i++) {
862e5b6d6dSopenharmony_ci    // print out the date.
872e5b6d6dSopenharmony_ci    // You should use the udat_* API to properly format it
882e5b6d6dSopenharmony_ci    printf("year: %d, month: %d (%d in the implementation), day: %d\n",
892e5b6d6dSopenharmony_ci           ucal_get(cal, UCAL_YEAR, &status),
902e5b6d6dSopenharmony_ci           ucal_get(cal, UCAL_MONTH, &status) + 1,
912e5b6d6dSopenharmony_ci           ucal_get(cal, UCAL_MONTH, &status),
922e5b6d6dSopenharmony_ci           ucal_get(cal, UCAL_DATE, &status));
932e5b6d6dSopenharmony_ci    if (U_FAILURE(status)) {
942e5b6d6dSopenharmony_ci        puts("Calendar::get failed");
952e5b6d6dSopenharmony_ci        return;
962e5b6d6dSopenharmony_ci    }
972e5b6d6dSopenharmony_ci    // Add a day to the date
982e5b6d6dSopenharmony_ci    ucal_add(cal, UCAL_DATE, 1, &status);
992e5b6d6dSopenharmony_ci    if (U_FAILURE(status)) {
1002e5b6d6dSopenharmony_ci        puts("Calendar::add failed");
1012e5b6d6dSopenharmony_ci        return;
1022e5b6d6dSopenharmony_ci    }
1032e5b6d6dSopenharmony_ci}
1042e5b6d6dSopenharmony_ciucal_close(cal);
1052e5b6d6dSopenharmony_ci```
1062e5b6d6dSopenharmony_ci
1072e5b6d6dSopenharmony_ci**Java**
1082e5b6d6dSopenharmony_ci
1092e5b6d6dSopenharmony_ci```java
1102e5b6d6dSopenharmony_ciCalendar cal = new GregorianCalendar();
1112e5b6d6dSopenharmony_ciif (cal == null) {
1122e5b6d6dSopenharmony_ci    System.out.println("Couldn't create GregorianCalendar");
1132e5b6d6dSopenharmony_ci    return;
1142e5b6d6dSopenharmony_ci}
1152e5b6d6dSopenharmony_ci// set up the date
1162e5b6d6dSopenharmony_cical.set(Calendar.YEAR, 2000);
1172e5b6d6dSopenharmony_cical.set(Calendar.MONTH, Calendar.FEBRUARY); /* FEBRUARY */
1182e5b6d6dSopenharmony_cical.set(Calendar.DATE, 26);
1192e5b6d6dSopenharmony_cical.set(Calendar.HOUR_OF_DAY, 23);
1202e5b6d6dSopenharmony_cical.set(Calendar.MINUTE, 0);
1212e5b6d6dSopenharmony_cical.set(Calendar.SECOND, 0);
1222e5b6d6dSopenharmony_cical.set(Calendar.MILLISECOND, 0);
1232e5b6d6dSopenharmony_ci// Iterate through the days and print it out.
1242e5b6d6dSopenharmony_cifor (int i = 0; i < 30; i++) {
1252e5b6d6dSopenharmony_ci    // print out the date.
1262e5b6d6dSopenharmony_ci    System.out.println(" year: " + cal.get(Calendar.YEAR) + 
1272e5b6d6dSopenharmony_ci                       " month: " + (cal.get(Calendar.MONTH) + 1) +
1282e5b6d6dSopenharmony_ci                       " day : " + cal.get(Calendar.DATE)
1292e5b6d6dSopenharmony_ci    );
1302e5b6d6dSopenharmony_ci    cal.add(Calendar.DATE, 1);
1312e5b6d6dSopenharmony_ci}
1322e5b6d6dSopenharmony_ci```
1332e5b6d6dSopenharmony_ci
1342e5b6d6dSopenharmony_ci## Converting dates between calendars
1352e5b6d6dSopenharmony_ci
1362e5b6d6dSopenharmony_ciThese C++, C , and Java examples demonstrates converting dates from one calendar
1372e5b6d6dSopenharmony_ci(Gregorian) to another calendar (Japanese).
1382e5b6d6dSopenharmony_ci
1392e5b6d6dSopenharmony_ci**C++**
1402e5b6d6dSopenharmony_ci
1412e5b6d6dSopenharmony_ci```c++
1422e5b6d6dSopenharmony_ciUErrorCode status = U_ZERO_ERROR;
1432e5b6d6dSopenharmony_ciUDate time;
1442e5b6d6dSopenharmony_ciCalendar *cal1, *cal2;
1452e5b6d6dSopenharmony_ci// Create a new Gregorian Calendar.
1462e5b6d6dSopenharmony_cical1 = Calendar::createInstance("en_US@calendar=gregorian", status);
1472e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
1482e5b6d6dSopenharmony_ci    printf("Error creating Gregorian calendar.\n");
1492e5b6d6dSopenharmony_ci    return;
1502e5b6d6dSopenharmony_ci}
1512e5b6d6dSopenharmony_ci// Set the Gregorian Calendar to a specific date for testing.
1522e5b6d6dSopenharmony_cical1->set(1980, UCAL_SEPTEMBER, 3);
1532e5b6d6dSopenharmony_ci// Display the date.
1542e5b6d6dSopenharmony_ciprintf("Gregorian Calendar:\t%d/%d/%d\n",
1552e5b6d6dSopenharmony_ci        cal1->get(UCAL_MONTH, status) + 1,
1562e5b6d6dSopenharmony_ci        cal1->get(UCAL_DATE, status),
1572e5b6d6dSopenharmony_ci        cal1->get(UCAL_YEAR, status));
1582e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
1592e5b6d6dSopenharmony_ci    printf("Error getting Gregorian date.");
1602e5b6d6dSopenharmony_ci    return;
1612e5b6d6dSopenharmony_ci}
1622e5b6d6dSopenharmony_ci// Create a Japanese Calendar.
1632e5b6d6dSopenharmony_cical2 = Calendar::createInstance("ja_JP@calendar=japanese", status);
1642e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
1652e5b6d6dSopenharmony_ci    printf("Error creating Japnese calendar.\n");
1662e5b6d6dSopenharmony_ci    return;
1672e5b6d6dSopenharmony_ci}
1682e5b6d6dSopenharmony_ci// Set the date.
1692e5b6d6dSopenharmony_citime = cal1->getTime(status);
1702e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
1712e5b6d6dSopenharmony_ci    printf("Error getting time.\n");
1722e5b6d6dSopenharmony_ci    return;
1732e5b6d6dSopenharmony_ci}
1742e5b6d6dSopenharmony_cical2->setTime(time, status);
1752e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
1762e5b6d6dSopenharmony_ci    printf("Error setting the date for Japanese calendar.\n");
1772e5b6d6dSopenharmony_ci    return;
1782e5b6d6dSopenharmony_ci}
1792e5b6d6dSopenharmony_ci// Set the timezone
1802e5b6d6dSopenharmony_cical2->setTimeZone(cal1->getTimeZone());
1812e5b6d6dSopenharmony_ci// Display the date.
1822e5b6d6dSopenharmony_ciprintf("Japanese Calendar:\t%d/%d/%d\n",
1832e5b6d6dSopenharmony_ci        cal2->get(UCAL_MONTH, status) + 1,
1842e5b6d6dSopenharmony_ci        cal2->get(UCAL_DATE, status),
1852e5b6d6dSopenharmony_ci        cal2->get(UCAL_YEAR, status));
1862e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
1872e5b6d6dSopenharmony_ci    printf("Error getting Japanese date.");
1882e5b6d6dSopenharmony_ci    return;
1892e5b6d6dSopenharmony_ci}
1902e5b6d6dSopenharmony_cidelete cal1;
1912e5b6d6dSopenharmony_cidelete cal2;
1922e5b6d6dSopenharmony_ci```
1932e5b6d6dSopenharmony_ci
1942e5b6d6dSopenharmony_ci**C**
1952e5b6d6dSopenharmony_ci
1962e5b6d6dSopenharmony_ci```c
1972e5b6d6dSopenharmony_ciUErrorCode status = U_ZERO_ERROR;
1982e5b6d6dSopenharmony_ciUDate time;
1992e5b6d6dSopenharmony_ciUCalendar *cal1, *cal2;
2002e5b6d6dSopenharmony_ci// Create a new Gregorian Calendar.
2012e5b6d6dSopenharmony_cical1 = ucal_open(NULL, -1, "en_US@calendar=gregorian", UCAL_TRADITIONAL,
2022e5b6d6dSopenharmony_ci                 &status);
2032e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2042e5b6d6dSopenharmony_ci    printf("Couldn't create Gregorian Calendar.");
2052e5b6d6dSopenharmony_ci    return;
2062e5b6d6dSopenharmony_ci}
2072e5b6d6dSopenharmony_ci// Set the Gregorian Calendar to a specific date for testing.
2082e5b6d6dSopenharmony_ciucal_setDate(cal1, 1980, UCAL_SEPTEMBER, 3, &status);
2092e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2102e5b6d6dSopenharmony_ci    printf("Error setting date.");
2112e5b6d6dSopenharmony_ci    return;
2122e5b6d6dSopenharmony_ci}
2132e5b6d6dSopenharmony_ci// Display the date.
2142e5b6d6dSopenharmony_ciprintf("Gregorian Calendar:\t%d/%d/%d\n",
2152e5b6d6dSopenharmony_ci        ucal_get(cal1, UCAL_MONTH, &status) + 1,
2162e5b6d6dSopenharmony_ci        ucal_get(cal1, UCAL_DATE, &status),
2172e5b6d6dSopenharmony_ci        ucal_get(cal1, UCAL_YEAR, &status));
2182e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2192e5b6d6dSopenharmony_ci    printf("Error getting Gregorian date.");
2202e5b6d6dSopenharmony_ci    return 1;
2212e5b6d6dSopenharmony_ci}
2222e5b6d6dSopenharmony_ci// Create a Japanese Calendar.
2232e5b6d6dSopenharmony_cical2 = ucal_open(NULL, -1, "ja_J@calendar=japanese", UCAL_TRADITIONAL, &status);
2242e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2252e5b6d6dSopenharmony_ci    printf("Couldn't create Japanese Calendar.");
2262e5b6d6dSopenharmony_ci    return 1;
2272e5b6d6dSopenharmony_ci}
2282e5b6d6dSopenharmony_ci// Set the date.
2292e5b6d6dSopenharmony_citime = ucal_getMillis(cal1, &status);
2302e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2312e5b6d6dSopenharmony_ci    printf("Error getting time.\n");
2322e5b6d6dSopenharmony_ci    return;
2332e5b6d6dSopenharmony_ci}
2342e5b6d6dSopenharmony_ciucal_setMillis(cal2, time, &status);
2352e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2362e5b6d6dSopenharmony_ci    printf("Error setting time.\n");
2372e5b6d6dSopenharmony_ci    return;
2382e5b6d6dSopenharmony_ci}
2392e5b6d6dSopenharmony_ci// Display the date.
2402e5b6d6dSopenharmony_ciprintf("Japanese Calendar:\t%d/%d/%d\n",
2412e5b6d6dSopenharmony_ci        ucal_get(cal2, UCAL_MONTH, &status) + 1,
2422e5b6d6dSopenharmony_ci        ucal_get(cal2, UCAL_DATE, &status),
2432e5b6d6dSopenharmony_ci        ucal_get(cal2, UCAL_YEAR, &status));
2442e5b6d6dSopenharmony_ciif (U_FAILURE(status)) {
2452e5b6d6dSopenharmony_ci    printf("Error getting Japanese date.");
2462e5b6d6dSopenharmony_ci    return;
2472e5b6d6dSopenharmony_ci}
2482e5b6d6dSopenharmony_ciucal_close(cal1);
2492e5b6d6dSopenharmony_ciucal_close(cal2);
2502e5b6d6dSopenharmony_ci```
2512e5b6d6dSopenharmony_ci
2522e5b6d6dSopenharmony_ci**Java**
2532e5b6d6dSopenharmony_ci
2542e5b6d6dSopenharmony_ci```java
2552e5b6d6dSopenharmony_ciCalendar cal1, cal2;
2562e5b6d6dSopenharmony_ci// Create a new Gregorian Calendar.
2572e5b6d6dSopenharmony_cical1 = new GregorianCalendar();
2582e5b6d6dSopenharmony_ci// Set the Gregorian Calendar to a specific date for testing.
2592e5b6d6dSopenharmony_cical1.set(1980, Calendar.SEPTEMBER, 3);
2602e5b6d6dSopenharmony_ci// Display the date.
2612e5b6d6dSopenharmony_ciSystem.out.println("Gregorian Calendar:\t" + (cal1.get(Calendar.MONTH) + 1) +
2622e5b6d6dSopenharmony_ci                    "/" +
2632e5b6d6dSopenharmony_ci                    cal1.get(Calendar.DATE) + "/" +
2642e5b6d6dSopenharmony_ci                    cal1.get(Calendar.YEAR));
2652e5b6d6dSopenharmony_ci// Create a Japanese Calendar.
2662e5b6d6dSopenharmony_cical2 = new JapaneseCalendar();
2672e5b6d6dSopenharmony_ci// Set the date and timezone
2682e5b6d6dSopenharmony_cical2.setTime(cal1.getTime());
2692e5b6d6dSopenharmony_cical2.setTimeZone(cal1.getTimeZone());
2702e5b6d6dSopenharmony_ci// Display the date.
2712e5b6d6dSopenharmony_ciSystem.out.println("Japanese Calendar:\t" + (cal2.get(Calendar.MONTH) + 1) +
2722e5b6d6dSopenharmony_ci                    "/" +
2732e5b6d6dSopenharmony_ci                    cal2.get(Calendar.DATE) + "/" +
2742e5b6d6dSopenharmony_ci                    cal2.get(Calendar.YEAR));
2752e5b6d6dSopenharmony_ci```
276