比较开始日期和结束日期安卓



我有两个函数,它允许用户设置特定的日期。它是开始日期和结束日期。现在我需要比较这两个变量,因为"开始日期"不应大于"结束日期"。 这是我在安卓中的代码。任何帮助都可以。谢谢。

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {
        yr = year;
        month = monthOfYear;
        day = dayOfMonth;
        Date.setText((month + 1) + "/" + day + "/" + yr);
    }
};
private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker view1, int year1, int monthOfYear1, int dayOfMonth1)
    {
        yr = year1;
        month = monthOfYear1;
        day = dayOfMonth1;
        DateEnd.setText((month + 1) + "/" + day + "/" + yr);
    }
};

以下是我比较两个日期的代码,

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String startDate = tvStartDate.getText().toString().trim(); 
String endDate = tvEndDate.getText().toString().trim();
startDate = startDate.substring( startDate.indexOf( ":" ) + 1 ); 
endDate = endDate.substring( endDate.indexOf( ":" ) + 1 );
Date startDat = sdf.parse(startDate); 
Date endDat = sdf.parse(endDate);
if ( startDat.compareTo(endDat) <= 0 )
{
     // ok
}
else
{
    Toast.makeText(getApplicationContext(), "Start date must be lesser than or equal to End Date", Toast.LENGTH_SHORT ).show();
}

您可以通过以下代码进行检查。

if (mDateSetListener.before(mDateSetListener1) || Code.DueDate.equals(Code.AssignDate)) {
    AlertDialog.Builder alertDialogBuilderDate = new AlertDialog.Builder(Assignment_Create_Ext_DB.this);
    alertDialogBuilderDate.setTitle("Date assigning issue");
    alertDialogBuilderDate
    .setMessage("Due date can not be equal or less then the Assign date")
    .setCancelable(false)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
    alertDialogBuilderDate.show();
}

只需将日期字符串转换为日期对象,如下所示:

 try{       
                SimpleDateFormat curFormater = new SimpleDateFormat(dateFormat.getValue(),locale); 
                Date d1 = curFormater.parse(dateStr); 
Date d2 = curFormater.parse(dateStr2); 
//Now just compare the two dates as
if(d1.before(d2)){
}
else{
}
            }catch (Exception e) {
                LogHandler.error(tag, "getDataFormate", e);
                return null;
            }

让我知道它是否有帮助。

    boolean compareTime(int startDay, int startMonth, int startYear,
        int monthDay, int month, int year) {
    Time timeNow = new Time();
    Time timeSelected = new Time();
    timeNow.set(startDay, startMonth, startYear);
    timeSelected.set(monthDay, month, year);
    long millisNow = timeNow.toMillis(true);
    long millisSelected = timeSelected.toMillis(true);
    long diff = millisSelected - millisNow;
    long diffHours = diff / (60 * 60 * 1000);
    Log.i(TAG, "diffHours = " + diffHours);
    if (diffHours <= 0) {
        return true;
    } else {
        return false;
    }
}

相关内容

  • 没有找到相关文章

最新更新