use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestFormalChineseDate.
@Test
public void TestFormalChineseDate() {
String pattern = "y\u5e74M\u6708d\u65e5";
String override = "y=hanidec;M=hans;d=hans";
// create formatter
SimpleDateFormat sdf = new SimpleDateFormat(pattern, override, ULocale.CHINA);
Calendar cal = Calendar.getInstance(ULocale.ENGLISH);
cal.clear(Calendar.MILLISECOND);
cal.set(2009, 6, 28, 0, 0, 0);
FieldPosition pos = new FieldPosition(0);
StringBuffer result = new StringBuffer();
sdf.format(cal, result, pos);
String res1 = result.toString();
String expected = "\u4e8c\u3007\u3007\u4e5d\u5e74\u4e03\u6708\u4e8c\u5341\u516b\u65e5";
if (!res1.equals(expected)) {
errln((String) "FAIL: -> " + result.toString() + " expected -> " + expected);
}
ParsePosition pp = new ParsePosition(0);
Date parsedate = sdf.parse(expected, pp);
long time1 = parsedate.getTime();
long time2 = cal.getTimeInMillis();
if (time1 != time2) {
errln("FAIL: parsed -> " + parsedate.toString() + " expected -> " + cal.toString());
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestCzechMonths459.
/**
* Verify the handling of Czech June and July, which have the unique attribute that
* one is a proper prefix substring of the other.
*/
@Test
public void TestCzechMonths459() {
DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, new Locale("cs", "", ""));
logln("Pattern " + ((SimpleDateFormat) fmt).toPattern());
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(1997, Calendar.JUNE, 15);
Date june = cal.getTime();
cal.clear();
cal.set(1997, Calendar.JULY, 15);
Date july = cal.getTime();
String juneStr = fmt.format(june);
String julyStr = fmt.format(july);
try {
logln("format(June 15 1997) = " + juneStr);
Date d = fmt.parse(juneStr);
String s = fmt.format(d);
int month, yr, day;
cal.setTime(d);
yr = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
logln(" . parse . " + s + " (month = " + month + ")");
if (month != Calendar.JUNE)
errln("FAIL: Month should be June");
if (yr != 1997)
errln("FAIL: Year should be 1997");
if (day != 15)
errln("FAIL: day should be 15");
logln("format(July 15 1997) = " + julyStr);
d = fmt.parse(julyStr);
s = fmt.format(d);
cal.setTime(d);
yr = cal.get(Calendar.YEAR) - 1900;
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_WEEK);
logln(" . parse . " + s + " (month = " + month + ")");
if (month != Calendar.JULY)
errln("FAIL: Month should be July");
} catch (ParseException e) {
errln(e.getMessage());
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestInvalidPattern.
@Test
public void TestInvalidPattern() {
Exception e = null;
SimpleDateFormat f = null;
String out = null;
try {
f = new SimpleDateFormat("Yesterday");
out = f.format(new Date(0));
} catch (IllegalArgumentException e1) {
e = e1;
}
if (e != null) {
logln("Ok: Received " + e.getMessage());
} else {
errln("FAIL: Expected exception, got " + f.toPattern() + "; " + out);
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestDateFormatLeniency.
@Test
public void TestDateFormatLeniency() {
class TestDateFormatLeniencyItem {
public ULocale locale;
public boolean leniency;
public String parseString;
public String pattern;
// null indicates expected error
public String expectedResult;
// Simple constructor
public TestDateFormatLeniencyItem(ULocale loc, boolean len, String parString, String patt, String expResult) {
locale = loc;
leniency = len;
pattern = patt;
parseString = parString;
expectedResult = expResult;
}
}
;
final TestDateFormatLeniencyItem[] items = { // locale leniency parse String pattern expected result
new TestDateFormatLeniencyItem(ULocale.ENGLISH, true, "2008-07 02", "yyyy-LLLL dd", "2008-July 02"), new TestDateFormatLeniencyItem(ULocale.ENGLISH, false, "2008-07 02", "yyyy-LLLL dd", null), new TestDateFormatLeniencyItem(ULocale.ENGLISH, true, "2008-Jan 02", "yyyy-LLL. dd", "2008-Jan. 02"), new TestDateFormatLeniencyItem(ULocale.ENGLISH, false, "2008-Jan 02", "yyyy-LLL. dd", null), new TestDateFormatLeniencyItem(ULocale.ENGLISH, true, "2008-Jan--02", "yyyy-MMM' -- 'dd", "2008-Jan -- 02"), new TestDateFormatLeniencyItem(ULocale.ENGLISH, false, "2008-Jan--02", "yyyy-MMM' -- 'dd", null) };
for (TestDateFormatLeniencyItem item : items) {
SimpleDateFormat sdfmt = new SimpleDateFormat(item.pattern, item.locale);
sdfmt.setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE, item.leniency).setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC, item.leniency).setBooleanAttribute(BooleanAttribute.PARSE_PARTIAL_LITERAL_MATCH, item.leniency);
ParsePosition p = new ParsePosition(0);
Date d = sdfmt.parse(item.parseString, p);
if (item.expectedResult == null) {
if (p.getErrorIndex() != -1)
continue;
else
errln("error: unexpected parse success..." + item.parseString + " w/ lenient=" + item.leniency + " should have failed");
}
if (p.getErrorIndex() != -1) {
errln("error: parse error for string " + item.parseString + " -- idx[" + p.getIndex() + "] errIdx[" + p.getErrorIndex() + "]");
continue;
}
String result = sdfmt.format(d);
if (!result.equalsIgnoreCase(item.expectedResult)) {
errln("error: unexpected format result. expected - " + item.expectedResult + " but result was - " + result);
} else {
logln("formatted results match! - " + result);
}
}
}
use of android.icu.text.SimpleDateFormat in project j2objc by google.
the class DateFormatTest method TestDateFormatZone061.
/**
* Test the formatting of time zones.
*/
@Test
public void TestDateFormatZone061() {
Date date;
DateFormat formatter;
date = new Date(859248000000L);
logln("Date 1997/3/25 00:00 GMT: " + date);
formatter = new SimpleDateFormat("dd-MMM-yyyyy HH:mm", Locale.UK);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
String temp = formatter.format(date);
logln("Formatted in GMT to: " + temp);
try {
Date tempDate = formatter.parse(temp);
logln("Parsed to: " + tempDate);
if (!tempDate.equals(date))
errln("FAIL: Expected " + date + " Got: " + tempDate);
} catch (Throwable t) {
System.out.println(t);
}
}
Aggregations