我有一个应用程序从数据库中检索以日期时间格式格式化的字符串,我想在某些情况下只显示时间,我将如何完成此操作?
这是我到目前为止所尝试的但是我得到了一个Unparsable错误
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
private static final String TIME_FORMAT = "kk:mm";
// ...
SimpleDateFormat TimeFormat = new SimpleDateFormat(TIME_FORMAT);
Date startTimer = null;
Date endTimer = null;
try
{
String dateString = cursor.getString(6);
startTimer = TimeFormat.parse(dateString);
//Code for End Time
String endDateString = cursor.getString(7);
endTimer = TimeFormat.parse(endDateString);
totalBillable += endTimer.getTime() - startTimer.getTime();
}
catch (ParseException e)
{
Log.e("ReminderEditActivity", e.getMessage(), e);
}
答案 0 :(得分:0)
构造一种表示您想要的格式,并在您只需要时间时使用该格式:
DateFormat df = new SimpleDateFormat("HH:mm:ss");
String onlyTime = df.format(new Date());
答案 1 :(得分:0)
我猜你得到的是Unparsable error
,因为你正在解析一个格式不同的字符串日期。试试这个:
String dateString = cursor.getString(6);
startTimer = TimeFormat.parse(dateString.substring(11, 16));
String endDateString = cursor.getString(7);
endTimer = TimeFormat.parse(endDateString.substring(11, 16));
希望通过此substring()
方法,您将获得kk:mm
日期格式。