我从日期选择器获取字符串格式的month
和year
,如何使用任何日期格式化程序将其转换为完成日期?
答案 0 :(得分:1)
您可以将string
解析为date
,因此您需要将字符串与日期相关联的格式:
使用dateformat将字符串转换为日期:
String startDateString = "Feb 2007"; //this is the var comming from the datepicker
DateFormat df = new SimpleDateFormat("MMM yyyy");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
private DatePickerDialog.OnDateSetListener pDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String dt = String.valueOf(dayOfMonth) + String.valueOf(monthOfYear) + String.valueOf(year);
Date d = sdf.parse(dt);
log.d(d);
}catch(ParseException ex){}
}
};