Convert time value to format “hh:mm Am/Pm” using Android

前端 未结 10 1963
粉色の甜心
粉色の甜心 2020-11-28 13:10

I am getting date value from database like \"2013-02-27 06:06:30\" using StringTokenizer I will get time separately like below

String startTime = \"2013-02         


        
10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 13:55

    Selected answer had an issue of showing wrong time. If your time is 12:30:00 it shows 12:30 AM instead 12:30 PM. The below code will help to overcome the issue.

    SimpleDateFormat sdf = new SimpleDateFormat("KK:mm:ss");
    SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
    
        Date dt1 = null;
    
        try {
            dt1 = sdf.parse("12:00:00");
            Log.i("Time in Am Pm  ", sdfs.format(dt1));
    
        } catch (ParseException e) {
            e.printStackTrace();
        }
    

提交回复
热议问题