Java Code Examples for android.icu.util.Calendar#clear()
The following examples show how to use
android.icu.util.Calendar#clear() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * @bug 4071441 */ @Test public void Test4071441() { DateFormat fmtA = DateFormat.getInstance(); DateFormat fmtB = DateFormat.getInstance(); // {sfb} Is it OK to cast away const here? Calendar calA = fmtA.getCalendar(); Calendar calB = fmtB.getCalendar(); calA.clear(); calA.set(1900, 0 ,0); calB.clear(); calB.set(1900, 0, 0); if (!calA.equals(calB)) errln("Fail: Can't complete test; Calendar instances unequal"); if (!fmtA.equals(fmtB)) errln("Fail: DateFormat unequal when Calendars equal"); calB.clear(); calB.set(1961, Calendar.DECEMBER, 25); if (calA.equals(calB)) errln("Fail: Can't complete test; Calendar instances equal"); if (!fmtA.equals(fmtB)) errln("Fail: DateFormat unequal when Calendars equivalent"); logln("DateFormat.equals ok"); }
Example 2
Source File: DangiTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * Test minimum and maximum functions. */ @Test public void TestLimits() { // The number of days and the start date can be adjusted // arbitrarily to either speed up the test or make it more // thorough, but try to test at least a full year, preferably a // full non-leap and a full leap year. // Final parameter is either number of days, if > 0, or test // duration in seconds, if < 0. Calendar tempcal = Calendar.getInstance(); tempcal.clear(); tempcal.set(1989, Calendar.NOVEMBER, 1); Calendar dangi = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi")); doLimitsTest(dangi, null, tempcal.getTime()); doTheoreticalLimitsTest(dangi, true); }
Example 3
Source File: DateFormatRegressionTestJ.java From j2objc with Apache License 2.0 | 6 votes |
@Test public void Test4358730() { SimpleDateFormat sdf = new SimpleDateFormat(); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(2001,11,10); Date today = cal.getTime(); sdf.applyPattern("MM d y"); logln(sdf.format(today)); sdf.applyPattern("MM d yy"); logln(sdf.format(today)); sdf.applyPattern("MM d yyy"); logln(sdf.format(today)); sdf.applyPattern("MM d yyyy"); logln(sdf.format(today)); sdf.applyPattern("MM d yyyyy"); logln(sdf.format(today)); }
Example 4
Source File: CalendarViewLegacyDelegate.java From android_9.0.0_r45 with Apache License 2.0 | 5 votes |
/** * Calculates the day that the given x position is in, accounting for * week number. * * @param x The x position of the touch event. * @return True if a day was found for the given location. */ public boolean getDayFromLocation(float x, Calendar outCalendar) { final boolean isLayoutRtl = isLayoutRtl(); int start; int end; if (isLayoutRtl) { start = 0; end = mShowWeekNumber ? mWidth - mWidth / mNumCells : mWidth; } else { start = mShowWeekNumber ? mWidth / mNumCells : 0; end = mWidth; } if (x < start || x > end) { outCalendar.clear(); return false; } // Selection is (x - start) / (pixels/day) which is (x - start) * day / pixels int dayPosition = (int) ((x - start) * mDaysPerWeek / (end - start)); if (isLayoutRtl) { dayPosition = mDaysPerWeek - 1 - dayPosition; } outCalendar.setTimeInMillis(mFirstDay.getTimeInMillis()); outCalendar.add(Calendar.DAY_OF_MONTH, dayPosition); return true; }
Example 5
Source File: CalendarRegressionTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * Calendar does not update field values when setTimeZone is called. */ @Test public void Test4177484() { TimeZone PST = TimeZone.getTimeZone("PST"); TimeZone EST = TimeZone.getTimeZone("EST"); Calendar cal = Calendar.getInstance(PST, Locale.US); cal.clear(); cal.set(1999, 3, 21, 15, 5, 0); // Arbitrary int h1 = cal.get(Calendar.HOUR_OF_DAY); cal.setTimeZone(EST); int h2 = cal.get(Calendar.HOUR_OF_DAY); if (h1 == h2) { errln("FAIL: Fields not updated after setTimeZone"); } // getTime() must NOT change when time zone is changed. // getTime() returns zone-independent time in ms. cal.clear(); cal.setTimeZone(PST); cal.set(Calendar.HOUR_OF_DAY, 10); Date pst10 = cal.getTime(); cal.setTimeZone(EST); Date est10 = cal.getTime(); if (!pst10.equals(est10)) { errln("FAIL: setTimeZone changed time"); } }
Example 6
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * j32 {JDK Bug 4210209 4209272} * DateFormat cannot parse Feb 29 2000 when setLenient(false) */ @Test public void Test4210209() { String pattern = "MMM d, yyyy"; DateFormat fmt = new SimpleDateFormat(pattern, Locale.US); DateFormat disp = new SimpleDateFormat("MMM dd yyyy GG", Locale.US); Calendar calx = fmt.getCalendar(); calx.setLenient(false); Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(2000, Calendar.FEBRUARY, 29); Date d = calendar.getTime(); String s = fmt.format(d); logln(disp.format(d) + " f> " + pattern + " => \"" + s + "\""); ParsePosition pos = new ParsePosition(0); d = fmt.parse(s, pos); logln("\"" + s + "\" p> " + pattern + " => " + (d!=null?disp.format(d):"null")); logln("Parse pos = " + pos.getIndex() + ", error pos = " + pos.getErrorIndex()); if (pos.getErrorIndex() != -1) { errln("FAIL: Error index should be -1"); } // The underlying bug is in GregorianCalendar. If the following lines // succeed, the bug is fixed. If the bug isn't fixed, they will throw // an exception. GregorianCalendar cal = new GregorianCalendar(); cal.clear(); cal.setLenient(false); cal.set(2000, Calendar.FEBRUARY, 29); // This should work! d = cal.getTime(); logln("Attempt to set Calendar to Feb 29 2000: " + disp.format(d)); }
Example 7
Source File: CalendarTestFmwk.java From j2objc with Apache License 2.0 | 5 votes |
/** * Process test cases for <code>add</code> and <code>roll</code> methods. * Each test case is an array of integers, as follows: * <ul> * <li>0: input year * <li>1: month (zero-based) * <li>2: day * <li>3: field to roll or add to * <li>4: amount to roll or add * <li>5: result year * <li>6: month (zero-based) * <li>7: day * </ul> * For example: * <pre> * // input add by output * // year month day field amount year month day * { 5759, HESHVAN, 2, MONTH, 1, 5759, KISLEV, 2 }, * </pre> * * @param roll <code>true</code> or <code>ROLL</code> to test the <code>roll</code> method; * <code>false</code> or <code>ADD</code> to test the <code>add</code method */ protected void doRollAdd(boolean roll, Calendar cal, int[][] tests) { String name = roll ? "rolling" : "adding"; for (int i = 0; i < tests.length; i++) { int[] test = tests[i]; cal.clear(); if (cal instanceof ChineseCalendar) { cal.set(Calendar.EXTENDED_YEAR, test[0]); cal.set(Calendar.MONTH, test[1]); cal.set(Calendar.DAY_OF_MONTH, test[2]); } else { cal.set(test[0], test[1], test[2]); } double day0 = getJulianDay(cal); if (roll) { cal.roll(test[3], test[4]); } else { cal.add(test[3], test[4]); } int y = cal.get(cal instanceof ChineseCalendar ? Calendar.EXTENDED_YEAR : YEAR); if (y != test[5] || cal.get(MONTH) != test[6] || cal.get(DATE) != test[7]) { errln("Fail: " + name + " "+ ymdToString(test[0], test[1], test[2]) + " (" + day0 + ")" + " " + FIELD_NAME[test[3]] + " by " + test[4] + ": expected " + ymdToString(test[5], test[6], test[7]) + ", got " + ymdToString(cal)); } else if (isVerbose()) { logln("OK: " + name + " "+ ymdToString(test[0], test[1], test[2]) + " (" + day0 + ")" + " " + FIELD_NAME[test[3]] + " by " + test[4] + ": got " + ymdToString(cal)); } } }
Example 8
Source File: DangiTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * Test the behavior of fields that are out of range. */ @Test public void TestOutOfRange() { int[] DATA = new int[] { // Input Output 4334, 13, 1, 4335, 1, 1, 4334, 18, 1, 4335, 6, 1, 4335, 0, 1, 4334, 12, 1, 4335, -6, 1, 4334, 6, 1, 4334, 1, 32, 4334, 2, 2, // 1-4334 has 30 days 4334, 2, -1, 4334, 1, 29, }; Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi")); for (int i = 0; i < DATA.length;) { int y1 = DATA[i++]; int m1 = DATA[i++] - 1; int d1 = DATA[i++]; int y2 = DATA[i++]; int m2 = DATA[i++] - 1; int d2 = DATA[i++]; cal.clear(); cal.set(Calendar.EXTENDED_YEAR, y1); cal.set(MONTH, m1); cal.set(DATE, d1); int y = cal.get(Calendar.EXTENDED_YEAR); int m = cal.get(MONTH); int d = cal.get(DATE); if (y != y2 || m != m2 || d != d2) { errln("Fail: " + y1 + "/" + (m1 + 1) + "/" + d1 + " resolves to " + y + "/" + (m + 1) + "/" + d + ", expected " + y2 + "/" + (m2 + 1) + "/" + d2); } else if (isVerbose()) { logln("OK: " + y1 + "/" + (m1 + 1) + "/" + d1 + " resolves to " + y + "/" + (m + 1) + "/" + d); } } }
Example 9
Source File: IBMCalendarTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * Make sure that when adding a day, we actually wind up in a * different day. The DST adjustments we use to keep the hour * constant across DST changes can backfire and change the day. */ @Test public void TestTimeZoneTransitionAdd() { Locale locale = Locale.US; // could also be CHINA SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm z", locale); String tz[] = TimeZone.getAvailableIDs(); for (int z=0; z<tz.length; ++z) { TimeZone t = TimeZone.getTimeZone(tz[z]); dateFormat.setTimeZone(t); Calendar cal = Calendar.getInstance(t, locale); cal.clear(); // Scan the year 2003, overlapping the edges of the year cal.set(Calendar.YEAR, 2002); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DAY_OF_MONTH, 25); for (int i=0; i<365+10; ++i) { Date yesterday = cal.getTime(); int yesterday_day = cal.get(Calendar.DAY_OF_MONTH); cal.add(Calendar.DAY_OF_MONTH, 1); if (yesterday_day == cal.get(Calendar.DAY_OF_MONTH)) { errln(tz[z] + " " + dateFormat.format(yesterday) + " +1d= " + dateFormat.format(cal.getTime())); } } } }
Example 10
Source File: CalendarRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * Set behavior of DST_OFFSET field. ICU4J Jitterbug 9. */ @Test public void TestJ9() { int HOURS = 60*60*1000; Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("PST"), Locale.US); final int END_FIELDS = 0x1234; int[] DATA = { // With no explicit ZONE/DST expect 12:00 am Calendar.MONTH, Calendar.JUNE, END_FIELDS, 0, 0, // expected hour, min // Normal ZONE/DST for June 1 Pacific is 8:00/1:00 Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET, -8*HOURS, Calendar.DST_OFFSET, HOURS, END_FIELDS, 0, 0, // expected hour, min // With ZONE/DST of 8:00/0:30 expect time of 12:30 am Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET, -8*HOURS, Calendar.DST_OFFSET, HOURS/2, END_FIELDS, 0, 30, // expected hour, min // With ZONE/DST of 8:00/UNSET expect time of 1:00 am Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET, -8*HOURS, END_FIELDS, 1, 0, // expected hour, min // With ZONE/DST of UNSET/0:30 expect 4:30 pm (day before) Calendar.MONTH, Calendar.JUNE, Calendar.DST_OFFSET, HOURS/2, END_FIELDS, 16, 30, // expected hour, min }; for (int i=0; i<DATA.length; ) { int start = i; cal.clear(); // Set fields while (DATA[i] != END_FIELDS) { cal.set(DATA[i++], DATA[i++]); } ++i; // skip over END_FIELDS // Get hour/minute int h = cal.get(Calendar.HOUR_OF_DAY); int m = cal.get(Calendar.MINUTE); // Check if (h != DATA[i] || m != DATA[i+1]) { errln("Fail: expected " + DATA[i] + ":" + DATA[i+1] + ", got " + h + ":" + m + " after:"); while (DATA[start] != END_FIELDS) { logln("set(" + FIELD_NAME[DATA[start++]] + ", " + DATA[start++] + ");"); } } i += 2; // skip over expected hour, min } }
Example 11
Source File: CalendarRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * Test fieldDifference(). */ @Test public void TestJ438() throws Exception { int DATA[] = { 2000, Calendar.JANUARY, 20, 2010, Calendar.JUNE, 15, 2010, Calendar.JUNE, 15, 2000, Calendar.JANUARY, 20, 1964, Calendar.SEPTEMBER, 7, 1999, Calendar.JUNE, 4, 1999, Calendar.JUNE, 4, 1964, Calendar.SEPTEMBER, 7, }; Calendar cal = Calendar.getInstance(Locale.US); for (int i=0; i<DATA.length; i+=6) { int y1 = DATA[i]; int m1 = DATA[i+1]; int d1 = DATA[i+2]; int y2 = DATA[i+3]; int m2 = DATA[i+4]; int d2 = DATA[i+5]; cal.clear(); cal.set(y1, m1, d1); Date date1 = cal.getTime(); cal.set(y2, m2, d2); Date date2 = cal.getTime(); cal.setTime(date1); int dy = cal.fieldDifference(date2, Calendar.YEAR); int dm = cal.fieldDifference(date2, Calendar.MONTH); int dd = cal.fieldDifference(date2, Calendar.DATE); logln("" + date2 + " - " + date1 + " = " + dy + "y " + dm + "m " + dd + "d"); cal.setTime(date1); cal.add(Calendar.YEAR, dy); cal.add(Calendar.MONTH, dm); cal.add(Calendar.DATE, dd); Date date22 = cal.getTime(); if (!date2.equals(date22)) { errln("FAIL: " + date1 + " + " + dy + "y " + dm + "m " + dd + "d = " + date22 + ", exp " + date2); } else { logln("Ok: " + date1 + " + " + dy + "y " + dm + "m " + dd + "d = " + date22); } } }
Example 12
Source File: IBMCalendarTest.java From j2objc with Apache License 2.0 | 4 votes |
void setTo(Calendar cal) { cal.clear(); cal.set(year, month - 1, day, hour, min, sec); cal.set(Calendar.MILLISECOND, ms); }
Example 13
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
@Test public void TestT10619() { class TestDateFormatLeniencyItem { public boolean leniency; public String parseString; public String pattern; public String expectedResult; // null indicates expected error // Simple constructor public TestDateFormatLeniencyItem(boolean len, String parString, String patt, String expResult) { leniency = len; pattern = patt; parseString = parString; expectedResult = expResult; } }; final TestDateFormatLeniencyItem[] items = { // leniency parse String pattern expected result new TestDateFormatLeniencyItem(true, "2008-Jan 02", "yyyy-LLL. dd", "2008-Jan. 02"), new TestDateFormatLeniencyItem(false, "2008-Jan 03", "yyyy-LLL. dd", null), new TestDateFormatLeniencyItem(true, "2008-Jan--04", "yyyy-MMM' -- 'dd", "2008-Jan -- 04"), new TestDateFormatLeniencyItem(false, "2008-Jan--05", "yyyy-MMM' -- 'dd", null), new TestDateFormatLeniencyItem(true, "2008-12-31", "yyyy-mm-dd", "2008-12-31"), new TestDateFormatLeniencyItem(false, "6 Jan 05 2008", "eee MMM dd yyyy", null), new TestDateFormatLeniencyItem(true, "6 Jan 05 2008", "eee MMM dd yyyy", "Sat Jan 05 2008"), }; StringBuffer result = new StringBuffer(); Date d = new Date(); Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US); SimpleDateFormat sdfmt = new SimpleDateFormat(); ParsePosition p = new ParsePosition(0); for (TestDateFormatLeniencyItem item: items) { cal.clear(); sdfmt.setCalendar(cal); sdfmt.applyPattern(item.pattern); sdfmt.setLenient(item.leniency); result.setLength(0); p.setIndex(0); p.setErrorIndex(-1); d = sdfmt.parse(item.parseString, p); if(item.expectedResult == null) { if(p.getErrorIndex() != -1) continue; else errln("error: unexpected parse success..."+item.parseString + " w/ lenient="+item.leniency+" should have failed"); } if(p.getErrorIndex() != -1) { errln("error: parse error for string " +item.parseString + " -- idx["+p.getIndex()+"] errIdx["+p.getErrorIndex()+"]"); continue; } cal.setTime(d); result = sdfmt.format(cal, result, new FieldPosition(0)); if(!result.toString().equalsIgnoreCase(item.expectedResult)) { errln("error: unexpected format result. expected - " + item.expectedResult + " but result was - " + result); } else { logln("formatted results match! - " + result.toString()); } } }
Example 14
Source File: JapaneseTest.java From j2objc with Apache License 2.0 | 4 votes |
@Test public void Test3860() { ULocale loc = new ULocale("ja_JP@calendar=japanese"); Calendar cal = new JapaneseCalendar(loc); DateFormat enjformat = cal.getDateTimeFormat(0,0,new ULocale("en_JP@calendar=japanese")); DateFormat format = cal.getDateTimeFormat(0,0,loc); ((SimpleDateFormat)format).applyPattern("y.M.d"); // Note: just 'y' doesn't work here. ParsePosition pos = new ParsePosition(0); Date aDate = format.parse("1.1.9", pos); // after the start of heisei accession. Jan 1, 1H wouldn't work because it is actually showa 64 String inEn = enjformat.format(aDate); cal.clear(); cal.setTime(aDate); int gotYear = cal.get(Calendar.YEAR); int gotEra = cal.get(Calendar.ERA); int expectYear = 1; int expectEra = JapaneseCalendar.CURRENT_ERA; if((gotYear != expectYear) || (gotEra != expectEra)) { errln("Expected year " + expectYear + ", era " + expectEra +", but got year " + gotYear + " and era " + gotEra + ", == " + inEn); } else { logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn); } // Test parse with missing era (should default to current era, heisei) // Test parse with incomplete information logln("Testing parse w/ just year..."); Calendar cal2 = new JapaneseCalendar(loc); SimpleDateFormat fmt = new SimpleDateFormat("y", loc); SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian")); cal2.clear(); String samplestr = "1"; logln("Test Year: " + samplestr); try { aDate = fmt.parse(samplestr); } catch (ParseException pe) { errln("Error parsing " + samplestr); } ParsePosition pp = new ParsePosition(0); fmt.parse(samplestr, cal2, pp); logln("cal2 after 1 parse:"); String str = fmt2.format(aDate); logln("as Gregorian Calendar: " + str); cal2.setTime(aDate); gotYear = cal2.get(Calendar.YEAR); gotEra = cal2.get(Calendar.ERA); expectYear = 1; expectEra = JapaneseCalendar.CURRENT_ERA; if((gotYear != 1) || (gotEra != expectEra)) { errln("parse "+ samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear + " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")"); } else { logln(" year: " + gotYear + ", era: " + gotEra); } }
Example 15
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * @bug 4065240 */ @Test public void Test4065240() { Date curDate; DateFormat shortdate, fulldate; String strShortDate, strFullDate; Locale saveLocale = Locale.getDefault(); TimeZone saveZone = TimeZone.getDefault(); try { Locale curLocale = new Locale("de", "DE"); Locale.setDefault(curLocale); // {sfb} adoptDefault instead of setDefault //TimeZone.setDefault(TimeZone.createTimeZone("EST")); TimeZone.setDefault(TimeZone.getTimeZone("EST")); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(98 + 1900, 0, 1); curDate = cal.getTime(); shortdate = DateFormat.getDateInstance(DateFormat.SHORT); fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); strShortDate = "The current date (short form) is "; String temp; temp = shortdate.format(curDate); strShortDate += temp; strFullDate = "The current date (long form) is "; String temp2 = fulldate.format(curDate); strFullDate += temp2; logln(strShortDate); logln(strFullDate); // {sfb} What to do with resource bundle stuff????? // Check to see if the resource is present; if not, we can't test //ResourceBundle bundle = //The variable is never used // ICULocaleData.getBundle("DateFormatZoneData", curLocale); // {sfb} API change to ResourceBundle -- add getLocale() /*if (bundle.getLocale().getLanguage().equals("de")) { // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated if (!strFullDate.endsWith("GMT-05:00")) errln("Fail: Want GMT-05:00"); } else { logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***"); logln("*** FOR LOCALE de OR de_DE IS MISSING ***"); }*/ } catch (Exception e) { logln(e.getMessage()); } finally { Locale.setDefault(saveLocale); TimeZone.setDefault(saveZone); } }
Example 16
Source File: DangiTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * Make sure IS_LEAP_MONTH participates in field resolution. */ @Test public void TestResolution() { Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi")); DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT); // May 22 4334 = y4334 m4 d30 doy119 // May 23 4334 = y4334 m4* d1 doy120 final int THE_YEAR = 4334; final int END = -1; int[] DATA = { // Format: // (field, value)+, END, exp.month, exp.isLeapMonth, exp.DOM // Note: exp.month is ONE-BASED // If we set DAY_OF_YEAR only, that should be used Calendar.DAY_OF_YEAR, 1, END, 1,0,1, // Expect 1-1 // If we set MONTH only, that should be used Calendar.IS_LEAP_MONTH, 1, Calendar.DAY_OF_MONTH, 1, Calendar.MONTH, 3, END, 4,1,1, // Expect 4*-1 // If we set the DOY last, that should take precedence Calendar.MONTH, 1, // Should ignore Calendar.IS_LEAP_MONTH, 1, // Should ignore Calendar.DAY_OF_MONTH, 1, // Should ignore Calendar.DAY_OF_YEAR, 121, END, 4,1,2, // Expect 4*-2 // If we set IS_LEAP_MONTH last, that should take precedence Calendar.MONTH, 3, Calendar.DAY_OF_MONTH, 1, Calendar.DAY_OF_YEAR, 5, // Should ignore Calendar.IS_LEAP_MONTH, 1, END, 4,1,1, // Expect 4*-1 }; StringBuilder buf = new StringBuilder(); for (int i=0; i<DATA.length; ) { cal.clear(); cal.set(Calendar.EXTENDED_YEAR, THE_YEAR); buf.setLength(0); buf.append("EXTENDED_YEAR=" + THE_YEAR); while (DATA[i] != END) { cal.set(DATA[i++], DATA[i++]); buf.append(" " + fieldName(DATA[i-2]) + "=" + DATA[i-1]); } ++i; // Skip over END mark int expMonth = DATA[i++]-1; int expIsLeapMonth = DATA[i++]; int expDOM = DATA[i++]; int month = cal.get(Calendar.MONTH); int isLeapMonth = cal.get(Calendar.IS_LEAP_MONTH); int dom = cal.get(Calendar.DAY_OF_MONTH); if (expMonth == month && expIsLeapMonth == isLeapMonth && dom == expDOM) { logln("OK: " + buf + " => " + fmt.format(cal.getTime())); } else { String s = fmt.format(cal.getTime()); cal.clear(); cal.set(Calendar.EXTENDED_YEAR, THE_YEAR); cal.set(Calendar.MONTH, expMonth); cal.set(Calendar.IS_LEAP_MONTH, expIsLeapMonth); cal.set(Calendar.DAY_OF_MONTH, expDOM); errln("Fail: " + buf + " => " + s + "=" + (month+1) + "," + isLeapMonth + "," + dom + ", expected " + fmt.format(cal.getTime()) + "=" + (expMonth+1) + "," + expIsLeapMonth + "," + expDOM); } } }
Example 17
Source File: CalendarTestFmwk.java From j2objc with Apache License 2.0 | 4 votes |
/** * Iterates through a list of calendar <code>TestCase</code> objects and * makes sure that the time-to-fields and fields-to-time calculations work * correnctly for the values in each test case. */ protected void doTestCases(TestCase[] cases, Calendar cal) { cal.setTimeZone(UTC); // Get a format to use for printing dates in the calendar system we're testing DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.SHORT, -1, Locale.getDefault()); final String pattern = (cal instanceof ChineseCalendar) ? "E MMl/dd/y G HH:mm:ss.S z" : "E, MM/dd/yyyy G HH:mm:ss.S z"; ((SimpleDateFormat)format).applyPattern(pattern); // This format is used for printing Gregorian dates. DateFormat gregFormat = new SimpleDateFormat(pattern); gregFormat.setTimeZone(UTC); GregorianCalendar pureGreg = new GregorianCalendar(UTC); pureGreg.setGregorianChange(new Date(Long.MIN_VALUE)); DateFormat pureGregFmt = new SimpleDateFormat("E M/d/yyyy G"); pureGregFmt.setCalendar(pureGreg); // Now iterate through the test cases and see what happens for (int i = 0; i < cases.length; i++) { logln("\ntest case: " + i); TestCase test = cases[i]; // // First we want to make sure that the millis -> fields calculation works // test.applyTime will call setTime() on the calendar object, and // test.fieldsEqual will retrieve all of the field values and make sure // that they're the same as the ones in the testcase // test.applyTime(cal); if (!test.fieldsEqual(cal, this)) { errln("Fail: (millis=>fields) " + gregFormat.format(test.getTime()) + " => " + format.format(cal.getTime()) + ", expected " + test); } // // If that was OK, check the fields -> millis calculation // test.applyFields will set all of the calendar's fields to // match those in the test case. // cal.clear(); test.applyFields(cal); if (!test.equals(cal)) { errln("Fail: (fields=>millis) " + test + " => " + pureGregFmt.format(cal.getTime()) + ", expected " + pureGregFmt.format(test.getTime())); } } }
Example 18
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * @bug 4052408 */ @Test public void Test4052408() { DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(97 + 1900, Calendar.MAY, 3, 8, 55); Date dt = cal.getTime(); String str = fmt.format(dt); logln(str); if (!str.equals("5/3/97, 8:55 AM")) errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str); String expected[] = { "", //"ERA_FIELD", "97", //"YEAR_FIELD", "5", //"MONTH_FIELD", "3", //"DATE_FIELD", "", //"HOUR_OF_DAY1_FIELD", "", //"HOUR_OF_DAY0_FIELD", "55", //"MINUTE_FIELD", "", //"SECOND_FIELD", "", //"MILLISECOND_FIELD", "", //"DAY_OF_WEEK_FIELD", "", //"DAY_OF_YEAR_FIELD", "", //"DAY_OF_WEEK_IN_MONTH_FIELD", "", //"WEEK_OF_YEAR_FIELD", "", //"WEEK_OF_MONTH_FIELD", "AM", //"AM_PM_FIELD", "8", //"HOUR1_FIELD", "", //"HOUR0_FIELD", "" //"TIMEZONE_FIELD" }; String fieldNames[] = { "ERA_FIELD", "YEAR_FIELD", "MONTH_FIELD", "DATE_FIELD", "HOUR_OF_DAY1_FIELD", "HOUR_OF_DAY0_FIELD", "MINUTE_FIELD", "SECOND_FIELD", "MILLISECOND_FIELD", "DAY_OF_WEEK_FIELD", "DAY_OF_YEAR_FIELD", "DAY_OF_WEEK_IN_MONTH_FIELD", "WEEK_OF_YEAR_FIELD", "WEEK_OF_MONTH_FIELD", "AM_PM_FIELD", "HOUR1_FIELD", "HOUR0_FIELD", "TIMEZONE_FIELD"}; boolean pass = true; for (int i = 0; i <= 17; ++i) { FieldPosition pos = new FieldPosition(i); StringBuffer buf = new StringBuffer(""); fmt.format(dt, buf, pos); //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()]; String dst = buf.substring(pos.getBeginIndex(), pos.getEndIndex()); str = dst; log(i + ": " + fieldNames[i] + ", \"" + str + "\", " + pos.getBeginIndex() + ", " + pos.getEndIndex()); String exp = expected[i]; if ((exp.length() == 0 && str.length() == 0) || str.equals(exp)) logln(" ok"); else { logln(" expected " + exp); pass = false; } } if (!pass) errln("Fail: FieldPosition not set right by DateFormat"); }
Example 19
Source File: HolidayTest.java From j2objc with Apache License 2.0 | 4 votes |
@Test public void TestIsOn() { // jb 1901 SimpleHoliday sh = new SimpleHoliday(Calendar.AUGUST, 15, "Doug's Day", 1958, 2058); Calendar gcal = new GregorianCalendar(); gcal.clear(); gcal.set(Calendar.YEAR, 2000); gcal.set(Calendar.MONTH, Calendar.AUGUST); gcal.set(Calendar.DAY_OF_MONTH, 15); Date d0 = gcal.getTime(); gcal.add(Calendar.SECOND, 1); Date d1 = gcal.getTime(); gcal.add(Calendar.SECOND, -2); Date d2 = gcal.getTime(); gcal.add(Calendar.DAY_OF_MONTH, 1); Date d3 = gcal.getTime(); gcal.add(Calendar.SECOND, 1); Date d4 = gcal.getTime(); gcal.add(Calendar.SECOND, -2); gcal.set(Calendar.YEAR, 1957); Date d5 = gcal.getTime(); gcal.set(Calendar.YEAR, 1958); Date d6 = gcal.getTime(); gcal.set(Calendar.YEAR, 2058); Date d7 = gcal.getTime(); gcal.set(Calendar.YEAR, 2059); Date d8 = gcal.getTime(); Date[] dates = { d0, d1, d2, d3, d4, d5, d6, d7, d8 }; boolean[] isOns = { true, true, false, true, false, false, true, true, false }; for (int i = 0; i < dates.length; ++i) { Date d = dates[i]; logln("\ndate: " + d); boolean isOn = sh.isOn(d); logln("isOnDate: " + isOn); if (isOn != isOns[i]) { errln("date: " + d + " should be on Doug's Day!"); } Date h = sh.firstAfter(d); logln("firstAfter: " + h); } }
Example 20
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * @bug 4056591 * Verify the function of the [s|g]et2DigitYearStart() API. */ @Test public void Test4056591() { try { SimpleDateFormat fmt = new SimpleDateFormat("yyMMdd", Locale.US); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(1809, Calendar.DECEMBER, 25); Date start = cal.getTime(); fmt.set2DigitYearStart(start); if ((fmt.get2DigitYearStart() != start)) errln("get2DigitYearStart broken"); cal.clear(); cal.set(1809, Calendar.DECEMBER, 25); Date d1 = cal.getTime(); cal.clear(); cal.set(1909, Calendar.DECEMBER, 24); Date d2 = cal.getTime(); cal.clear(); cal.set(1809, Calendar.DECEMBER, 26); Date d3 = cal.getTime(); cal.clear(); cal.set(1861, Calendar.DECEMBER, 25); Date d4 = cal.getTime(); Date dates[] = {d1, d2, d3, d4}; String strings[] = {"091225", "091224", "091226", "611225"}; for (int i = 0; i < 4; i++) { String s = strings[i]; Date exp = dates[i]; Date got = fmt.parse(s); logln(s + " . " + got + "; exp " + exp); if (got.getTime() != exp.getTime()) errln("set2DigitYearStart broken"); } } catch (ParseException e) { errln("Fail: " + e); e.printStackTrace(); } }