如何为我的日期对象添加时间?

时间:2013-05-16 19:39:49

标签: java android

如何使用DatePicker为我的日期对象添加时间。默认情况下,日期选择器设置为00:00:00到我的日期对象的时间,我需要添加当前系统到我的约会对象的时间。

Calendar now = Calendar.getInstance();
int hours = now.get(Calendar.HOUR_OF_DAY);
int minutes = now.get(Calendar.MINUTE);
int seconds = now.get(Calendar.SECOND);
//int hours = new Time(System.currentTimeMillis()).getHours();
//int minutes = new Time(System.currentTimeMillis()).getMinutes();
//int seconds = new Time(System.currentTimeMillis()).getSeconds();
now.set(year, month, day, hours, minutes, seconds);
textDateField.setText(getDateAsString(now.getTime()));

我正在使用日期选择器并单击设置按钮,我会在字段中显示日期,但日期对象没有时间。如何用我的日期对象追加时间。

public static String getDateAsString(Date date) {
    SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
    return format.format(date);
}

以上是我试过的,但它没有用。

1 个答案:

答案 0 :(得分:2)

要在您的字段中显示时间,您只需将其添加到SimpleDateFormat

public static String getDateAsString(final Date date) {
    SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
    return format.format(date);
}