Convert 24-hour work time to am / pm in Joda-Time

I just started working with Joda-Time and got the correct display of my date in 24-hour format ("wartime"), but I would rather be am / pm. He looked, and he mentioned hourOfDay , which, as I understand it, was an HH value, so I tried to write a loop that would break it into AM / Pm, but that did not work.

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); } } 
+6
source share
1 answer

See the DateTimeFormat API documentation. This should do what you want:

 DateTimeFormatter builder = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm:ss.SSa"); 

There is no need to complicate with different cases.

+19
source

Source: https://habr.com/ru/post/954872/


All Articles