Convert 24 Hour Time in to 12 Hour Time in Java

后端 未结 2 892
心在旅途
心在旅途 2021-01-22 13:30

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

相关标签:
2条回答
  • 2021-01-22 13:46

    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 );
    
    0 讨论(0)
  • 2021-01-22 14:06

    Try this:

    String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm a").format(new Date());
    
    0 讨论(0)
提交回复
热议问题