How do I get today’s date/time using TimePicker?… here is a solution to the problem.
How do I get today’s date/time using TimePicker?
I have a TimePicker
to get the time. I’m writing Java code for an Android application:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Log.e("Alarm", "" + calendar.getTime());
calendar.set(Calendar.HOUR, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());
The second process is one day ahead of the first.
I tried the following code and subtracted a day.
calendar.add(Calendar.DATE, -1);
Is this the only way or the best? How do I get today’s date/time using TimePicker
?
Solution
Check the code below….. Maybe you can get help from this
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(R.id.tvTime);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
set current time into textview
tvDisplayTime.setText(
new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}