从Web服务接收时间为23:30:00。我想将它转换为12小时的格式。我希望输出像下午11:30(转换23:30:00之后)。如何将HH:mm:ss转换为hh:mm上午/下午在Android
-1
A
回答
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
示例
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());
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();
}
}
相关问题
- 1. Android将DateTime从上午/下午转换为24小时
- 2. 将上午/下午时间转换为时期
- 3. 上午/下午将24位整数转换为12小时
- 4. 将24小时转换为上午/下午格式
- 5. 将日期时间转换为上午/下午时间
- 6. 将整数(0,1,2 ...,23)转换为小时(上午/下午)
- 7. 将分钟自午夜转换为上午/下午时间的函数
- 8. 转换字符串在12(下午/上午)小时上午下午时间到24小时时间android
- 9. 如何将时间范围转换为下午或上午时间?
- 10. Android的报警在上午和下午
- 11. DateTime上午/下午格式转换为24h格式
- 12. 存储时间值字符串hh:mm上午/下午
- 13. 在Joda-Time转换24小时时间到上午/下午
- 14. Timepicker上午/下午返回错误android
- 15. 如何测试上午或下午?
- 16. 将日期转换为字符串缺失上午/下午的指示符
- 17. UILocalNotification setTimeZone上午/下午
- 18. TimePickerDialog和上午或下午
- 19. 上午/下午到TimeSpan
- 20. 上午/下午设置为UIDatePicker错误
- 21. Android:我如何将上午和下午格式的时间转换为整数秒
- 22. 转换时间:分钟上午/下午到unix时间戳
- 23. 上午和下午帮助在java
- 24. 获取上午/下午在gmt时区
- 25. 添加上午/下午到datepicker?
- 26. 计算上午到下午的时间
- 27. 提取上午或下午jQuery中
- 28. 上午下午没有插入MySQL表
- 29. 输出日期到上午/下午
- 30. javascript日期添加上午下午
// am if(hours> 12){ hours = hours-12; // pm } – mayosk