Android 结束日期大于开始日期
我有两个编辑文本。一个带开始日期,另一个带结束日期。 我需要进行验证,并检查结束日期是否大于开始日期。我不知道该怎么做 在我的代码中,我将两个日期之间的差异设置为天,现在我还需要检查结束日期是否大于开始日期 这是我的密码:Android 结束日期大于开始日期,android,validation,date,calendar,Android,Validation,Date,Calendar,我有两个编辑文本。一个带开始日期,另一个带结束日期。 我需要进行验证,并检查结束日期是否大于开始日期。我不知道该怎么做 在我的代码中,我将两个日期之间的差异设置为天,现在我还需要检查结束日期是否大于开始日期 这是我的密码: //EditText with string of start date dataInicio = (EditText)findViewById(R.id.ses_dpDataIni); //Edit
//EditText with string of start date
dataInicio = (EditText)findViewById(R.id.ses_dpDataIni);
//EditText with string of end date
dataFim = (EditText)findViewById(R.id.ses_dpDataFim);
//Convert String to calendar to check the difference between two dates..
try{
dateInic = dataInicio.getText().toString();
dateFim = dataFim.getText().toString();
calInic=Calendar.getInstance();
calFim = Calendar.getInstance();
calInic.setTime(form.parse(dateInic));
calFim.setTime(form.parse(dateFim));
}
catch (ParseException e) {
e.printStackTrace();
}
Log.w(SessaoQuotaEdit.class.getName(),"DIFERENCA DE DIAS" +daysBetween(calInic,calFim));
tvDiasQuotas = (TextView)findViewById(R.id.ses_tvNumDiasQuota);
tvDiasQuotas.setText("NUMBER OF DAYS: " +daysBetween(calInic,calFim));
//CHECK IS END-DATE IS GREATER THAN START-DATE
.............
.............
你能帮我吗?
谢谢:)试试这个功能
public static boolean isDateAfter(String startDate,String endDate)
{
try
{
String myFormatString = "yyyy-M-dd"; // for example
SimpleDateFormat df = new SimpleDateFormat(myFormatString);
Date date1 = df.parse(endDate));
Date startingDate = df.parse(startDate);
if (date1.after(startingDate))
return true;
else
return false;
}
catch (Exception e)
{
return false;
}
}
如果enddate在开始日期之后,则返回true。为什么不试试?感谢大家
Here is my solution
if(calInic.after(calFim)==true)
Log.w(SessaoQuotaEdit.class.getName(),"ERROR: DATA FINAL É ANTES DA DATA INICIAL");
else
if(calInic.before(calFim)==true)
Log.w(SessaoQuotaEdit.class.getName(),"CORRECTO: DATAS CORRECTAS");
else
if(calInic.equals(calFim)==true)
Log.w(SessaoQuotaEdit.class.getName(),"CORRECTO: DATAS IGUAIS");
***使用Ram kiran回答***
公共布尔校验日期(字符串开始日期、字符串结束日期){
布尔b=假;
SimpleDataFormat dfDate=新的SimpleDataFormat(“MM/dd/yyyy”);
试一试{
if(dfDate.parse(startDate).before(dfDate.parse(endDate))){
b=true;//如果开始日期早于结束日期
}else if(dfDate.parse(startDate).equals(dfDate.parse(endDate))){
b=true;//如果两个日期相等
}否则{
b=false;//如果开始日期在结束日期之后
}
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回b;
}
检查日期之前(开始日期、结束日期);
**并检查另一时间是否少于或等于今天**
public boolean checkDatesAfter(String selectedDate,String today){
布尔b=假;
SimpleDataFormat dfDate=新的SimpleDataFormat(“MM/dd/yyyy”);
试一试{
if(dfDate.parse(selectedDate).compareTo((dfDate.parse(今天)))<0)
{
b=true;//如果开始日期早于结束日期
}else if(dfDate.parse(selectedDate).equals(dfDate.parse(today))){
b=true;//如果两个日期相等
}否则{
b=false;//如果开始日期在结束日期之后
}
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回b;
}
在这里输入代码
致电checkDatesAfter(选择日期,今天)
**祝你好运**
拉姆·基兰的简化答案,它只会在一条语句中进行检查
public static boolean checkDates(String d1, String d2) {
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
boolean b = false;
try {
b = dfDate.parse(d1).before(dfDate.parse(d2)) || dfDate.parse(d1).equals(dfDate.parse(d2));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
Kotlin扩展名:
fun String.isLessThanEndDate(toDate:String):Boolean {
val dfDate = SimpleDateFormat("dd MMM, yyyy", Locale.ENGLISH)
var result = false
try
{
result = dfDate.parse(this).before(dfDate.parse(toDate)) || dfDate.parse(this) == dfDate.parse(toDate)
}
catch (e:ParseException) {
e.printStackTrace()
}
return result
}
谢谢,你救了我一天。我在用kotlin,我的一直给我例外。你能帮助我吗?一些解释或代码注释可以改进答案。虽然这个代码片段可以解决这个问题,但它没有解释为什么或者如何回答这个问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
if (!dateValidation("2016-11-23", "2016-11-24", "yyyy-MM-dd")) {
// ToastHelper.show("Please select date properly");
// or do something
}
public static boolean dateValidation(String startDate,String endDate,String dateFormat )
{
try
{
SimpleDateFormat df = new SimpleDateFormat(dateFormat);
Date date1 = df.parse(endDate);
Date startingDate = df.parse(startDate);
if (date1.after(startingDate))
return true;
else
return false;
}
catch (Exception e)
{
return false;
}
}
public static boolean checkDates(String d1, String d2) {
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
boolean b = false;
try {
b = dfDate.parse(d1).before(dfDate.parse(d2)) || dfDate.parse(d1).equals(dfDate.parse(d2));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
public static boolean CheckDates(String d1, String d2){
SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
boolean b = false;
try {
if(dfDate.parse(d1).before(dfDate.parse(d2)))
{
b = true;//If start date is before end date
}
else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
{
b = true;//If two dates are equal
}
else
{
b = false; //If start date is after the end date
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
fun String.isLessThanEndDate(toDate:String):Boolean {
val dfDate = SimpleDateFormat("dd MMM, yyyy", Locale.ENGLISH)
var result = false
try
{
result = dfDate.parse(this).before(dfDate.parse(toDate)) || dfDate.parse(this) == dfDate.parse(toDate)
}
catch (e:ParseException) {
e.printStackTrace()
}
return result
}
Calendar startdate = Calendar.getInstance();
Calendar enddate = Calendar.getInstance();
imgstartcal1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fromDatePickerDialog = new DatePickerDialog(mainActivity, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
startdate.set(year, monthOfYear, dayOfMonth);
edtstartdate.setText(dateFormatter.format(startdate.getTime()));
edtenddate.setText(null);
}
}, startdate.get(Calendar.YEAR), startdate.get(Calendar.MONTH), startdate.get(Calendar.DAY_OF_MONTH));
fromDatePickerDialog.show();
}
});
imgendcal1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fromDatePickerDialog = new DatePickerDialog(mainActivity, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
enddate.set(year, monthOfYear, dayOfMonth);
if (startdate.after(enddate) || enddate.equals(startdate)){
edtenddate.getText().toString().equals(null);
}else{
fromDatePickerDialog.getDatePicker().setMinDate(startdate.getInstance().getTimeInMillis());
edtenddate.setText(dateFormatter.format(enddate.getTime()));
}
}
}, enddate.get(Calendar.YEAR), startdate.get(Calendar.MONTH),startdate.get(Calendar.DAY_OF_MONTH));
fromDatePickerDialog.show();
}
});