日期转化器的主要代码:
但是出现了错误,找不出错在哪里啊!
求解答啊
public class MainActivity extends Activity {
int year,month,day;
DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker =findViewById(R.id.datepicker);
Calendar calendar= Calendar.getInstance();//日期对象
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);//匿名内部类
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
MainActivity.this.year=year;
MainActivity.this.month=monthOfYear;
MainActivity.this.day=dayOfMonth;
show(year,monthOfYear,dayOfMonth);
}
});
}
private void show(int year,int month,int day){
String str=year+"年"+(month+1)+"月"+day+"日";
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
}
在XML文件中,使用DatePicker组件,都是选择matchparent。
month是从0开始的 。在Java中用Activity