如何将时间解析为 android 中的字符串值?

How to parse time to String value in android?

我有来自服务器的时间值作为 13:00:45:888 返回,它作为 TIME 值从 SAP 后端返回。如何将其转换为 01:00 PM

希望这可能有所帮助

String str = "08:03:10 pm";
 DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
 Date date = (Date)formatter.parse(str);

try {

    SimpleDateFormat format = new SimpleDateFormat("hh:mm a"); //if 24 hour format
    // or
    SimpleDateFormat format = new SimpleDateFormat("HH:mm"); // 12 hour format

    java.util.Date d1 =(java.util.Date)format.parse(your_Time);

    java.sql.Time ppstime = new java.sql.Time(d1.getTime());

} catch(Exception e) {

    Log.e("Exception is ", e.toString());
}
You can try the code below.

SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm a");
            SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss:SSS");
            Date serverTime = sdf2.parse(ValueFromServer);
            ResultYouWant = sdf1.format(serverTime);