1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 28 * 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions are met: 33 * 34 * * Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 37 * * Redistributions in binary form must reproduce the above copyright notice, 38 * this list of conditions and the following disclaimer in the documentation 39 * and/or other materials provided with the distribution. 40 * 41 * * Neither the name of JSR-310 nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 49 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 50 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 52 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 53 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 54 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 58 package test.java.time.chrono; 59 60 import static org.testng.Assert.assertEquals; 61 62 import java.time.LocalDate; 63 import java.time.LocalTime; 64 import java.time.ZoneId; 65 import java.time.chrono.ChronoLocalDate; 66 import java.time.chrono.ChronoLocalDateTime; 67 import java.time.chrono.ChronoZonedDateTime; 68 import java.time.chrono.Chronology; 69 import java.time.chrono.HijrahChronology; 70 import java.time.chrono.HijrahDate; 71 import java.time.chrono.ThaiBuddhistDate; 72 import java.time.temporal.ChronoField; 73 import java.time.temporal.ChronoUnit; 74 import java.util.Locale; 75 import java.util.Set; 76 77 import org.testng.annotations.DataProvider; 78 import org.testng.annotations.Test; 79 80 /** 81 * Test case verify that the example code in the package-info.java compiles 82 * and runs. 83 */ 84 public class TestExampleCode { 85 86 @Test test_chronoPackageExample()87 public void test_chronoPackageExample() { 88 // Print the Thai Buddhist date 89 ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow(); 90 int day = now1.get(ChronoField.DAY_OF_MONTH); 91 int dow = now1.get(ChronoField.DAY_OF_WEEK); 92 int month = now1.get(ChronoField.MONTH_OF_YEAR); 93 int year = now1.get(ChronoField.YEAR); 94 System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(), 95 dow, day, month, year); 96 97 // Enumerate the list of available calendars and print today for each 98 Set<Chronology> chronos = Chronology.getAvailableChronologies(); 99 for (Chronology chrono : chronos) { 100 ChronoLocalDate date = chrono.dateNow(); 101 System.out.printf(" %20s: %s%n", chrono.getId(), date.toString()); 102 } 103 104 // Print today's date and the last day of the year for the Thai Buddhist Calendar. 105 ChronoLocalDate first = now1 106 .with(ChronoField.DAY_OF_MONTH, 1) 107 .with(ChronoField.MONTH_OF_YEAR, 1); 108 ChronoLocalDate last = first 109 .plus(1, ChronoUnit.YEARS) 110 .minus(1, ChronoUnit.DAYS); 111 System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(), 112 first, last); 113 } 114 115 //----------------------------------------------------------------------- 116 // Data provider for Hijrah Type names 117 //----------------------------------------------------------------------- 118 @DataProvider(name = "HijrahTypeNames") data_of_ummalqura()119 Object[][] data_of_ummalqura() { 120 return new Object[][]{ 121 { "Hijrah-umalqura", "islamic-umalqura"}, 122 }; 123 } 124 125 @Test(dataProvider= "HijrahTypeNames") test_HijrahTypeViaLocale(String calendarId, String calendarType)126 public void test_HijrahTypeViaLocale(String calendarId, String calendarType) { 127 Locale.Builder builder = new Locale.Builder(); 128 builder.setLanguage("en").setRegion("US"); 129 builder.setUnicodeLocaleKeyword("ca", calendarType); 130 Locale locale = builder.build(); 131 Chronology chrono = Chronology.ofLocale(locale); 132 System.out.printf(" Locale language tag: %s, Chronology ID: %s, type: %s%n", 133 locale.toLanguageTag(), chrono, chrono.getCalendarType()); 134 Chronology expected = Chronology.of(calendarId); 135 assertEquals(chrono, expected, "Expected chronology not found"); 136 } 137 138 @Test test_calendarPackageExample()139 public void test_calendarPackageExample() { 140 141 // Enumerate the list of available calendars and print today for each 142 Set<Chronology> chronos = Chronology.getAvailableChronologies(); 143 for (Chronology chrono : chronos) { 144 ChronoLocalDate date = chrono.dateNow(); 145 System.out.printf(" %20s: %s%n", chrono.getId(), date.toString()); 146 } 147 148 // Print the Thai Buddhist date 149 ThaiBuddhistDate now1 = ThaiBuddhistDate.now(); 150 int day = now1.get(ChronoField.DAY_OF_MONTH); 151 int dow = now1.get(ChronoField.DAY_OF_WEEK); 152 int month = now1.get(ChronoField.MONTH_OF_YEAR); 153 int year = now1.get(ChronoField.YEAR); 154 System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(), 155 dow, day, month, year); 156 157 // Print today's date and the last day of the year for the Thai Buddhist Calendar. 158 ThaiBuddhistDate first = now1 159 .with(ChronoField.DAY_OF_MONTH, 1) 160 .with(ChronoField.MONTH_OF_YEAR, 1); 161 ThaiBuddhistDate last = first 162 .plus(1, ChronoUnit.YEARS) 163 .minus(1, ChronoUnit.DAYS); 164 System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(), 165 first, last); 166 } 167 HijrahExample1()168 void HijrahExample1() { 169 HijrahDate hd2 = HijrahChronology.INSTANCE.date(1200, 1, 1); 170 171 ChronoLocalDateTime<HijrahDate> hdt = hd2.atTime(LocalTime.MIDNIGHT); 172 ChronoZonedDateTime<HijrahDate> zhdt = hdt.atZone(ZoneId.of("GMT")); 173 HijrahDate hd3 = zhdt.toLocalDate(); 174 ChronoLocalDateTime<HijrahDate> hdt2 = zhdt.toLocalDateTime(); 175 HijrahDate hd4 = hdt2.toLocalDate(); 176 177 HijrahDate hd5 = next(hd2); 178 } 179 test_unknownChronologyWithDateTime()180 void test_unknownChronologyWithDateTime() { 181 ChronoLocalDate date = LocalDate.now(); 182 ChronoLocalDateTime<?> cldt = date.atTime(LocalTime.NOON); 183 ChronoLocalDate ld = cldt.toLocalDate(); 184 ChronoLocalDateTime<?> noonTomorrow = tomorrowNoon(ld); 185 } 186 187 @Test test_library()188 public void test_library() { 189 HijrahDate date = HijrahDate.now(); 190 HijrahDate next = next(date); 191 ChronoLocalDateTime<HijrahDate> noonTomorrow = tomorrowNoon(date); 192 HijrahDate hd3 = noonTomorrow.toLocalDate(); 193 System.out.printf(" now: %s, noon tomorrow: %s%n", date, noonTomorrow); 194 } 195 196 /** 197 * Simple function based on a date, returning a ChronoDate of the same type. 198 * @param <D> a parameterized ChronoLocalDate 199 * @param date a specific date extending ChronoLocalDate 200 * @return a new date in the same chronology. 201 */ 202 @SuppressWarnings("unchecked") next(D date)203 private <D extends ChronoLocalDate> D next(D date) { 204 return (D) date.plus(1, ChronoUnit.DAYS); 205 } 206 207 /** 208 * Simple function based on a date, returning a ChronoLocalDateTime of the 209 * same chronology. 210 * @param <D> a parameterized ChronoLocalDate 211 * @param date a specific date extending ChronoLocalDate 212 * @return a [@code ChronoLocalDateTime<D>} using the change chronology. 213 */ 214 @SuppressWarnings("unchecked") tomorrowNoon(D date)215 private <D extends ChronoLocalDate> ChronoLocalDateTime<D> tomorrowNoon(D date) { 216 return (ChronoLocalDateTime<D>) date.plus(1, ChronoUnit.DAYS).atTime(LocalTime.of(12, 0)); 217 } 218 } 219