use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class CurrencyTest method TestAvailableCurrencyCodes.
@Test
public void TestAvailableCurrencyCodes() {
String[][] tests = { { "eo_AM", "1950-01-05" }, { "eo_AM", "1969-12-31", "SUR" }, { "eo_AM", "1991-12-26", "RUR" }, { "eo_AM", "2000-12-23", "AMD" }, { "eo_AD", "2000-12-23", "EUR", "ESP", "FRF", "ADP" }, { "eo_AD", "1969-12-31", "ESP", "FRF", "ADP" }, { "eo_AD", "1950-01-05", "ESP", "ADP" }, { "eo_AD", "1900-01-17", "ESP" }, { "eo_UA", "1994-12-25" }, { "eo_QQ", "1969-12-31" }, { "eo_AO", "2000-12-23", "AOA" }, { "eo_AO", "1995-12-25", "AOR", "AON" }, { "eo_AO", "1990-12-26", "AON", "AOK" }, { "eo_AO", "1979-12-29", "AOK" }, { "eo_AO", "1969-12-31" }, { "eo_DE@currency=DEM", "2000-12-23", "EUR", "DEM" }, { "eo-DE-u-cu-dem", "2000-12-23", "EUR", "DEM" }, { "en_US", null, "USD", "USN" }, { "en_US_PREEURO", null, "USD", "USN" }, { "en_US_Q", null, "USD", "USN" } };
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
for (String[] test : tests) {
ULocale locale = new ULocale(test[0]);
String timeString = test[1];
Date date;
if (timeString == null) {
date = new Date();
timeString = "today";
} else {
try {
date = fmt.parse(timeString);
} catch (Exception e) {
fail("could not parse date: " + timeString);
continue;
}
}
String[] expected = null;
if (test.length > 2) {
expected = new String[test.length - 2];
System.arraycopy(test, 2, expected, 0, expected.length);
}
String[] actual = Currency.getAvailableCurrencyCodes(locale, date);
// Order is not important as of 4.4. We never documented that it was.
Set<String> expectedSet = new HashSet<String>();
if (expected != null) {
expectedSet.addAll(Arrays.asList(expected));
}
Set<String> actualSet = new HashSet<String>();
if (actual != null) {
actualSet.addAll(Arrays.asList(actual));
}
assertEquals(locale + " on " + timeString, expectedSet, actualSet);
// Note: skip this test on Java 6 or older when keywords are available
if (locale.getKeywords() == null || TestUtil.getJavaVendor() == JavaVendor.Android || TestUtil.getJavaVersion() >= 7) {
Locale javaloc = locale.toLocale();
String[] actualWithJavaLocale = Currency.getAvailableCurrencyCodes(javaloc, date);
// should be exactly same with the ULocale version
boolean same = true;
if (actual == null) {
if (actualWithJavaLocale != null) {
same = false;
}
} else {
if (actualWithJavaLocale == null || actual.length != actualWithJavaLocale.length) {
same = false;
} else {
same = true;
for (int i = 0; i < actual.length; i++) {
if (!actual[i].equals(actualWithJavaLocale[i])) {
same = false;
break;
}
}
}
}
assertTrue("getAvailableCurrencyCodes with ULocale vs Locale", same);
}
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class CurrencyTest method testCurrencyMetaInfoRanges.
// A real test of CurrencyMetaInfo.
@Test
public void testCurrencyMetaInfoRanges() {
CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
assertNotNull("have metainfo", metainfo);
// must be capitalized
CurrencyFilter filter = CurrencyFilter.onRegion("DE");
List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
logln("currencies: " + currenciesInGermany.size());
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
Date demLastDate = new Date(Long.MAX_VALUE);
Date eurFirstDate = new Date(Long.MIN_VALUE);
for (CurrencyInfo info : currenciesInGermany) {
logln(info.toString());
logln("from: " + fmt.format(info.from) + Long.toHexString(info.from));
logln(" to: " + fmt.format(info.to) + Long.toHexString(info.to));
if (info.code.equals("DEM")) {
demLastDate = new Date(info.to);
} else if (info.code.equals("EUR")) {
eurFirstDate = new Date(info.from);
}
}
// the Euro and Deutschmark overlapped for several years
assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
// demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
Date demLastDatePlus1ms = new Date(demLastDate.getTime() + 1);
assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
// both available for start of euro
assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
// but not one millisecond before the start of the first day
Date eurFirstDateMinus1ms = new Date(eurFirstDate.getTime() - 1);
assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
// end time is last millisecond of day
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.setTime(demLastDate);
assertEquals("hour is 23", 23, cal.get(GregorianCalendar.HOUR_OF_DAY));
assertEquals("minute is 59", 59, cal.get(GregorianCalendar.MINUTE));
assertEquals("second is 59", 59, cal.get(GregorianCalendar.SECOND));
assertEquals("millisecond is 999", 999, cal.get(GregorianCalendar.MILLISECOND));
// start time is first millisecond of day
cal.setTime(eurFirstDate);
assertEquals("hour is 0", 0, cal.get(GregorianCalendar.HOUR_OF_DAY));
assertEquals("minute is 0", 0, cal.get(GregorianCalendar.MINUTE));
assertEquals("second is 0", 0, cal.get(GregorianCalendar.SECOND));
assertEquals("millisecond is 0", 0, cal.get(GregorianCalendar.MILLISECOND));
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestCoverage.
@Test
public void TestCoverage() {
Date now = new Date();
Calendar cal = new GregorianCalendar();
DateFormat f = DateFormat.getTimeInstance();
logln("time: " + f.format(now));
// sigh, everyone overrides this
int hash = f.hashCode();
f = DateFormat.getInstance(cal);
if (hash == f.hashCode()) {
errln("FAIL: hashCode equal for inequal objects");
}
logln("time again: " + f.format(now));
f = DateFormat.getTimeInstance(cal, DateFormat.FULL);
logln("time yet again: " + f.format(now));
f = DateFormat.getDateInstance();
logln("time yet again: " + f.format(now));
ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "de_DE");
DateFormatSymbols sym = new DateFormatSymbols(rb, Locale.GERMANY);
DateFormatSymbols sym2 = (DateFormatSymbols) sym.clone();
if (sym.hashCode() != sym2.hashCode()) {
errln("fail, date format symbols hashcode not equal");
}
if (!sym.equals(sym2)) {
errln("fail, date format symbols not equal");
}
Locale foo = new Locale("fu", "FU", "BAR");
rb = null;
sym = new DateFormatSymbols(GregorianCalendar.class, foo);
sym.equals(null);
sym = new ChineseDateFormatSymbols();
sym = new ChineseDateFormatSymbols(new Locale("en_US"));
try {
sym = new ChineseDateFormatSymbols(null, new Locale("en_US"));
errln("ChineseDateFormatSymbols(Calender, Locale) was suppose to return a null " + "pointer exception for a null paramater.");
} catch (Exception e) {
}
sym = new ChineseDateFormatSymbols(new ChineseCalendar(), new Locale("en_US"));
try {
sym = new ChineseDateFormatSymbols(null, new ULocale("en_US"));
errln("ChineseDateFormatSymbols(Calender, ULocale) was suppose to return a null " + "pointer exception for a null paramater.");
} catch (Exception e) {
}
sym = new ChineseDateFormatSymbols(new ChineseCalendar(), foo);
// cover new ChineseDateFormatSymbols(Calendar, ULocale)
ChineseCalendar ccal = new ChineseCalendar();
// gclsh1 add
sym = new ChineseDateFormatSymbols(ccal, ULocale.CHINA);
StringBuffer buf = new StringBuffer();
FieldPosition pos = new FieldPosition(0);
f.format((Object) cal, buf, pos);
f.format((Object) now, buf, pos);
f.format((Object) new Long(now.getTime()), buf, pos);
try {
f.format((Object) "Howdy", buf, pos);
} catch (Exception e) {
}
NumberFormat nf = f.getNumberFormat();
f.setNumberFormat(nf);
boolean lenient = f.isLenient();
f.setLenient(lenient);
ULocale uloc = f.getLocale(ULocale.ACTUAL_LOCALE);
DateFormat sdfmt = new SimpleDateFormat();
if (f.hashCode() != f.hashCode()) {
errln("hashCode is not stable");
}
if (!f.equals(f)) {
errln("f != f");
}
if (f.equals(null)) {
errln("f should not equal null");
}
if (f.equals(sdfmt)) {
errln("A time instance shouldn't equal a default date format");
}
Date d;
{
ChineseDateFormat fmt = new ChineseDateFormat("yymm", Locale.US);
try {
// fewer symbols than required 2
fmt.parse("2");
errln("whoops");
} catch (ParseException e) {
logln("ok");
}
try {
// should succeed with obeycount
fmt.parse("2255");
logln("ok");
} catch (ParseException e) {
errln("whoops");
}
try {
// not a number, should fail
fmt.parse("ni hao");
errln("whoops ni hao");
} catch (ParseException e) {
logln("ok ni hao");
}
}
{
Calendar xcal = new GregorianCalendar();
xcal.set(Calendar.HOUR_OF_DAY, 0);
DateFormat fmt = new SimpleDateFormat("k");
StringBuffer xbuf = new StringBuffer();
FieldPosition fpos = new FieldPosition(Calendar.HOUR_OF_DAY);
fmt.format(xcal, xbuf, fpos);
try {
fmt.parse(xbuf.toString());
logln("ok");
xbuf.setLength(0);
xcal.set(Calendar.HOUR_OF_DAY, 25);
fmt.format(xcal, xbuf, fpos);
Date d2 = fmt.parse(xbuf.toString());
logln("ok again - d2=" + d2);
} catch (ParseException e) {
errln("whoops");
}
}
{
// cover gmt+hh:mm
DateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
try {
d = fmt.parse("07/10/53 GMT+10:00");
logln("ok : d = " + d);
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+10:00 for pattern MM/dd/yy z");
}
// cover invalid separator after GMT
{
ParsePosition pp = new ParsePosition(0);
String text = "07/10/53 GMT=10:00";
d = fmt.parse(text, pp);
if (pp.getIndex() != 12) {
errln("Parse of 07/10/53 GMT=10:00 for pattern MM/dd/yy z");
}
logln("Parsing of the text stopped at pos: " + pp.getIndex() + " as expected and length is " + text.length());
}
// cover bad text after GMT+.
try {
fmt.parse("07/10/53 GMT+blecch");
logln("ok GMT+blecch");
} catch (ParseException e) {
errln("whoops GMT+blecch");
}
// cover bad text after GMT+hh:.
try {
fmt.parse("07/10/53 GMT+07:blecch");
logln("ok GMT+xx:blecch");
} catch (ParseException e) {
errln("whoops GMT+xx:blecch");
}
// cover no ':' GMT+#, # < 24 (hh)
try {
d = fmt.parse("07/10/53 GMT+07");
logln("ok GMT+07");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # > 24 (hhmm)
try {
d = fmt.parse("07/10/53 GMT+0730");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+0730 for pattern MM/dd/yy z");
}
// cover GMT+#, # with second field
try {
d = fmt.parse("07/10/53 GMT+07:30:15");
logln("ok GMT+07:30:15");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07:30:15 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # with second field, no leading zero
try {
d = fmt.parse("07/10/53 GMT+73015");
logln("ok GMT+73015");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+73015 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # with 1 digit second field
try {
d = fmt.parse("07/10/53 GMT+07300");
logln("ok GMT+07300");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07300 for pattern MM/dd/yy z");
}
// cover raw digits with no leading sign (bad RFC822)
try {
d = fmt.parse("07/10/53 07");
errln("Parse of 07/10/53 07 for pattern MM/dd/yy z passed!");
} catch (ParseException e) {
logln("ok");
}
// cover raw digits (RFC822)
try {
d = fmt.parse("07/10/53 +07");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 +07 for pattern MM/dd/yy z failed");
}
// cover raw digits (RFC822)
try {
d = fmt.parse("07/10/53 -0730");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 -00730 for pattern MM/dd/yy z failed");
}
// cover raw digits (RFC822) in DST
try {
fmt.setTimeZone(TimeZone.getTimeZone("PDT"));
d = fmt.parse("07/10/53 -0730");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 -0730 for pattern MM/dd/yy z failed");
}
}
// TODO: revisit toLocalizedPattern
if (false) {
SimpleDateFormat fmt = new SimpleDateFormat("aabbcc");
try {
String pat = fmt.toLocalizedPattern();
errln("whoops, shouldn't have been able to localize aabbcc");
} catch (IllegalArgumentException e) {
logln("aabbcc localize ok");
}
}
{
SimpleDateFormat fmt = new SimpleDateFormat("'aabbcc");
try {
fmt.toLocalizedPattern();
errln("whoops, localize unclosed quote");
} catch (IllegalArgumentException e) {
logln("localize unclosed quote ok");
}
}
{
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
// bogus time zone
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("recognized bogus time zone DBDY");
} catch (ParseException e) {
logln("time zone ex ok");
}
}
{
// force fallback to default timezone when fmt timezone
// is not named
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
// force fallback to default time zone, still fails
// not in equivalency group
fmt.setTimeZone(TimeZone.getTimeZone("GMT+0147"));
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
} catch (ParseException e) {
logln("time zone ex2 ok");
}
// force success on fallback
text = "08/15/58 " + TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);
try {
fmt.parse(text);
logln("found default tz");
} catch (ParseException e) {
errln("whoops, got parse exception");
}
}
{
// force fallback to symbols list of timezones when neither
// fmt and default timezone is named
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
TimeZone oldtz = TimeZone.getDefault();
// nonstandard tz
TimeZone newtz = TimeZone.getTimeZone("GMT+0137");
fmt.setTimeZone(newtz);
// todo: fix security issue
TimeZone.setDefault(newtz);
// fallback to symbol list, but fail
// try to parse the bogus time zone
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
} catch (ParseException e) {
logln("time zone ex3 ok");
} catch (Exception e) {
// hmmm... this shouldn't happen. don't want to exit this
// fn with timezone improperly set, so just in case
TimeZone.setDefault(oldtz);
throw new IllegalStateException(e.getMessage());
}
}
{
// cover getAvailableULocales
final ULocale[] locales = DateFormat.getAvailableULocales();
long count = locales.length;
if (count == 0) {
errln(" got a empty list for getAvailableULocales");
} else {
logln("" + count + " available ulocales");
}
}
{
// cover DateFormatSymbols.getDateFormatBundle
cal = new GregorianCalendar();
Locale loc = Locale.getDefault();
DateFormatSymbols mysym = new DateFormatSymbols(cal, loc);
if (mysym == null)
errln("FAIL: constructs DateFormatSymbols with calendar and locale failed");
uloc = ULocale.getDefault();
// These APIs are obsolete and return null
ResourceBundle resb = DateFormatSymbols.getDateFormatBundle(cal, loc);
ResourceBundle resb2 = DateFormatSymbols.getDateFormatBundle(cal, uloc);
ResourceBundle resb3 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), loc);
ResourceBundle resb4 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), uloc);
if (resb != null) {
logln("resb is not null");
}
if (resb2 != null) {
logln("resb2 is not null");
}
if (resb3 != null) {
logln("resb3 is not null");
}
if (resb4 != null) {
logln("resb4 is not null");
}
}
{
// cover DateFormatSymbols.getInstance
DateFormatSymbols datsym1 = DateFormatSymbols.getInstance();
DateFormatSymbols datsym2 = new DateFormatSymbols();
if (!datsym1.equals(datsym2)) {
errln("FAIL: DateFormatSymbols returned by getInstance()" + "does not match new DateFormatSymbols().");
}
datsym1 = DateFormatSymbols.getInstance(Locale.JAPAN);
datsym2 = DateFormatSymbols.getInstance(ULocale.JAPAN);
if (!datsym1.equals(datsym2)) {
errln("FAIL: DateFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
}
}
{
// cover DateFormatSymbols.getAvailableLocales/getAvailableULocales
Locale[] allLocales = DateFormatSymbols.getAvailableLocales();
if (allLocales.length == 0) {
errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
} else {
logln("PASS: " + allLocales.length + " available locales returned by DateFormatSymbols.getAvailableLocales");
}
ULocale[] allULocales = DateFormatSymbols.getAvailableULocales();
if (allULocales.length == 0) {
errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
} else {
logln("PASS: " + allULocales.length + " available locales returned by DateFormatSymbols.getAvailableULocales");
}
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestFormatToCharacterIterator.
/*
@Test
public void TestJB4757(){
DateFormat dfmt = DateFormat.getDateInstance(DateFormat.FULL, ULocale.ROOT);
}
*/
/*
* Test case for formatToCharacterIterator
*/
@Test
public void TestFormatToCharacterIterator() {
// Generate pattern string including all pattern letters with various length
AttributedCharacterIterator acit;
final char SEPCHAR = '~';
String[] patterns = new String[5];
StringBuffer sb = new StringBuffer();
for (int i = 0; i < patterns.length; i++) {
sb.setLength(0);
for (int j = 0; j < PATTERN_CHARS.length(); j++) {
if (j != 0) {
for (int k = 0; k <= i; k++) {
sb.append(SEPCHAR);
}
}
char letter = PATTERN_CHARS.charAt(j);
for (int k = 0; k <= i; k++) {
sb.append(letter);
}
}
patterns[i] = sb.toString();
}
if (isVerbose()) {
for (int i = 0; i < patterns.length; i++) {
logln("patterns[" + i + "] = " + patterns[i]);
}
}
Calendar cal = Calendar.getInstance();
cal.set(2007, Calendar.JULY, 16, 8, 20, 25);
cal.set(Calendar.MILLISECOND, 567);
final Date d = cal.getTime();
// Test AttributedCharacterIterator returned by SimpleDateFormat
for (int i = 0; i < patterns.length; i++) {
SimpleDateFormat sdf = new SimpleDateFormat(patterns[i]);
acit = sdf.formatToCharacterIterator(d);
int patidx = 0;
while (true) {
Map map = acit.getAttributes();
int limit = acit.getRunLimit();
if (map.isEmpty()) {
// Must be pattern literal - '~'
while (acit.getIndex() < limit) {
if (acit.current() != SEPCHAR) {
errln("FAIL: Invalid pattern literal at " + acit.current() + " in patterns[" + i + "]");
}
acit.next();
}
} else {
Set keySet = map.keySet();
if (keySet.size() == 1) {
// Check the attribute
Iterator keyIterator = keySet.iterator();
DateFormat.Field attr = (DateFormat.Field) keyIterator.next();
if (!DATEFORMAT_FIELDS[patidx].equals(attr)) {
errln("FAIL: The attribute at " + acit.getIndex() + " in patterns[" + i + "" + "] is " + attr + " - Expected: " + DATEFORMAT_FIELDS[patidx]);
}
} else {
// SimpleDateFormat#formatToCharacterIterator never set multiple
// attributes to a single text run.
errln("FAIL: Multiple attributes were set");
}
patidx++;
// Move to the run limit
acit.setIndex(limit);
}
if (acit.current() == CharacterIterator.DONE) {
break;
}
}
}
// ChineseDateFormat has pattern letter 'l' for leap month marker in addition to regular DateFormat
cal.clear();
// 26x78-5-30
cal.set(2009, Calendar.JUNE, 22);
// non-leap month
Date nonLeapMonthDate = cal.getTime();
// 26x78-5*-1
cal.set(2009, Calendar.JUNE, 23);
// leap month
Date leapMonthDate = cal.getTime();
ChineseDateFormat cdf = new ChineseDateFormat("y'x'G-Ml-d", ULocale.US);
acit = cdf.formatToCharacterIterator(nonLeapMonthDate);
Set keys = acit.getAllAttributeKeys();
if (keys.contains(ChineseDateFormat.Field.IS_LEAP_MONTH)) {
errln("FAIL: separate IS_LEAP_MONTH field should not be present for a Chinese calendar non-leap date" + cdf.format(nonLeapMonthDate));
}
acit = cdf.formatToCharacterIterator(leapMonthDate);
keys = acit.getAllAttributeKeys();
if (keys.contains(ChineseDateFormat.Field.IS_LEAP_MONTH)) {
errln("FAIL: separate IS_LEAP_MONTH field should no longer be present for a Chinese calendar leap date" + cdf.format(leapMonthDate));
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestMonthPatterns.
@Test
public void TestMonthPatterns() {
class ChineseCalTestDate {
public int era;
public int year;
// here 1-based
public int month;
public int isLeapMonth;
public int day;
// Simple constructor
public ChineseCalTestDate(int e, int y, int m, int il, int d) {
era = e;
year = y;
month = m;
isLeapMonth = il;
day = d;
}
}
;
final ChineseCalTestDate[] dates = { // (in chinese era 78) gregorian 2012-4-22
new ChineseCalTestDate(78, 29, 4, 0, 2), // (in chinese era 78) gregorian 2012-5-22
new ChineseCalTestDate(78, 29, 4, 1, 2), // (in chinese era 78) gregorian 2012-6-20
new ChineseCalTestDate(78, 29, 5, 0, 2) };
class MonthPatternItem {
public String locale;
public int style;
public String[] dateString;
// Simple constructor
public MonthPatternItem(String loc, int styl, String dateStr0, String dateStr1, String dateStr2) {
locale = loc;
style = styl;
dateString = new String[3];
dateString[0] = dateStr0;
dateString[1] = dateStr1;
dateString[2] = dateStr2;
}
}
;
final MonthPatternItem[] items = { new MonthPatternItem("root@calendar=chinese", DateFormat.LONG, "2012(ren-chen) M04 2", "2012(ren-chen) M04bis 2", "2012(ren-chen) M05 2"), new MonthPatternItem("root@calendar=chinese", DateFormat.SHORT, "2012-04-02", "2012-04bis-02", "2012-05-02"), new MonthPatternItem("root@calendar=chinese", -1, "29-4-2", "29-4bis-2", "29-5-2"), new MonthPatternItem("root@calendar=chinese", -2, "78x29-4-2", "78x29-4bis-2", "78x29-5-2"), new MonthPatternItem("root@calendar=chinese", -3, "ren-chen-4-2", "ren-chen-4bis-2", "ren-chen-5-2"), new MonthPatternItem("root@calendar=chinese", -4, "ren-chen M04 2", "ren-chen M04bis 2", "ren-chen M05 2"), new MonthPatternItem("en@calendar=gregorian", -3, "2012-4-22", "2012-5-22", "2012-6-20"), new MonthPatternItem("en@calendar=chinese", DateFormat.LONG, "Fourth Month 2, 2012(ren-chen)", "Fourth Monthbis 2, 2012(ren-chen)", "Fifth Month 2, 2012(ren-chen)"), new MonthPatternItem("en@calendar=chinese", DateFormat.SHORT, "4/2/2012", "4bis/2/2012", "5/2/2012"), new MonthPatternItem("zh@calendar=chinese", DateFormat.LONG, "2012\u58EC\u8FB0\u5E74\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u95F0\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u4E94\u6708\u521D\u4E8C"), new MonthPatternItem("zh@calendar=chinese", DateFormat.SHORT, "2012/4/2", "2012/\u95F04/2", "2012/5/2"), new MonthPatternItem("zh@calendar=chinese", -3, "\u58EC\u8FB0-4-2", "\u58EC\u8FB0-\u95F04-2", "\u58EC\u8FB0-5-2"), new MonthPatternItem("zh@calendar=chinese", -4, "\u58EC\u8FB0 \u56DB\u6708 2", "\u58EC\u8FB0 \u95F0\u56DB\u6708 2", "\u58EC\u8FB0 \u4E94\u6708 2"), new MonthPatternItem("zh_Hant@calendar=chinese", DateFormat.LONG, "2012\u58EC\u8FB0\u5E74\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u958F\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u4E94\u6708\u521D\u4E8C"), new MonthPatternItem("zh_Hant@calendar=chinese", DateFormat.SHORT, "2012/4/2", "2012/\u958F4/2", "2012/5/2"), new MonthPatternItem("fr@calendar=chinese", DateFormat.LONG, "2 s\u00ECyu\u00E8 ren-chen", "2 s\u00ECyu\u00E8bis ren-chen", "2 w\u01D4yu\u00E8 ren-chen"), new MonthPatternItem("fr@calendar=chinese", DateFormat.SHORT, "2/4/29", "2/4bis/29", "2/5/29"), new MonthPatternItem("en@calendar=dangi", DateFormat.LONG, "Third Monthbis 2, 2012(ren-chen)", "Fourth Month 2, 2012(ren-chen)", "Fifth Month 1, 2012(ren-chen)"), new MonthPatternItem("en@calendar=dangi", DateFormat.SHORT, "3bis/2/2012", "4/2/2012", "5/1/2012"), new MonthPatternItem("en@calendar=dangi", -2, "78x29-3bis-2", "78x29-4-2", "78x29-5-1"), new MonthPatternItem("ko@calendar=dangi", DateFormat.LONG, "\uC784\uC9C4\uB144 \uC7243\uC6D4 2\uC77C", "\uC784\uC9C4\uB144 4\uC6D4 2\uC77C", "\uC784\uC9C4\uB144 5\uC6D4 1\uC77C"), new MonthPatternItem("ko@calendar=dangi", DateFormat.SHORT, "29. \uC7243. 2.", "29. 4. 2.", "29. 5. 1.") };
// style: -1 -2 -3 -4
// previously G and l for chinese cal only handled by ChineseDateFormat
final String[] customPatterns = { "y-Ml-d", "G'x'y-Ml-d", "U-M-d", "U MMM d" };
Calendar rootChineseCalendar = Calendar.getInstance(new ULocale("root@calendar=chinese"));
for (MonthPatternItem item : items) {
ULocale locale = new ULocale(item.locale);
DateFormat dfmt = (item.style >= 0) ? DateFormat.getDateInstance(item.style, locale) : new SimpleDateFormat(customPatterns[-item.style - 1], locale);
int idate = 0;
for (ChineseCalTestDate date : dates) {
rootChineseCalendar.clear();
rootChineseCalendar.set(Calendar.ERA, date.era);
rootChineseCalendar.set(date.year, date.month - 1, date.day);
rootChineseCalendar.set(Calendar.IS_LEAP_MONTH, date.isLeapMonth);
StringBuffer result = new StringBuffer();
FieldPosition fpos = new FieldPosition(0);
dfmt.format(rootChineseCalendar, result, fpos);
if (result.toString().compareTo(item.dateString[idate]) != 0) {
errln("FAIL: Chinese calendar format for locale " + item.locale + ", style " + item.style + ", expected \"" + item.dateString[idate] + "\", got \"" + result + "\"");
} else {
// formatted OK, try parse
ParsePosition ppos = new ParsePosition(0);
// ensure we are really parsing the fields we should be
rootChineseCalendar.set(Calendar.YEAR, 1);
rootChineseCalendar.set(Calendar.MONTH, 0);
rootChineseCalendar.set(Calendar.IS_LEAP_MONTH, 0);
rootChineseCalendar.set(Calendar.DATE, 1);
//
dfmt.parse(result.toString(), rootChineseCalendar, ppos);
int era = rootChineseCalendar.get(Calendar.ERA);
int year = rootChineseCalendar.get(Calendar.YEAR);
int month = rootChineseCalendar.get(Calendar.MONTH) + 1;
int isLeapMonth = rootChineseCalendar.get(Calendar.IS_LEAP_MONTH);
int day = rootChineseCalendar.get(Calendar.DATE);
if (ppos.getIndex() < result.length() || year != date.year || month != date.month || isLeapMonth != date.isLeapMonth || day != date.day) {
errln("FAIL: Chinese calendar parse for locale " + item.locale + ", style " + item.style + ", string \"" + result + "\", expected " + date.year + "-" + date.month + "(" + date.isLeapMonth + ")-" + date.day + ", got pos " + ppos.getIndex() + " era(" + era + ")-" + year + "-" + month + "(" + isLeapMonth + ")-" + day);
}
}
idate++;
}
}
}
Aggregations