我将 aChoiceFormat
用于自定义输出,aDateFormat
用于其余部分。这可以使用MessageFormat
.
final String format = "{0,choice,0#midnight|0<am|12#noon|12<pm} {1,date,dd MMM YYYY HH:mm:ss}";
final MessageFormat messageFormat = new MessageFormat(format);
例子:
public static void main(String args[]) {
final Calendar calendar = Calendar.getInstance();
final String format = "{0,choice,0#midnight|0<am|12#noon|12<pm} {1,date,dd MMM YYYY HH:mm:ss}";
final MessageFormat messageFormat = new MessageFormat(format);
System.out.println(messageFormat.format(new Object[]{calendar.get(Calendar.HOUR_OF_DAY), calendar.getTime()}));
System.out.println("==== TESTS ====");
calendar.set(Calendar.HOUR_OF_DAY, 0);
System.out.println(messageFormat.format(new Object[]{calendar.get(Calendar.HOUR_OF_DAY), calendar.getTime()}));
calendar.set(Calendar.HOUR_OF_DAY, 1);
System.out.println(messageFormat.format(new Object[]{calendar.get(Calendar.HOUR_OF_DAY), calendar.getTime()}));
calendar.set(Calendar.HOUR_OF_DAY, 11);
System.out.println(messageFormat.format(new Object[]{calendar.get(Calendar.HOUR_OF_DAY), calendar.getTime()}));
calendar.set(Calendar.HOUR_OF_DAY, 12);
System.out.println(messageFormat.format(new Object[]{calendar.get(Calendar.HOUR_OF_DAY), calendar.getTime()}));
calendar.set(Calendar.HOUR_OF_DAY, 14);
System.out.println(messageFormat.format(new Object[]{calendar.get(Calendar.HOUR_OF_DAY), calendar.getTime()}));
}
输出:
am 08 Nov 2013 08:53:58
==== TESTS ====
midnight 08 Nov 2013 00:53:58
am 08 Nov 2013 01:53:58
am 08 Nov 2013 11:53:58
noon 08 Nov 2013 12:53:58
pm 08 Nov 2013 14:53:58