DateFormat 产生错误的年份
DateFormat produce wrong year
上图是日期选择器,我点击确定后,结果如下图
代码如下
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
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
Date date = new Date(year, month, day);
Fragment fragment = getActivity().getFragmentManager().findFragmentByTag(ConvocationAddDialogFragment.class.getName());
TextView textView = (TextView) fragment.getView().findViewById(R.id.open_registration_date);
textView.setText(DateFormat.getDateInstance().format(date));
}
}
预期值为2016年1月18日,为什么显示3916年1月18日?
根据源代码new Date(year, month, day);
public Date(int year, int month, int day) {
GregorianCalendar cal = new GregorianCalendar(false);
cal.set(1900 + year, month, day);
milliseconds = cal.getTimeInMillis();
}
在这里,您的 2016 年将添加到 1900 中,结果为 3916。
试试,
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Date date = calendar.getTime();
这样使用
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
tv_date= (TextView)getActivity().findViewById(R.id.tv_date);
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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMinDate(c.getTimeInMillis());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
tv_date.setText(new StringBuilder().append(day).append("-")
.append(month+1).append("-").append(year));
date=tv_date.getText().toString();
}
}
上图是日期选择器,我点击确定后,结果如下图
代码如下
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
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
Date date = new Date(year, month, day);
Fragment fragment = getActivity().getFragmentManager().findFragmentByTag(ConvocationAddDialogFragment.class.getName());
TextView textView = (TextView) fragment.getView().findViewById(R.id.open_registration_date);
textView.setText(DateFormat.getDateInstance().format(date));
}
}
预期值为2016年1月18日,为什么显示3916年1月18日?
根据源代码new Date(year, month, day);
public Date(int year, int month, int day) {
GregorianCalendar cal = new GregorianCalendar(false);
cal.set(1900 + year, month, day);
milliseconds = cal.getTimeInMillis();
}
在这里,您的 2016 年将添加到 1900 中,结果为 3916。
试试,
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Date date = calendar.getTime();
这样使用
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
tv_date= (TextView)getActivity().findViewById(R.id.tv_date);
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);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMinDate(c.getTimeInMillis());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
tv_date.setText(new StringBuilder().append(day).append("-")
.append(month+1).append("-").append(year));
date=tv_date.getText().toString();
}
}