gpt4 book ai didi

java - 日期验证android java

转载 作者:行者123 更新时间:2023-11-30 02:36:43 25 4
gpt4 key购买 nike

我的项目中有两个日期时间选择器,分别是开始日期和结束日期,
但是问题是说,如果我选择18/10/2014作为开始日期,结束日期是1/11/2014,则结束日期无效,因为第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);

=========================================================================================


对于验证部分的开始日期和结束日期

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;
}

最佳答案

要比较日期,请使用方法:
这是一个简单的示例,您可以将其用作返回结果的方法

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;

}

关于java - 日期验证android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26431307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com