use of java.text.DateFormatSymbols in project j2objc by google.
the class DateFormatSymbolsTest method testSerialization.
public void testSerialization() throws Exception {
// Set the default locale. The default locale used to determine what strings were used by
// the DateFormatSymbols after deserialization. See http://b/16502916
Locale.setDefault(Locale.US);
// The Polish language needs stand-alone month and weekday names.
Locale pl = new Locale("pl");
DateFormatSymbols originalDfs = new DateFormatSymbols(pl);
// Serialize...
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(originalDfs);
byte[] bytes = out.toByteArray();
// Deserialize...
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
DateFormatSymbols deserializedDfs = (DateFormatSymbols) in.readObject();
assertEquals(-1, in.read());
// The two objects be equal.
assertEquals(originalDfs, deserializedDfs);
// The original differentiates between regular month names and stand-alone month names...
assertEquals("stycznia", formatDate(pl, "MMMM", originalDfs));
assertEquals("styczeń", formatDate(pl, "LLLL", originalDfs));
// And so does the deserialized version.
assertEquals("stycznia", formatDate(pl, "MMMM", deserializedDfs));
assertEquals("styczeń", formatDate(pl, "LLLL", deserializedDfs));
}
use of java.text.DateFormatSymbols in project j2objc by google.
the class DateFormatSymbolsTest method assertLocaleIsEquivalentToRoot.
private void assertLocaleIsEquivalentToRoot(Locale locale) {
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
assertEquals(DateFormatSymbols.getInstance(Locale.ROOT), dfs);
}
use of java.text.DateFormatSymbols in project j2objc by google.
the class Calendar method getDisplayName.
/**
* Returns the string representation of the calendar
* <code>field</code> value in the given <code>style</code> and
* <code>locale</code>. If no string representation is
* applicable, <code>null</code> is returned. This method calls
* {@link Calendar#get(int) get(field)} to get the calendar
* <code>field</code> value if the string representation is
* applicable to the given calendar <code>field</code>.
*
* <p>For example, if this <code>Calendar</code> is a
* <code>GregorianCalendar</code> and its date is 2005-01-01, then
* the string representation of the {@link #MONTH} field would be
* "January" in the long style in an English locale or "Jan" in
* the short style. However, no string representation would be
* available for the {@link #DAY_OF_MONTH} field, and this method
* would return <code>null</code>.
*
* <p>The default implementation supports the calendar fields for
* which a {@link DateFormatSymbols} has names in the given
* <code>locale</code>.
*
* @param field
* the calendar field for which the string representation
* is returned
* @param style
* the style applied to the string representation; one of
* {@link #SHORT} or {@link #LONG}.
* @param locale
* the locale for the string representation
* @return the string representation of the given
* <code>field</code> in the given <code>style</code>, or
* <code>null</code> if no string representation is
* applicable.
* @exception IllegalArgumentException
* if <code>field</code> or <code>style</code> is invalid,
* or if this <code>Calendar</code> is non-lenient and any
* of the calendar fields have invalid values
* @exception NullPointerException
* if <code>locale</code> is null
* @since 1.6
*/
public String getDisplayName(int field, int style, Locale locale) {
if (!checkDisplayNameParams(field, style, ALL_STYLES, LONG, locale, ERA_MASK | MONTH_MASK | DAY_OF_WEEK_MASK | AM_PM_MASK)) {
return null;
}
DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
String[] strings = getFieldStrings(field, style, symbols);
if (strings != null) {
int fieldValue = get(field);
if (fieldValue < strings.length) {
return strings[fieldValue];
}
}
return null;
}
use of java.text.DateFormatSymbols in project robovm by robovm.
the class Calendar method getDisplayNameArray.
private String[] getDisplayNameArray(int field, int style, Locale locale) {
if (field < 0 || field >= FIELD_COUNT) {
throw new IllegalArgumentException("bad field " + field);
}
checkStyle(style);
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
switch(field) {
case AM_PM:
return dfs.getAmPmStrings();
case DAY_OF_WEEK:
return (style == LONG) ? dfs.getWeekdays() : dfs.getShortWeekdays();
case ERA:
return dfs.getEras();
case MONTH:
return (style == LONG) ? dfs.getMonths() : dfs.getShortMonths();
}
return null;
}
use of java.text.DateFormatSymbols in project robovm by robovm.
the class Date method parse.
/**
* Returns the millisecond value of the date and time parsed from the
* specified {@code String}. Many date/time formats are recognized, including IETF
* standard syntax, i.e. Tue, 22 Jun 1999 12:16:00 GMT-0500
*
* @param string
* the String to parse.
* @return the millisecond value parsed from the String.
*
* @deprecated Use {@link DateFormat} instead.
*/
@Deprecated
public static long parse(String string) {
if (string == null) {
throw new IllegalArgumentException("The string argument is null");
}
char sign = 0;
int commentLevel = 0;
int offset = 0, length = string.length(), state = 0;
int year = -1, month = -1, date = -1;
int hour = -1, minute = -1, second = -1, zoneOffset = 0, minutesOffset = 0;
boolean zone = false;
final int PAD = 0, LETTERS = 1, NUMBERS = 2;
StringBuilder buffer = new StringBuilder();
while (offset <= length) {
char next = offset < length ? string.charAt(offset) : '\r';
offset++;
if (next == '(') {
commentLevel++;
}
if (commentLevel > 0) {
if (next == ')') {
commentLevel--;
}
if (commentLevel == 0) {
next = ' ';
} else {
continue;
}
}
int nextState = PAD;
if ('a' <= next && next <= 'z' || 'A' <= next && next <= 'Z') {
nextState = LETTERS;
} else if ('0' <= next && next <= '9') {
nextState = NUMBERS;
} else if (!Character.isSpace(next) && ",+-:/".indexOf(next) == -1) {
throw parseError(string);
}
if (state == NUMBERS && nextState != NUMBERS) {
int digit = Integer.parseInt(buffer.toString());
buffer.setLength(0);
if (sign == '+' || sign == '-') {
if (zoneOffset == 0) {
zone = true;
if (next == ':') {
minutesOffset = sign == '-' ? -Integer.parseInt(string.substring(offset, offset + 2)) : Integer.parseInt(string.substring(offset, offset + 2));
offset += 2;
}
zoneOffset = sign == '-' ? -digit : digit;
sign = 0;
} else {
throw parseError(string);
}
} else if (digit >= 70) {
if (year == -1 && (Character.isSpace(next) || next == ',' || next == '/' || next == '\r')) {
year = digit;
} else {
throw parseError(string);
}
} else if (next == ':') {
if (hour == -1) {
hour = digit;
} else if (minute == -1) {
minute = digit;
} else {
throw parseError(string);
}
} else if (next == '/') {
if (month == -1) {
month = digit - 1;
} else if (date == -1) {
date = digit;
} else {
throw parseError(string);
}
} else if (Character.isSpace(next) || next == ',' || next == '-' || next == '\r') {
if (hour != -1 && minute == -1) {
minute = digit;
} else if (minute != -1 && second == -1) {
second = digit;
} else if (date == -1) {
date = digit;
} else if (year == -1) {
year = digit;
} else {
throw parseError(string);
}
} else if (year == -1 && month != -1 && date != -1) {
year = digit;
} else {
throw parseError(string);
}
} else if (state == LETTERS && nextState != LETTERS) {
String text = buffer.toString().toUpperCase(Locale.US);
buffer.setLength(0);
if (text.length() == 1) {
throw parseError(string);
}
if (text.equals("AM")) {
if (hour == 12) {
hour = 0;
} else if (hour < 1 || hour > 12) {
throw parseError(string);
}
} else if (text.equals("PM")) {
if (hour == 12) {
hour = 0;
} else if (hour < 1 || hour > 12) {
throw parseError(string);
}
hour += 12;
} else {
DateFormatSymbols symbols = new DateFormatSymbols(Locale.US);
String[] weekdays = symbols.getWeekdays(), months = symbols.getMonths();
int value;
if (parse(text, weekdays) != -1) {
/* empty */
} else if (month == -1 && (month = parse(text, months)) != -1) {
/* empty */
} else if (text.equals("GMT") || text.equals("UT") || text.equals("UTC")) {
zone = true;
zoneOffset = 0;
} else if ((value = zone(text)) != 0) {
zone = true;
zoneOffset = value;
} else {
throw parseError(string);
}
}
}
if (next == '+' || (year != -1 && next == '-')) {
sign = next;
} else if (!Character.isSpace(next) && next != ',' && nextState != NUMBERS) {
sign = 0;
}
if (nextState == LETTERS || nextState == NUMBERS) {
buffer.append(next);
}
state = nextState;
}
if (year != -1 && month != -1 && date != -1) {
if (hour == -1) {
hour = 0;
}
if (minute == -1) {
minute = 0;
}
if (second == -1) {
second = 0;
}
if (year < (CREATION_YEAR - 80)) {
year += 2000;
} else if (year < 100) {
year += 1900;
}
minute -= minutesOffset;
if (zone) {
if (zoneOffset >= 24 || zoneOffset <= -24) {
hour -= zoneOffset / 100;
minute -= zoneOffset % 100;
} else {
hour -= zoneOffset;
}
return UTC(year - 1900, month, date, hour, minute, second);
}
return new Date(year - 1900, month, date, hour, minute, second).getTime();
}
throw parseError(string);
}
Aggregations