how do i compare current date with user input date from date picker
Solution 1
Create a new Date object this way:
Date now = new Date(System.currentTimeMillis());
and use int result = now.compareTo(theOtherDate);
result will be an int < 0 if now Date is less than the theOtherDate Date, 0 if they are equal, and an int > 0 if this Date is greater.
Solution 2
The code gets the user date from date picker and converting to java date format and storing the date in date format variable and then i'm getting the current system date and storing the date in another date format variable, now i got two date where i can easily compare.
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int myear, int monthOfYear,int dayOfMonth) {
// get the user picked date from date picker
year=myear;
month=monthOfYear+1;
day=dayOfMonth;
// then convert all values to a single string
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String sday = (""+day);
String smonth=("-"+month);
String syear=("-"+year);
String y=(""+sday+smonth+syear);
//convert the string date to a java date
try
{
Date pickerdate = formatter.parse(y);
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//printing the system current date and picker date
Date systemdate = new Date(System.currentTimeMillis());
System.out.println("current "+systemdate);
System.out.println("format"+pickerdate);
//now u got the two date in date type to compare it easily put ur if and else condition
if(your comparedate condition)
{
do what U want to do
}
else
{
}
}
};
Solution 3
protected boolean checkDateValidity(Date date){
Calendar cal = new GregorianCalendar(date.getYear() + 1900, date.getMonth(), date.getDate(), date.getHours(), date.getMinutes());
if((cal.getTimeInMillis()-System.currentTimeMillis()) <= 0){
return false;
}else{
return true;
}
}
pass the date object to this method
if it returns false means you will show alert message
Chintan Soni
Hey visitor, Greetings !!! I am Chintan Soni, a Passionate, Tech savvy, Independent, Self-motivating Mobile Application Developer. Currently serving as TechLead - Android @ ARInspect. Always eager to learn new technologies. Love to help people learn with, whatever I got. Meet my brother: Yash Soni A tech-enthusiast, Gadget Freak and Apple Fanboy. Motto that LIT my life: Learn, Implement and Teach others. Github: https://github.com/iChintanSoni Medium: https://medium.com/@chintansoni My Mentor: Paresh Mayani, Pratik Patel Currently onto: Flutter Kotlin Platforms and Frameworks I have worked on: Android Native Application Development Ionic 3.x Application Development Microsoft ASP.net Web Api 2 using Entity Framework 6.1 (MSSQL Server Database 2014) Node.js (MySQL Server) Google Firebase Augmented Reality Facebook Messenger Bots And what not...!!! Follow me on: Facebook, Twitter, LinkedIn
Updated on June 15, 2022Comments
-
Chintan Soni almost 2 years
I am trying to keep constraint on date and time. I want that if user tries to set date less than current date then it should show alert, and same thing is to be done with time. I am using date and time pickers and my
onDateSetListener
andonTimeSetListener
are as below:DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { dateTime.set(Calendar.YEAR, year); dateTime.set(Calendar.MONTH, monthOfYear); dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth); update_date_display(); // ---for setting notification---- day = dayOfMonth; month = monthOfYear; AddEditTask.year = year; c.set(year, month, day); } }; protected void update_date_display() { et_task_date.setText(formatDate.format(dateTime.getTime())); } TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay); dateTime.set(Calendar.MINUTE, minute); update_time_display(); // ---for setting notification--- hour = hourOfDay; AddEditTask.minute = minute; c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); } }; private void update_time_display() { et_task_time.setText(formatTime.format(dateTime.getTime())); }
can you please tell me what is to be written within those listeners to achieve what i want. Thanks in advance...!!!
-
vortexwolf almost 11 yearsHow does this code work now? How should it work?
-
-
talegna about 10 yearsYou might want to look here at the help centre guide for writing answers, chunks of code might work but including an explanation is always better