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

前端 未结 10 1915
粉色の甜心
粉色の甜心 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();
        }
    
    0 讨论(0)
  • 2020-11-28 13:57

    Try this..

    Date dt = new Date(date1);
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
    String time1 = sdf.format(dt);
    
    0 讨论(0)
  • 2020-11-28 13:57

    Try this

     String time = "22:35";
    
    try {
         SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
         Date dateObj = sdf.parse(time);
        System.out.println(dateObj);
        System.out.println(new SimpleDateFormat("K:mm").format(dateObj));
    } catch (final ParseException e) {
        e.printStackTrace();
    }
    

    Trace out this link http://developer.android.com/reference/java/text/SimpleDateFormat.html

    0 讨论(0)
  • 2020-11-28 13:59

    Just use the following pattern you will get your expected result:

        try {
            String time = "06:06:30";
            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
            Date dt = sdf.parse(time);
    
            SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
            String formatedTime = sdfs.format(dt);
    
            Log.v("parseTime", formatedTime);
    
        } catch (ParseException e) {
            e.printStackTrace();
        }
    

    Happy coding :)

    0 讨论(0)
提交回复
热议问题