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

后端 未结 7 632
Happy的楠姐
Happy的楠姐 2021-01-14 16:56
Calendar ci = Calendar.getInstance();

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


        
7条回答
  •  暖寄归人
    2021-01-14 17:18

    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
    }
    

提交回复
热议问题