为什么我的程序每次点击进去日期都是显示1900年,然后点击一下才会获取正确的日期
package com.immoc.test9;
import java.util.Calendar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.os.Build;
import android.os.storage.OnObbStateChangeListener;
public class MainActivity extends Activity {
private TimePicker timePicker;
private DatePicker datePicker;
private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取日历的一个对象
cal = Calendar.getInstance();
//获取年月日时分秒的信息
cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DAY_OF_MONTH);
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
setTitle(year + "-" + month + "-" + day + "-" + hour + "-" + minute);
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker);
//datepicker初始化
datePicker.init(year, cal.get(Calendar.MONTH), day, new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
setTitle(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
}
});
}
}