问题
I just started working with Joda-Time, and got it to correctly display my date in 24-hour clock ("military time") but I would rather it be am/pm. Looked it up and it mentioned hourOfDay which I figured was the HH value so I tried to write a loop that would break it down into AM/Pm but it never worked out.
DateTime dtf = new DateTime(wikiParsedDate);
if (hourOfDay == 00) {
hourOfDay == 12;
DateTimeFormatter builder = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SS'AM" );
return builder.print(dtf);
} else if (0 < hourOfDay && hourOfDay < 12) {
DateTimeFormatter builder = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SS'AM" );
return builder.print(dtf);
} else if (hourOfDay > 12) {
hourOfDay - 12 == hourOfDay;
DateTimeFormatter builder = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss.SS'PM" );
return builder.print(dtf);
}
}
回答1:
Look at the API documentation of DateTimeFormat. This should do what you want:
DateTimeFormatter builder = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm:ss.SSa");
No need for the complication with different cases.
来源:https://stackoverflow.com/questions/19075188/converting-24-hour-clock-time-to-am-pm-in-joda-time