use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatTest method TestQuotePattern161.
/**
* Test the handling of single quotes in patterns.
*/
@Test
public void TestQuotePattern161() {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mm:ss a zzz", Locale.US);
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(1997, Calendar.AUGUST, 13, 10, 42, 28);
Date currentTime_1 = cal.getTime();
String dateString = ((DateFormat) formatter).format(currentTime_1);
String exp = "08/13/1997 at 10:42:28 AM ";
logln("format(" + currentTime_1 + ") = " + dateString);
if (!dateString.substring(0, exp.length()).equals(exp))
errln("FAIL: Expected " + exp);
}
use of android.icu.text.DateFormat 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.text.DateFormat 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.text.DateFormat in project j2objc by google.
the class DateFormatTest method TestEquals.
@Test
public void TestEquals() {
DateFormat fmtA = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
DateFormat fmtB = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
if (!fmtA.equals(fmtB))
errln("FAIL");
}
use of android.icu.text.DateFormat in project j2objc by google.
the class Calendar method formatHelper.
private static DateFormat formatHelper(Calendar cal, ULocale loc, int dateStyle, int timeStyle) {
if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) {
throw new IllegalArgumentException("Illegal time style " + timeStyle);
}
if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) {
throw new IllegalArgumentException("Illegal date style " + dateStyle);
}
PatternData patternData = PatternData.make(cal, loc);
String override = null;
// Resolve a pattern for the date/time style
String pattern = null;
if ((timeStyle >= 0) && (dateStyle >= 0)) {
pattern = SimpleFormatterImpl.formatRawPattern(patternData.getDateTimePattern(dateStyle), 2, 2, patternData.patterns[timeStyle], patternData.patterns[dateStyle + 4]);
// time style.
if (patternData.overrides != null) {
String dateOverride = patternData.overrides[dateStyle + 4];
String timeOverride = patternData.overrides[timeStyle];
override = mergeOverrideStrings(patternData.patterns[dateStyle + 4], patternData.patterns[timeStyle], dateOverride, timeOverride);
}
} else if (timeStyle >= 0) {
pattern = patternData.patterns[timeStyle];
if (patternData.overrides != null) {
override = patternData.overrides[timeStyle];
}
} else if (dateStyle >= 0) {
pattern = patternData.patterns[dateStyle + 4];
if (patternData.overrides != null) {
override = patternData.overrides[dateStyle + 4];
}
} else {
throw new IllegalArgumentException("No date or time style specified");
}
DateFormat result = cal.handleGetDateFormat(pattern, override, loc);
result.setCalendar(cal);
return result;
}
Aggregations