Java Code Examples for android.icu.text.DateFormat#format()
The following examples show how to use
android.icu.text.DateFormat#format() .
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 4073003 */ @Test public void Test4073003() { try { DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); String tests[] = {"12/25/61", "12/25/1961", "4/3/2010", "4/3/10"}; for (int i = 0; i < 4; i += 2) { Date d = fmt.parse(tests[i]); Date dd = fmt.parse(tests[i + 1]); String s; s = fmt.format(d); String ss; ss = fmt.format(dd); if (d.getTime() != dd.getTime()) errln("Fail: " + d + " != " + dd); if (!s.equals(ss)) errln("Fail: " + s + " != " + ss); logln("Ok: " + s + " " + d); } } catch (ParseException e) { errln("Fail: " + e); e.printStackTrace(); } }
Example 2
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void TestDangiFormat() { DateFormat fmt = DateFormat.getDateInstance(DateFormat.MEDIUM, new ULocale("en@calendar=dangi")); String calType = fmt.getCalendar().getType(); assertEquals("Incorrect calendar type used by the date format instance", "dangi", calType); GregorianCalendar gcal = new GregorianCalendar(); gcal.set(2013, Calendar.MARCH, 1, 0, 0, 0); Date d = gcal.getTime(); String dangiDateStr = fmt.format(d); assertEquals("Bad date format", "Mo1 20, 2013", dangiDateStr); }
Example 3
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void TestT5683() { Locale[] aliasLocales = { new Locale("zh", "CN"), new Locale("zh", "TW"), new Locale("zh", "HK"), new Locale("zh", "SG"), new Locale("zh", "MO") }; ULocale[] canonicalLocales = { new ULocale("zh_Hans_CN"), new ULocale("zh_Hant_TW"), new ULocale("zh_Hant_HK"), new ULocale("zh_Hans_SG"), new ULocale("zh_Hant_MO") }; Date d = new Date(0); for (int i = 0; i < aliasLocales.length; i++) { DateFormat dfAlias = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, aliasLocales[i]); DateFormat dfCanonical = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, canonicalLocales[i]); String sAlias = dfAlias.format(d); String sCanonical = dfCanonical.format(d); if (!sAlias.equals(sCanonical)) { errln("Fail: The format result for locale " + aliasLocales[i] + " is different from the result for locale " + canonicalLocales[i] + ": " + sAlias + "[" + aliasLocales[i] + "] / " + sCanonical + "[" + canonicalLocales[i] + "]"); } } }
Example 4
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 5
Source File: TimeZoneBoundaryTest.java From j2objc with Apache License 2.0 | 5 votes |
private static String showDate(Date d, TimeZone zone) { DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); fmt.setTimeZone(zone); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.setTime(d); return "" + (cal.get(Calendar.YEAR) - 1900) + "/" + showNN(cal.get(Calendar.MONTH) + 1) + "/" + showNN(cal.get(Calendar.DAY_OF_MONTH)) + " " + showNN(cal.get(Calendar.HOUR_OF_DAY)) + ":" + showNN(cal.get(Calendar.MINUTE)) + " \"" + d + "\" = " + fmt.format(d) + " = " + d.getTime(); }
Example 6
Source File: HebrewTest.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void Test1624() { HebrewCalendar hc = new HebrewCalendar (5742, HebrewCalendar.AV, 22); DateFormat df = hc.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, Locale.getDefault()); String dateString = df.format(hc.getTime()); for (int year = 5600; year < 5800; year ++) { boolean leapYear = HebrewCalendar.isLeapYear (year); for (int month = HebrewCalendar.TISHRI; month <= HebrewCalendar.ELUL;month++) { // skip the adar 1 month if year is not a leap year if (leapYear == false && month == HebrewCalendar.ADAR_1) { continue; } int day = 15; hc = new HebrewCalendar (year, month, day); dateString = df.format(hc.getTime()); int dayHC = hc.get (HebrewCalendar.DATE); int monthHC = hc.get (HebrewCalendar.MONTH); int yearHC = hc.get (HebrewCalendar.YEAR); String header = "year:" + year + " isleap:" + leapYear + " " + dateString; if (dayHC != day) { errln (header + " ==> day:" + dayHC + " incorrect, should be:" + day); break; } if (monthHC != month) { errln (header + " ==> month:" + monthHC + " incorrect, should be:" + month); break; } if (yearHC != year) { errln (header + " ==> year:" + yearHC + " incorrecte, should be:" + year); break; } } } }
Example 7
Source File: NumberFormatRegressionTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * DateFormat should call setIntegerParseOnly(TRUE) on adopted * NumberFormat objects. */ @Test public void TestJ691() { Locale loc = new Locale("fr", "CH"); // set up the input date string & expected output String udt = "11.10.2000"; String exp = "11.10.00"; // create a Calendar for this locale Calendar cal = Calendar.getInstance(loc); // create a NumberFormat for this locale NumberFormat nf = NumberFormat.getInstance(loc); // *** Here's the key: We don't want to have to do THIS: //nf.setParseIntegerOnly(true); // create the DateFormat DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc); df.setCalendar(cal); df.setNumberFormat(nf); // set parsing to lenient & parse Date ulocdat = new Date(); df.setLenient(true); try { ulocdat = df.parse(udt); } catch (java.text.ParseException pe) { errln(pe.getMessage()); } // format back to a string String outString = df.format(ulocdat); if (!outString.equals(exp)) { errln("FAIL: " + udt + " => " + outString); } }
Example 8
Source File: DateFormatRegressionTestJ.java From j2objc with Apache License 2.0 | 5 votes |
@Test public void Test4250359() { Locale.setDefault(Locale.US); Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(101 + 1900, 9, 9, 17, 53); Date d = cal.getTime(); DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT); String act_result = tf.format(d); String exp_result = "5:53 PM"; if(!act_result.equals(exp_result)){ errln("The result is not expected"); } }
Example 9
Source File: DateIntervalFormatTest.java From j2objc with Apache License 2.0 | 4 votes |
@Test public void TestTicket11583 () { ULocale[] locales = { ULocale.ENGLISH, SPANISH, LA_SPANISH, }; String[] skeletons = { "yMMMMd", "yMMMM", "MMMM", "yMMMd", "yMMM", "MMM", "yMMd", "yMMdd", "yMM", "MM", "yMdd", "yMd", "yM", "M" }; final long startDate = 1232364615000L; final long endDate = 1240399815000L; String filterPattern = null ; // "yMMM"; for (ULocale locale : locales) { for (String skeleton : skeletons) { if (filterPattern != null && !skeleton.equals(filterPattern)) { continue; } DateFormat dateFormat = DateFormat.getPatternInstance(skeleton, locale); String dateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); DateIntervalFormat intervalFormat = DateIntervalFormat.getInstance(skeleton, locale); DateIntervalInfo intervalInfo = intervalFormat.getDateIntervalInfo(); if (skeleton.equals(filterPattern)) { logln(filterPattern + " => " + intervalInfo.getRawPatterns().get(filterPattern)); } DateInterval date_interval = new DateInterval(startDate, endDate); String interval = intervalFormat.format(date_interval); String formattedStart = dateFormat.format(startDate); String formattedEnd = dateFormat.format(endDate); PatternInfo patternInfo = intervalFormat.getRawPatterns().get("M"); String firstPart = patternInfo.getFirstPart(); String secondPart = patternInfo.getSecondPart(); if (!matches(dateFormatPattern, firstPart, secondPart)) { if (logKnownIssue("11585", "incompatible pattern between date format and date interval format")) { logln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "»."); } else { errln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "»."); } } logln(locale + "\tskeleton: «" + skeleton + "»\tpattern: «" + dateFormatPattern + "»\tintervalPattern1: «" + firstPart + "»\tintervalPattern2: «" + secondPart + "»\tstartDate: «" + formattedStart + "»\tendDate: «" + formattedEnd + "»\tinterval: «" + interval + "»" ); } } }
Example 10
Source File: DateFormatRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * @bug 4106807 */ @Test public void Test4106807() { Date dt; DateFormat df = DateFormat.getDateTimeInstance(); SimpleDateFormat sdfs[] = { new SimpleDateFormat("yyyyMMddHHmmss"), new SimpleDateFormat("yyyyMMddHHmmss'Z'"), new SimpleDateFormat("yyyyMMddHHmmss''"), new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), new SimpleDateFormat("yyyyMMddHHmmss %")}; String strings[] = { "19980211140000", "19980211140000", "19980211140000", "19980211140000a", "19980211140000 "}; GregorianCalendar gc = new GregorianCalendar(); TimeZone timeZone = TimeZone.getDefault(); TimeZone gmt = (TimeZone) timeZone.clone(); gmt.setRawOffset(0); for (int i = 0; i < 5; i++) { SimpleDateFormat format = sdfs[i]; String dateString = strings[i]; try { format.setTimeZone(gmt); dt = format.parse(dateString); // {sfb} some of these parses will fail purposely StringBuffer fmtd = new StringBuffer(""); FieldPosition pos = new FieldPosition(0); fmtd = df.format(dt, fmtd, pos); logln(fmtd.toString()); //logln(df.format(dt)); gc.setTime(dt); logln("" + gc.get(Calendar.ZONE_OFFSET)); StringBuffer s = new StringBuffer(""); s = format.format(dt, s, pos); logln(s.toString()); } catch (ParseException e) { logln("No way Jose"); } } }
Example 11
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 12
Source File: TimeZoneRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * getDisplayName doesn't work with unusual savings/offsets. */ @Test public void Test4176686() { // Construct a zone that does not observe DST but // that does have a DST savings (which should be ignored). int offset = 90 * 60000; // 1:30 SimpleTimeZone z1 = new SimpleTimeZone(offset, "_std_zone_"); z1.setDSTSavings(45 * 60000); // 0:45 // Construct a zone that observes DST for the first 6 months. SimpleTimeZone z2 = new SimpleTimeZone(offset, "_dst_zone_"); z2.setDSTSavings(45 * 60000); // 0:45 z2.setStartRule(Calendar.JANUARY, 1, 0); z2.setEndRule(Calendar.JULY, 1, 0); // Also check DateFormat DateFormat fmt1 = new SimpleDateFormat("z"); fmt1.setTimeZone(z1); // Format uses standard zone DateFormat fmt2 = new SimpleDateFormat("z"); fmt2.setTimeZone(z2); // Format uses DST zone java.util.Calendar tempcal = java.util.Calendar.getInstance(); tempcal.clear(); tempcal.set(1970, Calendar.FEBRUARY, 1); Date dst = tempcal.getTime(); // Time in DST tempcal.set(1970, Calendar.AUGUST, 1); Date std = tempcal.getTime(); // Time in standard // Description, Result, Expected Result String[] DATA = { "getDisplayName(false, SHORT)/std zone", z1.getDisplayName(false, TimeZone.SHORT), "GMT+1:30", "getDisplayName(false, LONG)/std zone", z1.getDisplayName(false, TimeZone.LONG ), "GMT+01:30", "getDisplayName(true, SHORT)/std zone", z1.getDisplayName(true, TimeZone.SHORT), "GMT+1:30", "getDisplayName(true, LONG)/std zone", z1.getDisplayName(true, TimeZone.LONG ), "GMT+01:30", "getDisplayName(false, SHORT)/dst zone", z2.getDisplayName(false, TimeZone.SHORT), "GMT+1:30", "getDisplayName(false, LONG)/dst zone", z2.getDisplayName(false, TimeZone.LONG ), "GMT+01:30", "getDisplayName(true, SHORT)/dst zone", z2.getDisplayName(true, TimeZone.SHORT), "GMT+2:15", "getDisplayName(true, LONG)/dst zone", z2.getDisplayName(true, TimeZone.LONG ), "GMT+02:15", "DateFormat.format(std)/std zone", fmt1.format(std), "GMT+1:30", "DateFormat.format(dst)/std zone", fmt1.format(dst), "GMT+1:30", "DateFormat.format(std)/dst zone", fmt2.format(std), "GMT+1:30", "DateFormat.format(dst)/dst zone", fmt2.format(dst), "GMT+2:15", }; for (int i=0; i<DATA.length; i+=3) { if (!DATA[i+1].equals(DATA[i+2])) { errln("FAIL: " + DATA[i] + " -> " + DATA[i+1] + ", exp " + DATA[i+2]); } } }
Example 13
Source File: CalendarRegressionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * DateFormat class mistakes date style and time style as follows: - * DateFormat.getDateTimeInstance takes date style as time style, and time * style as date style - If a Calendar is passed to * DateFormat.getDateInstance, it returns time instance - If a Calendar is * passed to DateFormat.getTimeInstance, it returns date instance */ @Test public void TestDateFormatFactoryJ26() { TimeZone zone = TimeZone.getDefault(); try { Locale loc = Locale.US; TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); java.util.Calendar tempcal = java.util.Calendar.getInstance(); tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53); Date date = tempcal.getTime(); Calendar cal = Calendar.getInstance(loc); Object[] DATA = { DateFormat.getDateInstance(DateFormat.SHORT, loc), "DateFormat.getDateInstance(DateFormat.SHORT, loc)", "4/5/01", DateFormat.getTimeInstance(DateFormat.SHORT, loc), "DateFormat.getTimeInstance(DateFormat.SHORT, loc)", "5:43 PM", DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc), "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)", "Thursday, April 5, 2001 at 5:43 PM", DateFormat.getDateInstance(cal, DateFormat.SHORT, loc), "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)", "4/5/01", DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc), "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)", "5:43 PM", DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc), "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)", "Thursday, April 5, 2001 at 5:43 PM", cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc), "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)", "4/5/01, 5:43:53 PM Pacific Daylight Time", cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc), "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)", "Thursday, April 5, 2001 at 5:43 PM", }; for (int i=0; i<DATA.length; i+=3) { DateFormat df = (DateFormat) DATA[i]; String desc = (String) DATA[i+1]; String exp = (String) DATA[i+2]; String got = df.format(date); if (got.equals(exp)) { logln("Ok: " + desc + " => " + got); } else { errln("FAIL: " + desc + " => " + got + ", expected " + exp); } } } finally { TimeZone.setDefault(zone); } }
Example 14
Source File: JapaneseTest.java From j2objc with Apache License 2.0 | 4 votes |
@Test public void Test5345parse() { // Test parse with incomplete information DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US); JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US")); SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese")); fmt.applyPattern("G y"); logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE)); //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese")); long aDateLong = -3197117222000L; // 1868-09-08 00:00 Pacific Time (GMT-07:52:58) if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) { // Java time zone implementation does not support LMTs aDateLong = -3197116800000L; // 1868-09-08 00:00 Pacific Time (GMT-08:00) } Date aDate = new Date(aDateLong); logln("aDate: " + aDate.toString() +", from " + aDateLong); String str; str = fmt2.format(aDate); logln("Test Date: " + str); str = fmt.format(aDate); logln("as Japanese Calendar: " + str); String expected = "Meiji 1"; if(!str.equals(expected)) { errln("FAIL: Expected " + expected + " but got " + str); } Date otherDate; try { otherDate = fmt.parse(expected); if(!otherDate.equals(aDate)) { String str3; // ParsePosition pp; Date dd = fmt.parse(expected); str3 = fmt.format(otherDate); long oLong = otherDate.getTime(); long aLong = otherDate.getTime(); errln("FAIL: Parse incorrect of " + expected + ": wanted " + aDate + " ("+aLong+"), but got " + " " + otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() ); } else { logln("Parsed OK: " + expected); } } catch(java.text.ParseException pe) { errln("FAIL: ParseException: " + pe.toString()); pe.printStackTrace(); } }
Example 15
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 16
Source File: ChineseTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * Make sure IS_LEAP_MONTH participates in field resolution. */ @Test public void TestResolution() { ChineseCalendar cal = new ChineseCalendar(); DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT); // May 22 2001 = y4638 m4 d30 doy119 // May 23 2001 = y4638 m4* d1 doy120 final int THE_YEAR = 4638; 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 // I've disabled this test because it doesn't work this way, // not even with a GregorianCalendar! MONTH alone isn't enough // to supersede DAY_OF_YEAR. Some other month-related field is // also required. - Liu 11/28/00 //! // If we set MONTH last, that should take precedence //! ChineseCalendar.IS_LEAP_MONTH, 1, //! Calendar.DAY_OF_MONTH, 1, //! Calendar.DAY_OF_YEAR, 5, // Should ignore //! Calendar.MONTH, 3, //! END, //! 4,1,1, // Expect 4*-1 // 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 }; StringBuffer buf = new StringBuffer(); 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: 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); } } }