Skip to content Skip to sidebar Skip to footer

How Can I Change Date From 24-hours Format To 12-hours Format (am/pm) In Android

Calendar ci = Calendar.getInstance(); CiDateTime = '' + (ci.get(Calendar.MONTH) + 1) + '/' + ci.get(Calendar.DAY_OF_MONTH) + '/' + ci.get(Calendar.YEAR); String visitd

Solution 1:

Did you try this ?

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

Solution 2:

Below function may be useful to you. This will convert time from 24 hours to 12 hours

public static String Convert24to12(String time)
{
    String convertedTime ="";
    try {
        SimpleDateFormat displayFormat = new SimpleDateFormat("hh:mm a");
        SimpleDateFormat parseFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = parseFormat.parse(time);        
        convertedTime=displayFormat.format(date);
        System.out.println("convertedTime : "+convertedTime);
    } catch (final ParseException e) {
        e.printStackTrace();
    }
    return convertedTime;
    //Output will be 10:23 PM
}

Solution 3:

public static final String TIME_FORMAT = "hh:mm aa";
SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT);

Calendar ATime = Calendar.getInstance();
String Timein12hourFormat = TimeFormat.format(ATime.getTime());

Solution 4:

try with this answer this is shortest and best answer on stack.

 Calendar c = Calendar.getInstance();
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
 Datetime = sdf.format(c.getTime());
 System.out.println("============="+Datetime);

Result:-=========2015-11-20 05:52:25 PM


Solution 5:

Here is the answer to get a data in US-English Format date/time using a 12 hour clock.

DateTime.Now.ToString("MM/d/yyyy hh:mm:ss tt");
01/16/2014 03:53:57 PM

Post a Comment for "How Can I Change Date From 24-hours Format To 12-hours Format (am/pm) In Android"