I’d like to make a app who changes me the airplane mode. I have a timepicker and a calendar. Now I want to set the time from the tp into the cal. I have made 2 toast to control the time from both objects. The toast from the tp shows me the right time (f. e. 13:04) but the calendar object has after the “setting” the wrong time (11:12)
I’ve read that the time is setted just if methodeds like getTimeinMillis() are called so added this method but it isn’t still working . Thanks for your answers
Button b;
Button b2;
TimePicker tp;
Calendar cal;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button)(findViewById(R.id.button1));
tp = (TimePicker)(findViewById(R.id.timePicker1));
cal = Calendar.getInstance();
b2 = (Button)(findViewById(R.id.button2));
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),"TimePicker "+ tp.getCurrentHour()+":"+tp.getCurrentMinute(), Toast.LENGTH_LONG).show();
cal.set(Calendar.HOUR_OF_DAY,tp.getCurrentHour());
cal.set(Calendar.MINUTE,tp.getCurrentMinute());
cal.set(Calendar.SECOND,0);
cal.getTimeInMillis();
Toast.makeText(getApplicationContext(),"Calendar "+ cal.HOUR_OF_DAY+":"+cal.MINUTE, Toast.LENGTH_LONG).show();
}
});
You are setting the fields correctly, but you don’t query the fields of a calendar like that. What you are doing there is printing the int value of the HOUR_OF_DAY field on the calendar class which has the value 11 (See here).
The method getTimeinMillis will just return the number of miliseconds since Epoch (Jan 1, 1970 00:00:00 GMT). There’s no need for it to be there.
use: