【发布时间】:2017-08-08 19:29:43
【问题描述】:
来自网络服务,接收时间为 23:30:00。我想将其转换为 12 小时的格式。我想输出类似晚上 11:30 的内容(转换为 23:30:00 之后)。
【问题讨论】:
-
//am if (hours >12) { hours = hours - 12; //下午 }
来自网络服务,接收时间为 23:30:00。我想将其转换为 12 小时的格式。我想输出类似晚上 11:30 的内容(转换为 23:30:00 之后)。
【问题讨论】:
使用 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
【讨论】:
使用日期模式获取它的最简单方法 - h:mm a, where
h - Hour in am/pm (1-12)
m - Minute in hour
a - Am/pm marker
代码 sn-p:
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
更多信息请看link
【讨论】:
例子
Date dateToFormat = new Date(someDate);
SimpleDateFormat dateFormatExpression = new SimpleDateFormat("hh:mm a");
String formattedDate = dateFormatExpression.format(dateToFormat);
【讨论】:
只需使用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);
【讨论】:
我就这样得到了答案,我已经测试过了(y)
startTime = "2013-02-27 21:06:30";
StringTokenizer tk = new StringTokenizer(startTime);
String date = tk.nextToken();
String time = tk.nextToken();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt;
try {
dt = sdf.parse(time);
System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
【讨论】:
试试这个。
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
【讨论】:
android.text.format.DateFormat myDate = new android.text.format.DateFormat();
myDate.format("hh:mm a", new java.util.Date());
【讨论】:
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();
}
}
【讨论】:
一行代码
String time = null;
try{
time = new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("HH:mm:ss").parse("your_time");
}catch{
}
在your_time 中,您必须提供要转换的输入。
【讨论】:
SimpleDateFormat 类作为第一选择是不明智的建议。今天我们在java.time, the modern Java date and time API 和它的DateTimeFormatter 中做得更好。是的,您可以在 Android 上使用它。对于较旧的 Android,请参阅 How to use ThreeTenABP in Android Project。