编程语言
首页 > 编程语言> > 在Java中将24小时制转换为12小时制

在Java中将24小时制转换为12小时制

作者:互联网

我想将24小时制转换为12小时制.
例如:-如何将日期2012-03-20T22:30:00.000 05:30转换为2012-03-20 10:30 PM?

我已经引述了日期,但在24小时内.我虽然使用了SimpleDateFormater,但仍在12.30上显示00.30,而不是12.30 PM.

提前致谢.

解决方法:

捆绑的java.util.Date和.Calendar类非常麻烦.避免他们.在Java 8中使用Joda-Time或新的java.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 );

标签:java,date,datetime-conversion
来源: https://codeday.me/bug/20191011/1890209.html