use of android.icu.text.SimpleDateFormat 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.SimpleDateFormat 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.SimpleDateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4101483.
/**
* @bug 4101483
*/
@Test
public void Test4101483() {
SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.US);
FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
Date d = new Date(9234567890L);
StringBuffer buf = new StringBuffer("");
sdf.format(d, buf, fp);
logln(sdf.format(d, buf, fp).toString());
logln("beginIndex = " + fp.getBeginIndex());
logln("endIndex = " + fp.getEndIndex());
if (fp.getBeginIndex() == fp.getEndIndex())
errln("Fail: Empty field");
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4089106.
/**
* @bug 4089106
*/
@Test
public void Test4089106() {
TimeZone def = TimeZone.getDefault();
try {
TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000), "FAKEZONE");
TimeZone.setDefault(z);
// Android patch (http://b/28949992) start.
// ICU TimeZone.setDefault() not supported on Android.
z = TimeZone.getDefault();
// Android patch (http://b/28949992) end.
SimpleDateFormat f = new SimpleDateFormat();
if (!f.getTimeZone().equals(z))
errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
} finally {
TimeZone.setDefault(def);
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatRegressionTest method Test4151706.
/**
* @bug 4151706
* 'z' at end of date format throws index exception in SimpleDateFormat
* CANNOT REPRODUCE THIS BUG ON 1.2FCS
*/
@Test
public void Test4151706() {
String dateString = "Thursday, 31-Dec-98 23:00:00 GMT";
SimpleDateFormat fmt = new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss z", Locale.US);
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.US);
cal.clear();
cal.set(1998, Calendar.DECEMBER, 31, 23, 0, 0);
Date d = new Date();
try {
d = fmt.parse(dateString);
// {sfb} what about next two lines?
if (d.getTime() != cal.getTime().getTime())
errln("Incorrect value: " + d);
} catch (Exception e) {
errln("Fail: " + e);
}
StringBuffer temp = new StringBuffer("");
FieldPosition pos = new FieldPosition(0);
logln(dateString + " . " + fmt.format(d, temp, pos));
}
Aggregations