我的项目中有两个日期时间选择器,即开始日期和结束日期, 但问题是,如果我选择18/10/2014作为开始日期,结束日期是2014年1月11日,结束日期无效,因为第1天因为小于第18天而无效。需要帮助这个
//start date
TableRow r8 = new TableRow(this);
TextView tvStartDate = new TextView(this);
tvStartDate.setText("Start Date: ");
final EditText etStartDate = new EditText(this);
etStartDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar dateNow = Calendar.getInstance();
int year = dateNow.get(Calendar.YEAR);
int month = dateNow.get(Calendar.MONTH);
int day = dateNow.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp;
dp = new DatePickerDialog(AddPetActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
selectedmonth = selectedmonth + 1;
etStartDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, year, month, day);
dp.setTitle("Select Date");
dp.show();
}
});
r8.addView(tvStartDate);
r8.addView(etStartDate);
tb.addView(r8);
//end date
TableRow r9 = new TableRow(this);
TextView tvEndDate = new TextView(this);
tvEndDate.setText("End Date: ");
final EditText etEndDate = new EditText(this);
etEndDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar dateNow = Calendar.getInstance();
int year = dateNow.get(Calendar.YEAR);
int month = dateNow.get(Calendar.MONTH);
int day = dateNow.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp;
dp = new DatePickerDialog(AddPetActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
selectedmonth = selectedmonth + 1;
etEndDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, year, month, day);
dp.setTitle("Select Date");
dp.show();
}
});
r9.addView(tvEndDate);
r9.addView(etEndDate);
tb.addView(r9);
=========================================================================================
验证部分startdate和结束日期
else if (CheckDate(today, etStartDate.getText().toString()) == false)
Toast.makeText(getApplicationContext(), "Invalid Start Date!", Toast.LENGTH_SHORT).show();
else{
if (!etEndDate.getText().toString().matches("")) {
if (CheckDate( etStartDate.getText().toString(), etEndDate.getText().toString()) == false)
Toast.makeText(getApplicationContext(), "Invalid End Date!", Toast.LENGTH_SHORT).show();
=========================================================================================
最后一部分
private boolean CheckDate(String fromDate, String toDate)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date dateFrom = null;
Date dateTo = null;
try {
dateFrom = sdf.parse(fromDate);
dateTo = sdf.parse(toDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (dateTo.after(dateFrom) == true)
return true;
else
return false;
}
答案 0 :(得分:1)
要比较日期,请使用以下方法:Date.compareTo(AnotherDate)
:
这是一个简单的例子,你可以使用它作为返回结果的方法
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); //edit here
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is before Date2");
}else if(date1.compareTo(date2)==0){
System.out.println("Date1 is equal to Date2");
}else{
System.out.println("How to get here?");
}
}catch(ParseException ex){
ex.printStackTrace();
}
适用于您的情况:
private boolean CheckDate(String fromDate, String toDate)
{
boolean result=false;
try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); //edit here
Date dateStart = sdf.parse(fromDate);
Date dateEnd = sdf.parse(toDate);
if(dateStart .compareTo(dateEnd)>0){
System.out.println("dateStart is after dateEnd ");
result=false;
}else if(dateStart .compareTo(dateEnd)<0){
System.out.println("dateStart is before dateEnd ");
result=true;
}else if(dateStart .compareTo(dateEnd)==0){
System.out.println("dateStart is equal to dateEnd ");
result=true;
}else{
System.out.println("How to get here?");
}
}catch(ParseException ex){
ex.printStackTrace();
}
return result;
}