4 回答

TA貢獻1828條經驗 獲得超3個贊
像這樣的東西
DateFormat sdf = new SimpleDateFormat("hh:mm"); Date date = sdf.parse(time);

TA貢獻2011條經驗 獲得超2個贊
而不是使用Date和/或SimpleDateFormat類,也許可以考慮LocalTime
String time = "08:00";
LocalTime lt = LocalTime.parse(time);
System.out.println(lt);
輸出:
08:00
并且可以輕松地與其他時間進行比較LocalTime::isBefore()或LocalTime::isAfter()

TA貢獻1827條經驗 獲得超9個贊
試試下面的代碼,
String time = "08.00";
try {
DateFormat sdfInput = new SimpleDateFormat("hh.mm");
Date date = sdfInput.parse(time);
DateFormat sdfOutput = new SimpleDateFormat("hh:mm");
Log.e( "Time: ", sdfOutput.format(date));
} catch (Exception e) {
e.printStackTrace();
}
輸出 -> 時間:08:00

TA貢獻1802條經驗 獲得超10個贊
我有這樣的時間格式 hhmmss="151918" 所以你可以根據你當前的時間格式使用任何格式而不是 hhmmss,比如“hh.mm”或 hh:mm:ss 等,你可以在任何你需要的地方調用這個方法。
fun convertTimeFormat(time:String):String{
var formattedTime=""
try {
val inputFormat: DateFormat = SimpleDateFormat("hhmmss")
val timeObj = inputFormat.parse(time)
Log.d("timeObj",""+timeObj)
formattedTime=SimpleDateFormat("hh:mm aa").format(timeObj)
} catch (e: ParseException) {
e.printStackTrace()
}
return formattedTime
}
添加回答
舉報