use of android.icu.util.GregorianCalendar in project j2objc by google.
the class DateFormatTest method TestErrorChecking.
@Test
public void TestErrorChecking() {
try {
DateFormat.getDateTimeInstance(-1, -1, Locale.US);
errln("Expected exception for getDateTimeInstance(-1, -1, Locale)");
} catch (IllegalArgumentException e) {
logln("one ok");
} catch (Exception e) {
warnln("Expected IllegalArgumentException, got: " + e);
}
try {
DateFormat df = new SimpleDateFormat("aaNNccc");
df.format(new Date());
errln("Expected exception for format with bad pattern");
} catch (IllegalArgumentException ex) {
logln("two ok");
} catch (Exception e) {
warnln("Expected IllegalArgumentException, got: " + e);
}
{
// opposite of text
SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yy");
fmt.set2DigitYearStart(getDate(2003, Calendar.DECEMBER, 25));
String text = "12/25/03";
Calendar xcal = new GregorianCalendar();
xcal.setLenient(false);
ParsePosition pp = new ParsePosition(0);
// should get parse error on second field, not lenient
fmt.parse(text, xcal, pp);
if (pp.getErrorIndex() == -1) {
errln("Expected parse error");
} else {
logln("three ok");
}
}
}
use of android.icu.util.GregorianCalendar in project j2objc by google.
the class DateFormatTest method TestRelativeDateFormat.
// based on TestRelativeDateFormat() in icu/trunk/source/test/cintltst/cdattst.c
@Test
public void TestRelativeDateFormat() {
ULocale loc = ULocale.US;
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
Calendar cal = new GregorianCalendar(tz, loc);
Date now = new Date();
cal.setTime(now);
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 49);
cal.set(Calendar.SECOND, 0);
Date today = cal.getTime();
// minutes string to search for in formatted result
String minutesStr = "49";
int[] dateStylesList = { DateFormat.RELATIVE_FULL, DateFormat.RELATIVE_LONG, DateFormat.RELATIVE_MEDIUM, DateFormat.RELATIVE_SHORT };
for (int i = 0; i < dateStylesList.length; i++) {
int dateStyle = dateStylesList[i];
DateFormat fmtRelDateTime = DateFormat.getDateTimeInstance(dateStyle, DateFormat.SHORT, loc);
DateFormat fmtRelDate = DateFormat.getDateInstance(dateStyle, loc);
DateFormat fmtTime = DateFormat.getTimeInstance(DateFormat.SHORT, loc);
for (int dayOffset = -2; dayOffset <= 2; dayOffset++) {
StringBuffer dateTimeStr = new StringBuffer(64);
StringBuffer dateStr = new StringBuffer(64);
StringBuffer timeStr = new StringBuffer(64);
FieldPosition fp = new FieldPosition(DateFormat.MINUTE_FIELD);
cal.setTime(today);
cal.add(Calendar.DATE, dayOffset);
fmtRelDateTime.format(cal, dateTimeStr, fp);
fmtRelDate.format(cal, dateStr, new FieldPosition(0));
fmtTime.format(cal, timeStr, new FieldPosition(0));
logln(dayOffset + ", " + dateStyle + ", " + dateTimeStr);
logln(dayOffset + ", " + dateStyle + ", " + dateStr);
logln(dayOffset + ", " + dateStyle + ", " + timeStr);
// check that dateStr is in dateTimeStr
if (dateTimeStr.toString().indexOf(dateStr.toString()) < 0) {
errln("relative date string not found in datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
errln("datetime format is " + dateTimeStr.toString() + ", date string is " + dateStr.toString());
}
// check that timeStr is in dateTimeStr
if (dateTimeStr.toString().indexOf(timeStr.toString()) < 0) {
errln("short time string not found in datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
errln("datetime format is " + dateTimeStr.toString() + ", time string is " + timeStr.toString());
}
// check index of minutesStr
int minutesStrIndex = dateTimeStr.toString().indexOf(minutesStr);
if (fp.getBeginIndex() != minutesStrIndex) {
errln("FieldPosition beginIndex " + fp.getBeginIndex() + " instead of " + minutesStrIndex + " for datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
errln("datetime format is " + dateTimeStr.toString());
}
}
}
}
use of android.icu.util.GregorianCalendar in project j2objc by google.
the class DateFormatTest method TestTimeZoneDisplayName.
@Test
public void TestTimeZoneDisplayName() {
Calendar cal = new GregorianCalendar();
SimpleDateFormat testfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
testfmt.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
for (int i = 0; i < fallbackTests.length; ++i) {
String[] info = fallbackTests[i];
logln(info[0] + ";" + info[1] + ";" + info[2] + ";" + info[3]);
long time = 0;
try {
Date testd = testfmt.parse(info[2]);
time = testd.getTime();
} catch (ParseException pe) {
errln("Failed to parse test date data");
continue;
}
ULocale l = new ULocale(info[0]);
TimeZone tz = TimeZone.getTimeZone(info[1]);
SimpleDateFormat fmt = new SimpleDateFormat(info[3], l);
cal.setTimeInMillis(time);
cal.setTimeZone(tz);
String result = fmt.format(cal);
if (!result.equals(info[4])) {
errln(info[0] + ";" + info[1] + ";" + info[2] + ";" + info[3] + " expected: '" + info[4] + "' but got: '" + result + "'");
}
}
}
use of android.icu.util.GregorianCalendar in project j2objc by google.
the class CalendarRegressionTest method Test4288792.
/**
* WEEK_OF_YEAR computed incorrectly. A failure of this test can indicate a problem in several different places in
* the
*/
@Test
public void Test4288792() throws Exception {
TimeZone savedTZ = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
GregorianCalendar cal = new GregorianCalendar();
for (int i = 1900; i < 2100; i++) {
for (int j1 = 1; j1 <= 7; j1++) {
// Loop for MinimalDaysInFirstWeek: 1..7
for (int j = Calendar.SUNDAY; j <= Calendar.SATURDAY; j++) {
// Loop for FirstDayOfWeek: SUNDAY..SATURDAY
cal.clear();
cal.setMinimalDaysInFirstWeek(j1);
cal.setFirstDayOfWeek(j);
// Set the calendar to the first day of the last week
// of the year. This may overlap some of the start of
// the next year; that is, the last week of 1999 may
// include some of January 2000. Use the add() method
// to advance through the week. For each day, call
// get(WEEK_OF_YEAR). The result should be the same
// for the whole week. Note that a bug in
// getActualMaximum() will break this test.
// Set date to the mid year first before getActualMaximum(WEEK_OF_YEAR).
// getActualMaximum(WEEK_OF_YEAR) is based on the current calendar's
// year of week of year. After clear(), calendar is set to January 1st,
// which may belongs to previous year of week of year.
cal.set(i, Calendar.JULY, 1);
int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
cal.set(Calendar.WEEK_OF_YEAR, maxWeek);
cal.set(Calendar.DAY_OF_WEEK, j);
for (int k = 1; k < 7; k++) {
cal.add(Calendar.DATE, 1);
int WOY = cal.get(Calendar.WEEK_OF_YEAR);
if (WOY != maxWeek) {
errln(cal.getTime() + ",got=" + WOY + ",expected=" + maxWeek + ",min=" + j1 + ",first=" + j);
}
}
// Now advance the calendar one more day. This should
// put it at the first day of week 1 of the next year.
cal.add(Calendar.DATE, 1);
int WOY = cal.get(Calendar.WEEK_OF_YEAR);
if (WOY != 1) {
errln(cal.getTime() + ",got=" + WOY + ",expected=1,min=" + j1 + ",first" + j);
}
}
}
}
TimeZone.setDefault(savedTZ);
}
use of android.icu.util.GregorianCalendar in project j2objc by google.
the class CalendarRegressionTest method Test4096539.
@Test
public void Test4096539() {
int[] y = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int x = 0; x < 12; x++) {
GregorianCalendar gc = new GregorianCalendar(1997, x, y[x]);
int m1, m2;
log((m1 = gc.get(Calendar.MONTH) + 1) + "/" + gc.get(Calendar.DATE) + "/" + gc.get(Calendar.YEAR) + " + 1mo = ");
gc.add(Calendar.MONTH, 1);
logln((m2 = gc.get(Calendar.MONTH) + 1) + "/" + gc.get(Calendar.DATE) + "/" + gc.get(Calendar.YEAR));
int m = (m1 % 12) + 1;
if (m2 != m)
errln("Fail: Want " + m + " Got " + m2);
}
}
Aggregations