DateTimeFormatpublic class DateTimeFormat extends Object DateTimeFormat is a concrete class for formatting dates in a
locale-sensitive manner. It allows for formatting (date -> text).
DateTimeFormat allows you to start by choosing one of
predefined patterns for date-time formatting passing appropriate style to
factory method getInstance(style, locale) .
a
|
am/pm
|
H
|
hour in day 0-23
|
h
|
hour in am/pm 0-11
|
K
|
hour in day 1-24
|
k
|
hour in am/pm 1-12
|
m
|
minute 0-59
|
s
|
second 0-59
|
d
|
day in month (number)
|
dd
|
day in month (number 2 digits)
|
EE
|
day in week short "Mon"
|
EEEE
|
day in week long "Monday"
|
M
|
month in year (number)
|
MM
|
month in year (number 2 digits)
|
MMM
|
month in year short "Oct"
|
MMMM
|
month in year long "October"
|
yy
|
year short "05"
|
yyyy
|
year long "2005"
|
G
|
era
|
z
|
timezone
|
|
Fields Summary |
---|
private DateFormatSymbols | symbolsHolds initialized instance of DateFormatSymbols which
encapsulate locale-dependent information like names of days in week,
months etc. | private int | styleStyle of date/time formatting. |
Constructors Summary |
---|
public DateTimeFormat(int style, DateFormatSymbols symbols)Creates new DateTimeFormat object. It is assumed that
style contains correct value and symbols
refers to properly initialized DateFormatSymbols object.
this.style = style;
this.symbols = symbols;
|
Methods Summary |
---|
protected java.lang.String | format(java.util.Calendar calendar, NumberFormat nf)Formats date/time with the current style .
StringBuffer appendTo = new StringBuffer();
StringBuffer pattern = new StringBuffer(symbols.patterns[style]);
nf.setGroupingUsed(false);
char c;
int value;
int digits;
if (style == Formatter.DATETIME_SHORT) {
// date first paremeter, time second
pattern = new StringBuffer(MessageFormat.format(
symbols.patterns[style],
new String[] {symbols.patterns[Formatter.DATE_SHORT],
symbols.patterns[Formatter.TIME_SHORT]}));
} else if (style == Formatter.DATETIME_LONG) {
pattern = new StringBuffer(MessageFormat.format(
symbols.patterns[style],
new String[] {symbols.patterns[Formatter.DATE_LONG],
symbols.patterns[Formatter.TIME_LONG]}));
}
for (int i = 0; i < pattern.length(); i++) {
c = pattern.charAt(i);
switch (c) {
case 'a":
// AM or PM symbol
int ampm = calendar.get(Calendar.AM_PM);
appendTo.append(symbols.ampms[ampm == Calendar.AM ? 0 : 1]);
break;
case 'H":
// Hours in 24-hour mode 0 - based
value = calendar.get(Calendar.HOUR_OF_DAY);
digits = ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'H") ? 2 : 1;
nf.setMinimumIntegerDigits(digits);
appendTo.append(nf.format(value));
i += digits - 1;
break;
case 'h":
// Hours in 12-hour mode, 1 - based
value = calendar.get(Calendar.HOUR);
digits = ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'h") ? 2 : 1;
nf.setMinimumIntegerDigits(digits);
appendTo.append(nf.format(value));
i += digits - 1;
break;
case 'K":
// Hours in 12-hour mode 0 - based
value = calendar.get(Calendar.HOUR_OF_DAY);
value = value>12 ? value % 12 : value; // noon is 12 midnight is 0
digits = ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'K") ? 2 : 1;
nf.setMinimumIntegerDigits(digits);
appendTo.append(nf.format(value));
i += digits - 1;
break;
case 'k":
// Hours in 24-hour mode 1 based
value = calendar.get(Calendar.HOUR_OF_DAY);
value = (value == 0) ? 24 : value;
digits = ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'k") ? 2 : 1;
nf.setMinimumIntegerDigits(digits);
appendTo.append(nf.format(value));
i += digits - 1;
break;
case 'm":
// Minutes
value = calendar.get(Calendar.MINUTE);
digits = ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'm") ? 2 : 1;
nf.setMinimumIntegerDigits(digits);
appendTo.append(nf.format(value));
i += digits - 1;
break;
case 's":
// Seconds
value = calendar.get(Calendar.SECOND);
digits = ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 's") ? 2 : 1;
nf.setMinimumIntegerDigits(digits);
appendTo.append(nf.format(value));
i += digits - 1;
break;
case 'E":
// Long or short weekday name
if ((i + 3) < pattern.length() &&
pattern.charAt(i + 1) == 'E" &&
pattern.charAt(i + 2) == 'E" &&
pattern.charAt(i + 3) == 'E") {
// long day of week name
value = calendar.get(Calendar.DAY_OF_WEEK);
appendTo.append(symbols.weekDays[value]);
i += 3;
} else if ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'E") {
// short day of week name
value = calendar.get(Calendar.DAY_OF_WEEK);
appendTo.append(symbols.shortWeekDays[value]);
i += 1;
}
break;
case 'd":
// Numeric day of month
if ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'd") {
// numeric day 2 digits
value = calendar.get(Calendar.DAY_OF_MONTH);
nf.setMinimumIntegerDigits(2);
appendTo.append(nf.format(value));
i += 1;
} else {
// numeric day 1 digit
value = calendar.get(Calendar.DAY_OF_MONTH);
nf.setMinimumIntegerDigits(1);
appendTo.append(nf.format(value));
}
break;
case 'M":
// Long or short month name or numeric month
if ((i + 3) < pattern.length() &&
pattern.charAt(i + 1) == 'M" &&
pattern.charAt(i + 2) == 'M" &&
pattern.charAt(i + 3) == 'M") {
// long month name
value = calendar.get(Calendar.MONTH);
appendTo.append(symbols.months[value]);
i += 3;
} else if ((i + 2) < pattern.length() &&
pattern.charAt(i + 1) == 'M" &&
pattern.charAt(i + 2) == 'M") {
// short month name
value = calendar.get(Calendar.MONTH);
appendTo.append(symbols.shortMonths[value]);
i += 2;
} else if ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'M") {
value = calendar.get(Calendar.MONTH) + 1;
nf.setMinimumIntegerDigits(2);
appendTo.append(nf.format(value));
i += 1;
} else {
value = calendar.get(Calendar.MONTH) + 1;
nf.setMinimumIntegerDigits(1);
appendTo.append(nf.format(value));
}
break;
case 'y":
// Long or short year
value = calendar.get(Calendar.YEAR);
if ((i + 3) < pattern.length() &&
pattern.charAt(i + 1) == 'y" &&
pattern.charAt(i + 2) == 'y" &&
pattern.charAt(i + 3) == 'y") {
// long year
nf.setMinimumIntegerDigits(4);
appendTo.append(nf.format(value));
i += 3;
} else if ((i + 1) < pattern.length() &&
pattern.charAt(i + 1) == 'y") {
// short year
nf.setMinimumIntegerDigits(2);
String y = nf.format(value);
appendTo.append(y.substring(y.length() - 2,
y.length()));
i += 1;
}
break;
/*
* case 'G': // not in midp
* value = calendar.get(Calendar.ERA);
* appendTo.append(symbols.eras[value]);
* break;
*/
case 'z":
// Time zone
TimeZone tzone = calendar.getTimeZone();
if (tzone == null) {
break;
}
int offset = tzone.getRawOffset();
if (offset == 0) {
appendTo.append('Z");
} else {
offset /= 3600000;
if (offset >= 0) {
appendTo.append('+");
} else {
appendTo.append('-");
offset = -offset;
}
if (offset < 10) {
appendTo.append('0");
}
appendTo.append(offset);
appendTo.append(":00");
}
break;
default:
appendTo.append(c);
}
}
// for
return appendTo.toString();
|
|