在Java中将24小时时间转换为12小时 [英] Convert 24 Hour Time in to 12 Hour Time in Java

查看:507
本文介绍了在Java中将24小时时间转换为12小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将24小时的时间转换为上午/下午12点的时间。
例如: - 如何转换日期2012-03-20T22:30:00.000 + 05:30至2012-03-20 10:30 PM ??

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??

我已经将日期缩小,但是在24小时内。我虽然使用了SimpleDateFormater,但仍然是 12.30它显示00.30而不是12.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.

提前感谢。

推荐答案

捆绑的java.util.Date和.Calendar类是非常麻烦的。避免他们在Java 8中使用Joda-Time或新的java.time包。

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.

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中将24小时时间转换为12小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆