2017-03-17 49 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(); 
     } 
    }