如何将时间添加到我使用 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);
}
上面是我试过的,但是没有用。
最佳答案
要让时间也显示在您的字段中,您可以将它添加到您的SimpleDateFormat
:
public static String getDateAsString(final Date date) {
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
return format.format(date);
}
关于java - 如何将时间添加到我的日期对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596031/