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

后端 未结 7 626
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:12

    use below format to 12 hour with am/pm

    MM/dd/yyyy HH:mm:ss a

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2021-01-14 17:19

    Did you try this ?

    SimpleDateFormat dateFromat= new SimpleDateFormat("MM/dd/yyyy  hh:mm  aa");
    Date today = new Date();
    String todayStr = dateFromat.format(today);
    
    0 讨论(0)
  • 2021-01-14 17:23

    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

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

    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
    
    0 讨论(0)
提交回复
热议问题