日历选择器上面默认的时间不是当前手机系统的时间。
日历选择器上默认的时间不是手机系统的时间, 是随机的一个时间,是正常的吗?看老师的是手机默认时间啊
代码:
package com.example.hys.datatimepick;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private TimePicker tp;
private DatePicker dp;
private Calendar cal;
private int year;
private int month;
private int day;
private int hour;
private int minute;
private int second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取日历的对像
cal = Calendar.getInstance();
//获取时间
year = 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);
second = cal.get(Calendar.SECOND);
setTitle(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
dp = (DatePicker) findViewById(R.id.datePicker);
tp = (TimePicker) findViewById(R.id.timePicker);
//初始化datepicker
dp.init(year, cal.get(Calendar.MINUTE), day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
});
tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
setTitle(hourOfDay+":"+minute);
}
});
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
}
},year, cal.get(Calendar.MINUTE),day).show();
}
}