How can I change date from 24-hours format to 12-hours format (am/pm) in android

user1061793 picture user1061793 · Mar 2, 2012 · Viewed 22.1k times · Source
Calendar ci = Calendar.getInstance();

CiDateTime = "" + (ci.get(Calendar.MONTH) + 1) +  
    "/" + ci.get(Calendar.DAY_OF_MONTH) + 
    "/" + ci.get(Calendar.YEAR);

String visitdatetime = CiDateTime + "" + 
          ci.get(Calendar.HOUR_OF_DAY) + ":" + 
          ci.get(Calendar.MINUTE) + ":" + 
          ci.get(Calendar.SECOND);

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy  hh:mm  a");

I am getting from the server in 24 hours format.but want to show my date like MM/dd/yyyy hh:mm AM/PM like 12 hrs format.

Answer

Joand picture Joand · Mar 2, 2012

Did you try this ?

SimpleDateFormat dateFromat= new SimpleDateFormat("MM/dd/yyyy  hh:mm  aa");
Date today = new Date();
String todayStr = dateFromat.format(today);