I've a TimePicker
to get time. And I've following 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 gives a day ahead than first process.
I've tried following code and subtracted the one day.
calendar.add(Calendar.DATE, -1);
Is this the only way or best way? How to get the today's date / time using the TimePicker
?
Check the following code.....may be you 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);
}
OK, I got the solution. Thank you everyone! :)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());
You should add: calendar.getInstance();
instead calendar.add(Calendar.DATE, -1);
My code:
Date today = new Date();
final Calendar c = Calendar.getInstance();
preventa.setText(VarGlobales.formatofecha(today));
时间选择器给出GMT时间,您还应该考虑TimeZone偏移。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.