Long newTimeMsecValue = Calendar.getInstance().getTime().getTime();
timePicker.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
//NOTE: hourOfDay is always in 24h mode, not dependent of AM/PM mode!
newTimeMsecValue = new Long (((hourOfDay * 3600) + (minute * 60)) * 1000); //
}});
Calendar myCalend = Calendar.getInstance();
myCalend.setTimeInMillis(newTimeMsecValue);// value from the code above
Date calDate = myCalend.getTime();
DateFormat df = new SimpleDateFormat("HH:mm");
String humanDate = df.format(calDate);
2条答案
按热度按时间qvtsj1bj1#
1.首先,使用事件侦听器检索当前时间(以毫秒为单位):
然后,稍后,您将以所需的格式检索时间。例如,小时:分钟(“HH:mm”):
现在:humanDate保持如下值:十点二十一分
e5nqia272#
您可以使用下面提到的方法从时间选择器中获取小时,然后可以按以下方式对其进行格式化:
getHour()
getMinutes()