Convert 24 Hour Time in to 12 Hour Time in Java

馋奶兔 提交于 2019-12-20 03:56:09

问题


I want to convert 24 Hour time into 12 Hour AM/PM time. e.g:- How to convert date 2012-03-20T22:30:00.000+05:30 to to 2012-03-20 10:30 PM??

I have extratced the date but its in 24 Hour time. I though used SimpleDateFormater but still for 12.30 it is showing 00.30 and not 12.30 PM.

Thanks in advance.


回答1:


The bundled java.util.Date and .Calendar classes are notoriously troublesome. Avoid them. Use either Joda-Time or the new java.time package in Java 8.

Joda-Time

DateTimeZone timeZone = DateTimeZone.ForID( "Asia/Kolkata" );
DateTime dateTime = new DateTime( "2012-03-20T22:30:00.000+05:30", timeZone );
Java.util.Locale locale = java.util.Locale.Builder().setLanguage("en").setRegion("IN").build();
DateTimeFormatter formatter = DateTimeFormat.forStyle( "SS" ).withLocale( locale).withZone( timeZone );
String output = formatter.print( dateTime );



回答2:


Try this:

String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm a").format(new Date());


来源:https://stackoverflow.com/questions/9785072/convert-24-hour-time-in-to-12-hour-time-in-java

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