【问题标题】:String to datepicker字符串到日期选择器
【发布时间】:2017-04-10 21:00:52
【问题描述】:

我可以使用DatePicker 小部件将日期保存到字符串,如下所示:

dpResult = (DatePicker) findViewById(R.id.dpResult);
int day = dpResult.getDayOfMonth();
int month = dpResult.getMonth() -1;
int year = dpResult.getYear();


SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
Date d = new Date(year, month, day);
String strDate = dateFormatter.format(d);

这提供了一个字符串,例如2016 年 4 月 9 日:04-09-3916

我的问题是如何扭转这一点,让字符串值显示在我的 DatePicker 中?

我尝试了以下代码:

String[] parts = strDate.split("-");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];

month = Integer.parseInt(part1);
day = Integer.parseInt(part2);
year = Integer.parseInt(part3);

dpResult.init(year, month, day, null);

但 DatePicker 显示的日期是 2100 年 12 月 31 日,而不是 2016 年 4 月 9 日

【问题讨论】:

  • 你不能使用setter,因为你使用了getter?
  • this SO 问题可能重复?

标签: java android datepicker


【解决方案1】:

使用这个

datepicker.init(year, month, day, null);

【讨论】:

  • 我还添加了int yeara = Integer.parseInt(part3);year = yeara - 1900;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
相关资源
最近更新 更多