2017-03-17 54 views

回答

1

使用的SimpleDateFormat轉換從一個時間/日期格式到另一種。

Log.v("12HRS Time", getFormatedDateTime("23:30:00", "HH:mm:ss", "hh:mm a")) 

public static String getFormatedDateTime(String dateStr, String strReadFormat, String strWriteFormat) { 

    String formattedDate = dateStr; 

    DateFormat readFormat = new SimpleDateFormat(strReadFormat, Locale.getDefault()); 
    DateFormat writeFormat = new SimpleDateFormat(strWriteFormat, Locale.getDefault()); 

    Date date = null; 

    try { 
     date = readFormat.parse(dateStr); 
    } catch (ParseException e) { 
    } 

    if (date != null) { 
     formattedDate = writeFormat.format(date); 
    } 

    return formattedDate; 
} 

對於的SimpleDateFormat參考: - https://developer.android.com/reference/java/text/SimpleDateFormat.html

+0

謝謝......對我工作得很好。 – Chaudhary

3

最簡單的方法,通過使用日期模式得到它 - H:毫米,其中

h - Hour in am/pm (1-12) 
m - Minute in hour 
a - Am/pm marker 

代碼片段:

DateFormat dateFormat = new SimpleDateFormat("hh:mm a"); 

的詳細信息,請參閱本link

1

嘗試SimpleDateFormat

示例

Date dateToFormat = new Date(someDate); 
SimpleDateFormat dateFormatExpression = new SimpleDateFormat("hh:mm a"); 
String formattedDate = dateFormatExpression.format(dateToFormat); 
0

試試這個。

DateFormat dateFormat = new SimpleDateFormat("hh:mm a"); 
0

只需使用SimpleDateFormat

這樣.....

public String GetTimeWithAMPMFromTime(String dt) { 
     try { 
      SimpleDateFormat inFormat = new SimpleDateFormat("HH:mm:ss"); 
      Date date = inFormat.parse(dt); 
      SimpleDateFormat outFormat = new SimpleDateFormat("hh:mm a"); 
      String goal = outFormat.format(date); 
      return goal; 
     } catch (ParseException e) { 
      e.printStackTrace(); 
      return ""; 
     } 
    } 

調用方法...

String YOUR_TIME = GetTimeWithAMPMFromTime(WEB_SERVICE_TIME); 
0
android.text.format.DateFormat myDate = new android.text.format.DateFormat(); 
myDate.format("hh:mm a", new java.util.Date()); 

DateFormat

0
public static void convert(String time) throws Exception { 
     try {  
      SimpleDateFormat t24 = new SimpleDateFormat("HH:mm"); 
      SimpleDateFormat t12 = new SimpleDateFormat("hh:mm a"); 
      Date date = t24.parse(time); 
      System.out.println(t12.format(date)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    }