我在其中用戶選擇兩個日期即─從日期和迄今爲止。既領域不應該接受任何日期小於當前日期的應用。我已經瀏覽了本網站的一些帖子,並通過使用單個日期選擇器對話框和單個ondatesetListener實現了此功能。驗證「從日期」和「日期」,在Android的
我的問題是,我無法驗證的條件,迄今爲止應該從日期比總是大於或等於。請幫助我,我是無法從迄今捕捉爲活動日期爲超越控制由有源結束日期advance.Following由於是代碼:
public class Leave_form extends Activity
{
private static EditText tvDislpayResult;
private Button startPickDate;
private Button endPickDate;
private Calendar startDate;
private Calendar endDate;
//private EditText startDateDisplay;
//private EditText endDateDisplay;
static final int DATE_DIALOG_ID = 0;
private TextView startDateDisplay;
private TextView endDateDisplay;
private TextView activeDateDisplay;
private Calendar activeDate;
private Calendar currentDate;
private Calendar fromDate;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave_form);
/* capture our View elements for the start date function */
startDateDisplay = (EditText) findViewById(R.id.e_from_date);
startPickDate = (Button) findViewById(R.id.Set_date1);
/*get tday date */
currentDate=Calendar.getInstance();
Log.d("currentDate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
/* get the current date */
startDate = Calendar.getInstance();
Log.d("startdate ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));
/* add a click listener to the button */
startPickDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDateDialog(startDateDisplay, startDate);
Log.d("startDate-on click ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));
}
});
/* capture our View elements for the end date function */
endDateDisplay = (EditText) findViewById(R.id.e_to_date);
endPickDate = (Button) findViewById(R.id.Set_date2);
/* get the current date */
endDate = Calendar.getInstance();
Log.d("endDate ",""+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR)));
/* add a click listener to the button */
endPickDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
showDateDialog(endDateDisplay, endDate);
Log.d("endDate -on click",""+(endDate.get(Calendar.DAY_OF_MONTH))+(endDate.get(Calendar.MONTH)+1)+(endDate.get(Calendar.YEAR)));
}
});
/* display the current date (this method is below) */
// updateDisplay(startDateDisplay, startDate);
// updateDisplay(endDateDisplay, endDate);
}
private void updateDisplay(TextView dateDisplay, Calendar date)
{
dateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.MONTH)+1).append("-")
.append(date.get(Calendar.YEAR)).append(" "));
Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));
fromDate=date;
}
public void showDateDialog(TextView dateDisplay, Calendar date)
{
Log.d("SDD", dateDisplay.getText().toString());
Log.d("startdate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}
private OnDateSetListener dateSetListener = new OnDateSetListener()
{
@SuppressWarnings("deprecation")
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
Log.d("activeDate-on dateset ",""+(activeDate.get(Calendar.DAY_OF_MONTH))+(activeDate.get(Calendar.MONTH)+1)+(activeDate.get(Calendar.YEAR)));
if(currentDate.after(activeDate)|(currentDate.equals(activeDate)))
{
Toast toast=Toast.makeText(Leave_form.this, "Please select a valid date", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else
{
if((currentDate.after(activeDate)|(currentDate.equals(activeDate)))&&(startDateDisplay.getText().toString()!=null))
{
Log.d("startDate-on click ",""+(startDate.get(Calendar.DAY_OF_MONTH))+(startDate.get(Calendar.MONTH)+1)+(startDate.get(Calendar.YEAR)));
if(startDate.before(activeDate))
{
updateDisplay(activeDateDisplay, activeDate);
}
else
{
Toast.makeText(getApplicationContext(), "enter valid date", Toast.LENGTH_SHORT).show();
/*AlertDialog alertDialog=new AlertDialog.Builder(Leave_form.this).create();
alertDialog.setMessage("Choose a valid date");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialog.show();*/
}
}
else
{
updateDisplay(activeDateDisplay, activeDate);
}
}
unregisterDateDisplay();
}
};
private void unregisterDateDisplay()
{
Log.d("startdate ",""+(currentDate.get(Calendar.DAY_OF_MONTH))+(currentDate.get(Calendar.MONTH)+1)+(currentDate.get(Calendar.YEAR)));
Log.d("from date:","-"+activeDate.DAY_OF_MONTH+activeDate.MONTH+activeDate.YEAR);
activeDateDisplay = null;
activeDate = null;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
Log.d("fromdate","msg");
Log.d("id",""+DATE_DIALOG_ID);
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
Log.d("id",""+DATE_DIALOG_ID);
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
}
}
public void leave(View view)
{
Intent intent1=new Intent(this,Rough.class);
setContentView(R.layout.activity_rough);
startActivity(intent1);
}
public void logout(View view)
{
Intent intent2=new Intent (this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent2);
}
}
使用 的SimpleDateFormat curFormater =新的SimpleDateFormat( 「YYYY-MM-dd'T'HH:MM:SS」); 日期d1,d2; d1 = curFormater.parse(dbDateMod); d2 = curFormater.parse(wsDbDateMod); \t \t \t \t \t \t如果(d1.before(D2)) \t \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t} – Amit 2013-04-05 13:06:18
CHCK我的解決方案 – Nezam 2013-04-05 13:38:11
感謝@Amit ..但笏在這裏是「dbDateMod」嗎? – user2179989 2013-04-05 13:46:55