Converting 24-hour clock time to am/pm in Joda-Time

ぐ巨炮叔叔 提交于 2019-12-30 08:40:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!