use of android.icu.text.DateFormat in project j2objc by google.
the class DateIntervalFormatTest method expectUserCLDR.
private void expectUserCLDR(String[] data, int data_length) {
int i = 1;
while (i < data_length) {
String locName = data[i++];
ULocale loc = new ULocale(locName);
SimpleDateFormat ref = new SimpleDateFormat(data[0], loc);
// 'f'
String datestr = data[i++];
String datestr_2 = data[i++];
Date date, date_2;
try {
date = ref.parse(datestr);
date_2 = ref.parse(datestr_2);
} catch (ParseException e) {
errln("parse exception" + e);
continue;
}
DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());
DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yyyyMMMdd", loc);
// DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yMd");
// SimpleDateFormat dtfmt = new SimpleDateFormat("yyyy 'year' MMM 'month' dd 'day'", loc);
// dtitvfmt.setDateFormat(dtfmt);
DateIntervalInfo dtitvinf = new DateIntervalInfo();
dtitvinf.setFallbackIntervalPattern("{0} --- {1}");
dtitvinf.setIntervalPattern("yMMMd", Calendar.YEAR, "'all diff'");
dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "yyyy 'diff' MMM d - MMM y");
dtitvinf.setIntervalPattern("yMMMd", Calendar.DATE, "yyyy MMM d ~ d");
dtitvinf.setIntervalPattern("yMMMd", Calendar.HOUR_OF_DAY, "yyyy MMMd HH:mm ~ HH:mm");
dtitvfmt.setDateIntervalInfo(dtitvinf);
FieldPosition pos = new FieldPosition(0);
StringBuffer str = new StringBuffer("");
DateFormat dtfmt = dtitvfmt.getDateFormat();
Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
fromCalendar.setTimeInMillis(dtitv.getFromDate());
toCalendar.setTimeInMillis(dtitv.getToDate());
dtitvfmt.format(fromCalendar, toCalendar, str, pos);
String expected = data[i++];
String formatted = dtitvfmt.format(dtitv).toString();
if (!formatted.equals(Utility.unescape(expected))) {
errln("CLDR: \"" + locName + "\\" + datestr + "\\" + datestr_2 + "\"\t expected: " + expected + "\tgot: " + formatted + "\n");
}
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateIntervalFormatTest method TestTicket11583.
@Test
public void TestTicket11583() {
ULocale[] locales = { ULocale.ENGLISH, SPANISH, LA_SPANISH };
String[] skeletons = { "yMMMMd", "yMMMM", "MMMM", "yMMMd", "yMMM", "MMM", "yMMd", "yMMdd", "yMM", "MM", "yMdd", "yMd", "yM", "M" };
final long startDate = 1232364615000L;
final long endDate = 1240399815000L;
// "yMMM";
String filterPattern = null;
for (ULocale locale : locales) {
for (String skeleton : skeletons) {
if (filterPattern != null && !skeleton.equals(filterPattern)) {
continue;
}
DateFormat dateFormat = DateFormat.getPatternInstance(skeleton, locale);
String dateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
DateIntervalFormat intervalFormat = DateIntervalFormat.getInstance(skeleton, locale);
DateIntervalInfo intervalInfo = intervalFormat.getDateIntervalInfo();
if (skeleton.equals(filterPattern)) {
logln(filterPattern + " => " + intervalInfo.getRawPatterns().get(filterPattern));
}
DateInterval date_interval = new DateInterval(startDate, endDate);
String interval = intervalFormat.format(date_interval);
String formattedStart = dateFormat.format(startDate);
String formattedEnd = dateFormat.format(endDate);
PatternInfo patternInfo = intervalFormat.getRawPatterns().get("M");
String firstPart = patternInfo.getFirstPart();
String secondPart = patternInfo.getSecondPart();
if (!matches(dateFormatPattern, firstPart, secondPart)) {
if (logKnownIssue("11585", "incompatible pattern between date format and date interval format")) {
logln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
} else {
errln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
}
}
logln(locale + "\tskeleton: «" + skeleton + "»\tpattern: «" + dateFormatPattern + "»\tintervalPattern1: «" + firstPart + "»\tintervalPattern2: «" + secondPart + "»\tstartDate: «" + formattedStart + "»\tendDate: «" + formattedEnd + "»\tinterval: «" + interval + "»");
}
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatTest method TestNonGregoFmtParse.
@Test
public void TestNonGregoFmtParse() {
class CalAndFmtTestItem {
public int era;
public int year;
public int month;
public int day;
public int hour;
public int minute;
public String formattedDate;
// Simple constructor
public CalAndFmtTestItem(int er, int yr, int mo, int da, int hr, int mi, String fd) {
era = er;
year = yr;
month = mo;
day = da;
hour = hr;
minute = mi;
formattedDate = fd;
}
}
;
// test items for he@calendar=hebrew, long date format
final CalAndFmtTestItem[] cafti_he_hebrew_long = { // era yr mo da hr mi formattedDate
new CalAndFmtTestItem(0, 4999, 12, 29, 12, 0, "\u05DB\u05F4\u05D8 \u05D1\u05D0\u05DC\u05D5\u05DC \u05D3\u05F3\u05EA\u05EA\u05E7\u05E6\u05F4\u05D8"), new CalAndFmtTestItem(0, 5100, 0, 1, 12, 0, "\u05D0\u05F3 \u05D1\u05EA\u05E9\u05E8\u05D9 \u05E7\u05F3"), new CalAndFmtTestItem(0, 5774, 5, 1, 12, 0, "\u05D0\u05F3 \u05D1\u05D0\u05D3\u05E8 \u05D0\u05F3 \u05EA\u05E9\u05E2\u05F4\u05D3"), new CalAndFmtTestItem(0, 5999, 12, 29, 12, 0, "\u05DB\u05F4\u05D8 \u05D1\u05D0\u05DC\u05D5\u05DC \u05EA\u05EA\u05E7\u05E6\u05F4\u05D8"), new CalAndFmtTestItem(0, 6100, 0, 1, 12, 0, "\u05D0\u05F3 \u05D1\u05EA\u05E9\u05E8\u05D9 \u05D5\u05F3\u05E7\u05F3") };
final CalAndFmtTestItem[] cafti_zh_chinese_custU = { // era yr mo da hr mi formattedDate
new CalAndFmtTestItem(78, 31, 0, 1, 12, 0, "2014\u7532\u5348\u5E74\u6B63\u67081"), new CalAndFmtTestItem(77, 31, 0, 1, 12, 0, "1954\u7532\u5348\u5E74\u6B63\u67081") };
final CalAndFmtTestItem[] cafti_zh_chinese_custNoU = { // era yr mo da hr mi formattedDate
new CalAndFmtTestItem(78, 31, 0, 1, 12, 0, "2014\u5E74\u6B63\u67081"), new CalAndFmtTestItem(77, 31, 0, 1, 12, 0, "1954\u5E74\u6B63\u67081") };
final CalAndFmtTestItem[] cafti_ja_japanese_custGy = { // era yr mo da hr mi formattedDate
new CalAndFmtTestItem(235, 26, 2, 5, 12, 0, "2014(\u5E73\u621026)\u5E743\u67085\u65E5"), new CalAndFmtTestItem(234, 60, 2, 5, 12, 0, "1985(\u662D\u548C60)\u5E743\u67085\u65E5") };
final CalAndFmtTestItem[] cafti_ja_japanese_custNoGy = { // era yr mo da hr mi formattedDate
new CalAndFmtTestItem(235, 26, 2, 5, 12, 0, "2014\u5E743\u67085\u65E5"), new CalAndFmtTestItem(234, 60, 2, 5, 12, 0, "1985\u5E743\u67085\u65E5") };
final CalAndFmtTestItem[] cafti_en_islamic_cust = { // era yr mo da hr mi formattedDate
new CalAndFmtTestItem(0, 1384, 0, 1, 12, 0, "1 Muh. 1384 AH, 1964"), new CalAndFmtTestItem(0, 1436, 0, 1, 12, 0, "1 Muh. 1436 AH, 2014"), new CalAndFmtTestItem(0, 1487, 0, 1, 12, 0, "1 Muh. 1487 AH, 2064") };
class TestNonGregoItem {
public String locale;
public int style;
// ignored unless style == DateFormat.NONE
public String pattern;
public CalAndFmtTestItem[] caftItems;
// Simple constructor
public TestNonGregoItem(String loc, int styl, String pat, CalAndFmtTestItem[] items) {
locale = loc;
style = styl;
pattern = pat;
caftItems = items;
}
}
;
final TestNonGregoItem[] items = { new TestNonGregoItem("he@calendar=hebrew", DateFormat.LONG, "", cafti_he_hebrew_long), new TestNonGregoItem("zh@calendar=chinese", DateFormat.NONE, "rU\u5E74MMMd", cafti_zh_chinese_custU), new TestNonGregoItem("zh@calendar=chinese", DateFormat.NONE, "r\u5E74MMMd", cafti_zh_chinese_custNoU), new TestNonGregoItem("ja@calendar=japanese", DateFormat.NONE, "r(Gy)\u5E74M\u6708d\u65E5", cafti_ja_japanese_custGy), new TestNonGregoItem("ja@calendar=japanese", DateFormat.NONE, "r\u5E74M\u6708d\u65E5", cafti_ja_japanese_custNoGy), new TestNonGregoItem("en@calendar=islamic", DateFormat.NONE, "d MMM y G, r", cafti_en_islamic_cust) };
for (TestNonGregoItem item : items) {
ULocale locale = new ULocale(item.locale);
DateFormat dfmt = null;
if (item.style != DateFormat.NONE) {
dfmt = DateFormat.getDateInstance(item.style, locale);
} else {
dfmt = new SimpleDateFormat(item.pattern, locale);
}
Calendar cal = dfmt.getCalendar();
for (CalAndFmtTestItem caftItem : item.caftItems) {
cal.clear();
cal.set(Calendar.ERA, caftItem.era);
cal.set(caftItem.year, caftItem.month, caftItem.day, caftItem.hour, caftItem.minute, 0);
StringBuffer result = new StringBuffer();
FieldPosition fpos = new FieldPosition(0);
dfmt.format(cal, result, fpos);
if (result.toString().compareTo(caftItem.formattedDate) != 0) {
errln("FAIL: date format for locale " + item.locale + ", style " + item.style + ", expected \"" + caftItem.formattedDate + "\", got \"" + result + "\"");
} else {
// formatted OK, try parse
ParsePosition ppos = new ParsePosition(0);
dfmt.parse(result.toString(), cal, ppos);
int era = cal.get(Calendar.ERA);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
if (ppos.getIndex() < result.length() || era != caftItem.era || year != caftItem.year || month != caftItem.month || day != caftItem.day) {
errln("FAIL: date parse for locale " + item.locale + ", style " + item.style + ", string \"" + result + "\", expected " + caftItem.era + ":" + caftItem.year + "-" + caftItem.month + "-" + caftItem.day + ", got pos " + ppos.getIndex() + " " + year + "-" + month + "-" + day);
}
}
}
}
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatTest method _testIt917.
// internal test subroutine, used by TestRunTogetherPattern917
private void _testIt917(SimpleDateFormat fmt, String str, Date expected) {
logln("pattern=" + fmt.toPattern() + " string=" + str);
Date o = new Date();
o = (Date) ((DateFormat) fmt).parseObject(str, new ParsePosition(0));
logln("Parsed object: " + o);
if (o == null || !o.equals(expected))
errln("FAIL: Expected " + expected);
String formatted = o == null ? "null" : ((DateFormat) fmt).format(o);
logln("Formatted string: " + formatted);
if (!formatted.equals(str))
errln("FAIL: Expected " + str);
}
use of android.icu.text.DateFormat in project j2objc by google.
the class DateFormatTest method TestCalendarType.
/*
* Test for checking SimpleDateFormat/DateFormatSymbols creation
* honor the calendar keyword in the given locale. See ticket#6100
*/
@Test
public void TestCalendarType() {
final String testPattern = "GGGG y MMMM d EEEE";
final ULocale[] testLocales = { new ULocale("de"), new ULocale("fr_FR@calendar=gregorian"), new ULocale("en@calendar=islamic"), new ULocale("ja_JP@calendar=japanese"), new ULocale("zh_Hans_CN@calendar=bogus"), new ULocale("ko_KR@calendar=dangi") };
SimpleDateFormat[] formatters = new SimpleDateFormat[5];
for (int i = 0; i < testLocales.length; i++) {
// Create a locale with no keywords
StringBuffer locStrBuf = new StringBuffer();
if (testLocales[i].getLanguage().length() > 0) {
locStrBuf.append(testLocales[i].getLanguage());
}
if (testLocales[i].getScript().length() > 0) {
locStrBuf.append('_');
locStrBuf.append(testLocales[i].getScript());
}
if (testLocales[i].getCountry().length() > 0) {
locStrBuf.append('_');
locStrBuf.append(testLocales[i].getCountry());
}
ULocale locNoKeywords = new ULocale(locStrBuf.toString());
Calendar cal = Calendar.getInstance(testLocales[i]);
// Calendar getDateFormat method
DateFormat df = cal.getDateTimeFormat(DateFormat.MEDIUM, DateFormat.MEDIUM, locNoKeywords);
if (df instanceof SimpleDateFormat) {
formatters[0] = (SimpleDateFormat) df;
formatters[0].applyPattern(testPattern);
} else {
formatters[0] = null;
}
// DateFormat constructor with locale
df = DateFormat.getDateInstance(DateFormat.MEDIUM, testLocales[i]);
if (df instanceof SimpleDateFormat) {
formatters[1] = (SimpleDateFormat) df;
formatters[1].applyPattern(testPattern);
} else {
formatters[1] = null;
}
// DateFormat constructor with Calendar
df = DateFormat.getDateInstance(cal, DateFormat.MEDIUM, locNoKeywords);
if (df instanceof SimpleDateFormat) {
formatters[2] = (SimpleDateFormat) df;
formatters[2].applyPattern(testPattern);
} else {
formatters[2] = null;
}
// SimpleDateFormat constructor
formatters[3] = new SimpleDateFormat(testPattern, testLocales[i]);
// SimpleDateFormat with DateFormatSymbols
DateFormatSymbols dfs = new DateFormatSymbols(testLocales[i]);
formatters[4] = new SimpleDateFormat(testPattern, dfs, testLocales[i]);
// All SimpleDateFormat instances should produce the exact
// same result.
String expected = null;
Date d = new Date();
for (int j = 0; j < formatters.length; j++) {
if (formatters[j] != null) {
String tmp = formatters[j].format(d);
if (expected == null) {
expected = tmp;
} else if (!expected.equals(tmp)) {
errln("FAIL: formatter[" + j + "] returned \"" + tmp + "\" in locale " + testLocales[i] + " - expected: " + expected);
}
}
}
}
}
Aggregations