当datepicker选择值输入时,我将3914-01-06作为输出 是2014-01-06
int day = view.getDayOfMonth();
int month= view.getMonth();
int year = view.getYear();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = sdf.format(new Date(year, month, day));
Log.d("test", "" + formatedDate);//Here i'am retrieving 3914 instead 2014
答案 0 :(得分:2)
根据the documentation,Date()
的第一个参数是"年份减去1900"。这就是为什么你得到3914(= 2014 + 1900)。正如文档中所述,使用Calendar
或GregorianCalendar
代替Date
。
答案 1 :(得分:0)
你最好使用日历,这是一个简单的例子:
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
int [] tabd=new int[3];
tabd[0]=day;
tabd[1]=month+1;
tabd[2]=year;
Button b1=(Button)getActivity().findViewById(R.id.yourButton);
String d=tabd[0]+"-"+tabd[1]+"-"+tabd[2];
// d is your string that contain the date now you can do whatever you want with it
}