use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4100302.
/**
* @bug 4100302
*/
@Test
public void Test4100302() {
Locale[] locales = { Locale.CANADA, Locale.CANADA_FRENCH, Locale.CHINA, Locale.CHINESE, Locale.ENGLISH, Locale.FRANCE, Locale.FRENCH, Locale.GERMAN, Locale.GERMANY, Locale.ITALIAN, Locale.ITALY, Locale.JAPAN, Locale.JAPANESE, Locale.KOREA, Locale.KOREAN, Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, Locale.UK, Locale.US };
try {
boolean pass = true;
for (int i = 0; i < 21; i++) {
Format format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locales[i]);
byte[] bytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(format);
oos.flush();
baos.close();
bytes = baos.toByteArray();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object o = ois.readObject();
if (!format.equals(o)) {
pass = false;
logln("DateFormat instance for locale " + locales[i] + " is incorrectly serialized/deserialized.");
} else {
logln("DateFormat instance for locale " + locales[i] + " is OKAY.");
}
}
if (!pass)
errln("Fail: DateFormat serialization/equality bug");
} catch (OptionalDataException e) {
errln("Fail: " + e);
} catch (IOException e) {
errln("Fail: " + e);
} catch (ClassNotFoundException e) {
errln("Fail: " + e);
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4106807.
/**
* @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");
}
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DataDrivenFormatTest method testConvertDate.
private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) {
DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
int n = 0;
for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
++n;
long now = System.currentTimeMillis();
DataMap currentCase = (DataMap) iter.next();
String caseString = "[" + testData.getName() + "#" + n + (fmt ? "format" : "parse") + "]";
String locale = currentCase.getString("locale");
String zone = currentCase.getString("zone");
String spec = currentCase.getString("spec");
String date = currentCase.getString("date");
String str = currentCase.getString("str");
Date fromDate = null;
boolean useDate = false;
ULocale loc = new ULocale(locale);
String pattern = null;
// boolean usePattern = false;
DateFormat format = null;
DateTimeStyleSet styleSet;
CalendarFieldsSet fromSet = null;
// parse 'spec' - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y'
if (spec.startsWith(kPATTERN)) {
pattern = spec.substring(kPATTERN.length());
// usePattern = true;
format = new SimpleDateFormat(pattern, loc);
} else {
styleSet = new DateTimeStyleSet();
styleSet.parseFrom(spec);
format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
}
Calendar cal = Calendar.getInstance(loc);
if (zone.length() > 0) {
TimeZone tz = TimeZone.getFrozenTimeZone(zone);
cal.setTimeZone(tz);
format.setTimeZone(tz);
}
// parse 'date' - either 'MILLIS=12345' or a CalendarFieldsSet
if (date.startsWith(kMILLIS)) {
useDate = true;
fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
} else if (date.startsWith(kRELATIVE_MILLIS)) {
useDate = true;
fromDate = new Date(now + Long.parseLong(date.substring(kRELATIVE_MILLIS.length())));
} else if (date.startsWith(kRELATIVE_ADD)) {
// "add" is a string indicating which fields to add
String add = date.substring(kRELATIVE_ADD.length());
CalendarFieldsSet addSet = new CalendarFieldsSet();
addSet.parseFrom(add);
useDate = true;
cal.clear();
cal.setTimeInMillis(now);
// / perform op on 'to calendar'
for (int q = 0; q < addSet.fieldCount(); q++) {
if (addSet.isSet(q)) {
if (q == Calendar.DATE) {
cal.add(q, addSet.get(q));
} else {
cal.set(q, addSet.get(q));
}
}
}
fromDate = cal.getTime();
} else {
fromSet = new CalendarFieldsSet();
fromSet.parseFrom(date);
}
// run the test
if (fmt) {
StringBuffer output = new StringBuffer();
cal.clear();
FieldPosition pos = new FieldPosition(0);
if (useDate) {
output = format.format(fromDate, output, pos);
} else {
fromSet.setOnCalendar(cal);
format.format(cal, output, pos);
}
if (output.toString().equals(str)) {
logln(caseString + " Success - strings match: " + output);
} else {
errln(caseString + " FAIL: got " + output + " expected " + str);
}
} else {
// parse
cal.clear();
ParsePosition pos = new ParsePosition(0);
format.parse(str, cal, pos);
if (useDate) {
Date gotDate = cal.getTime();
if (gotDate.equals(fromDate)) {
logln(caseString + " SUCCESS: got=parse=" + str);
} else {
errln(caseString + " FAIL: parsed " + str + " but got " + basicFmt.format(gotDate) + " - " + gotDate + " expected " + basicFmt.format(fromDate));
}
} else {
CalendarFieldsSet diffSet = new CalendarFieldsSet();
if (!fromSet.matches(cal, diffSet)) {
String diffs = diffSet.diffFrom(fromSet);
errln(caseString + " FAIL: differences: " + diffs);
} else {
logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());
}
}
}
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4065240.
/**
* @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);
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4071441.
/*
DateFormat.equals is too narrowly defined. As a result, MessageFormat
does not work correctly. DateFormat.equals needs to be written so
that the Calendar sub-object is not compared using Calendar.equals,
but rather compared for equivalency. This may necessitate adding a
(package private) method to Calendar to test for equivalency.
Currently this bug breaks MessageFormat.toPattern
*/
/**
* @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");
}
Aggregations