Pregunta

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.

Snapshot

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?

¿Fue útil?

Solución 2

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());

Otros consejos

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); 
}

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));

Time Picker gives the GMT time, you should also take TimeZone offset into account.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top