1--- 2layout: default 3title: Calendar Examples 4nav_order: 2 5parent: Date/Time 6--- 7<!-- 8© 2020 and later: Unicode, Inc. and others. 9License & terms of use: http://www.unicode.org/copyright.html 10--> 11 12# Calendar Examples 13{: .no_toc } 14 15## Contents 16{: .no_toc .text-delta } 17 181. TOC 19{:toc} 20 21--- 22 23## Calendar for Default Time Zone 24 25These C++, C , and Java examples get a Calendar based on the default time zone 26and add days to a date. 27 28**C++** 29 30```c++ 31UErrorCode status = U_ZERO_ERROR; 32GregorianCalendar* gc = new GregorianCalendar(status); 33if (U_FAILURE(status)) { 34 puts("Couldn't create GregorianCalendar"); 35 return; 36} 37// set up the date 38gc->set(2000, Calendar::FEBRUARY, 26); 39gc->set(Calendar::HOUR_OF_DAY, 23); 40gc->set(Calendar::MINUTE, 0); 41gc->set(Calendar::SECOND, 0); 42gc->set(Calendar::MILLISECOND, 0); 43// Iterate through the days and print it out. 44for (int32_t i = 0; i < 30; i++) { 45 // print out the date. 46 // You should use the DateFormat to properly format it 47 printf("year: %d, month: %d (%d in the implementation), day: %d\n", 48 gc->get(Calendar::YEAR, status), 49 gc->get(Calendar::MONTH, status) + 1, 50 gc->get(Calendar::MONTH, status), 51 gc->get(Calendar::DATE, status)); 52 if (U_FAILURE(status)) { 53 puts("Calendar::get failed"); 54 return; 55 } 56 // Add a day to the date 57 gc->add(Calendar::DATE, 1, status); 58 if (U_FAILURE(status)) { 59 puts("Calendar::add failed"); 60 return; 61 } 62} 63delete gc; 64``` 65 66**C** 67 68```c 69UErrorCode status = U_ZERO_ERROR; 70int32_t i; 71UCalendar* cal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &status); 72if (U_FAILURE(status)) { 73 puts("Couldn't create GregorianCalendar"); 74 return; 75} 76// set up the date 77ucal_set(cal, UCAL_YEAR, 2000); 78ucal_set(cal, UCAL_MONTH, UCAL_FEBRUARY); /* FEBRUARY */ 79ucal_set(cal, UCAL_DATE, 26); 80ucal_set(cal, UCAL_HOUR_OF_DAY, 23); 81ucal_set(cal, UCAL_MINUTE, 0); 82ucal_set(cal, UCAL_SECOND, 0); 83ucal_set(cal, UCAL_MILLISECOND, 0); 84// Iterate through the days and print it out. 85for (i = 0; i < 30; i++) { 86 // print out the date. 87 // You should use the udat_* API to properly format it 88 printf("year: %d, month: %d (%d in the implementation), day: %d\n", 89 ucal_get(cal, UCAL_YEAR, &status), 90 ucal_get(cal, UCAL_MONTH, &status) + 1, 91 ucal_get(cal, UCAL_MONTH, &status), 92 ucal_get(cal, UCAL_DATE, &status)); 93 if (U_FAILURE(status)) { 94 puts("Calendar::get failed"); 95 return; 96 } 97 // Add a day to the date 98 ucal_add(cal, UCAL_DATE, 1, &status); 99 if (U_FAILURE(status)) { 100 puts("Calendar::add failed"); 101 return; 102 } 103} 104ucal_close(cal); 105``` 106 107**Java** 108 109```java 110Calendar cal = new GregorianCalendar(); 111if (cal == null) { 112 System.out.println("Couldn't create GregorianCalendar"); 113 return; 114} 115// set up the date 116cal.set(Calendar.YEAR, 2000); 117cal.set(Calendar.MONTH, Calendar.FEBRUARY); /* FEBRUARY */ 118cal.set(Calendar.DATE, 26); 119cal.set(Calendar.HOUR_OF_DAY, 23); 120cal.set(Calendar.MINUTE, 0); 121cal.set(Calendar.SECOND, 0); 122cal.set(Calendar.MILLISECOND, 0); 123// Iterate through the days and print it out. 124for (int i = 0; i < 30; i++) { 125 // print out the date. 126 System.out.println(" year: " + cal.get(Calendar.YEAR) + 127 " month: " + (cal.get(Calendar.MONTH) + 1) + 128 " day : " + cal.get(Calendar.DATE) 129 ); 130 cal.add(Calendar.DATE, 1); 131} 132``` 133 134## Converting dates between calendars 135 136These C++, C , and Java examples demonstrates converting dates from one calendar 137(Gregorian) to another calendar (Japanese). 138 139**C++** 140 141```c++ 142UErrorCode status = U_ZERO_ERROR; 143UDate time; 144Calendar *cal1, *cal2; 145// Create a new Gregorian Calendar. 146cal1 = Calendar::createInstance("en_US@calendar=gregorian", status); 147if (U_FAILURE(status)) { 148 printf("Error creating Gregorian calendar.\n"); 149 return; 150} 151// Set the Gregorian Calendar to a specific date for testing. 152cal1->set(1980, UCAL_SEPTEMBER, 3); 153// Display the date. 154printf("Gregorian Calendar:\t%d/%d/%d\n", 155 cal1->get(UCAL_MONTH, status) + 1, 156 cal1->get(UCAL_DATE, status), 157 cal1->get(UCAL_YEAR, status)); 158if (U_FAILURE(status)) { 159 printf("Error getting Gregorian date."); 160 return; 161} 162// Create a Japanese Calendar. 163cal2 = Calendar::createInstance("ja_JP@calendar=japanese", status); 164if (U_FAILURE(status)) { 165 printf("Error creating Japnese calendar.\n"); 166 return; 167} 168// Set the date. 169time = cal1->getTime(status); 170if (U_FAILURE(status)) { 171 printf("Error getting time.\n"); 172 return; 173} 174cal2->setTime(time, status); 175if (U_FAILURE(status)) { 176 printf("Error setting the date for Japanese calendar.\n"); 177 return; 178} 179// Set the timezone 180cal2->setTimeZone(cal1->getTimeZone()); 181// Display the date. 182printf("Japanese Calendar:\t%d/%d/%d\n", 183 cal2->get(UCAL_MONTH, status) + 1, 184 cal2->get(UCAL_DATE, status), 185 cal2->get(UCAL_YEAR, status)); 186if (U_FAILURE(status)) { 187 printf("Error getting Japanese date."); 188 return; 189} 190delete cal1; 191delete cal2; 192``` 193 194**C** 195 196```c 197UErrorCode status = U_ZERO_ERROR; 198UDate time; 199UCalendar *cal1, *cal2; 200// Create a new Gregorian Calendar. 201cal1 = ucal_open(NULL, -1, "en_US@calendar=gregorian", UCAL_TRADITIONAL, 202 &status); 203if (U_FAILURE(status)) { 204 printf("Couldn't create Gregorian Calendar."); 205 return; 206} 207// Set the Gregorian Calendar to a specific date for testing. 208ucal_setDate(cal1, 1980, UCAL_SEPTEMBER, 3, &status); 209if (U_FAILURE(status)) { 210 printf("Error setting date."); 211 return; 212} 213// Display the date. 214printf("Gregorian Calendar:\t%d/%d/%d\n", 215 ucal_get(cal1, UCAL_MONTH, &status) + 1, 216 ucal_get(cal1, UCAL_DATE, &status), 217 ucal_get(cal1, UCAL_YEAR, &status)); 218if (U_FAILURE(status)) { 219 printf("Error getting Gregorian date."); 220 return 1; 221} 222// Create a Japanese Calendar. 223cal2 = ucal_open(NULL, -1, "ja_J@calendar=japanese", UCAL_TRADITIONAL, &status); 224if (U_FAILURE(status)) { 225 printf("Couldn't create Japanese Calendar."); 226 return 1; 227} 228// Set the date. 229time = ucal_getMillis(cal1, &status); 230if (U_FAILURE(status)) { 231 printf("Error getting time.\n"); 232 return; 233} 234ucal_setMillis(cal2, time, &status); 235if (U_FAILURE(status)) { 236 printf("Error setting time.\n"); 237 return; 238} 239// Display the date. 240printf("Japanese Calendar:\t%d/%d/%d\n", 241 ucal_get(cal2, UCAL_MONTH, &status) + 1, 242 ucal_get(cal2, UCAL_DATE, &status), 243 ucal_get(cal2, UCAL_YEAR, &status)); 244if (U_FAILURE(status)) { 245 printf("Error getting Japanese date."); 246 return; 247} 248ucal_close(cal1); 249ucal_close(cal2); 250``` 251 252**Java** 253 254```java 255Calendar cal1, cal2; 256// Create a new Gregorian Calendar. 257cal1 = new GregorianCalendar(); 258// Set the Gregorian Calendar to a specific date for testing. 259cal1.set(1980, Calendar.SEPTEMBER, 3); 260// Display the date. 261System.out.println("Gregorian Calendar:\t" + (cal1.get(Calendar.MONTH) + 1) + 262 "/" + 263 cal1.get(Calendar.DATE) + "/" + 264 cal1.get(Calendar.YEAR)); 265// Create a Japanese Calendar. 266cal2 = new JapaneseCalendar(); 267// Set the date and timezone 268cal2.setTime(cal1.getTime()); 269cal2.setTimeZone(cal1.getTimeZone()); 270// Display the date. 271System.out.println("Japanese Calendar:\t" + (cal2.get(Calendar.MONTH) + 1) + 272 "/" + 273 cal2.get(Calendar.DATE) + "/" + 274 cal2.get(Calendar.YEAR)); 275``` 276