How to compare current date with selected date in android with java

I am using the current date using the code as below

long millis=System.currentTimeMillis(); java.sql.Date date=new java.sql.Date(millis); 

And I choose the date using

 CalendarView cal.setOnDateChangeListener(new OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) String s = +year + " : " + (month + 1) + " : " +dayOfMonth ; 

and passing it on to the next action as -

 Intent in = new Intent(MainActivity.this, sec.class); in.putExtra("TextBox", s.toString()); startActivity(in); 

I want to check here if the user selected the previous date from the current date then give a message and not continue the next operation.

+9
source share
3 answers

Use SimpleDateFormat:

If your date is in the format 12/31/2014 .

 String my_date = "31/12/2014" 

Then you need to convert it to SimpleDateFormat

 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = sdf.parse(my_date); if (new Date().after(strDate)) { your_date_is_outdated = true; } else{ your_date_is_outdated = false; } 

or

 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = sdf.parse(my_date); if (System.currentTimeMillis() > strDate.getTime()) { your_date_is_outdated = true; } else{ your_date_is_outdated = false; } 
+19
source

Use below code,

  • Create a date object using the date formatter.
  • Compare the date (there are many options for comparing dates and one is mentioned here)
  • Open the intention or make a toast as you said the message.

     CalendarView cal.setOnDateChangeListener(new OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { String s = (month + 1) + "-" + dayOfMonth + "-" + year; SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Date dateSource = null; Calendar cal = Calendar.getInstance(); Date sysDate = cal.getTime(); try { dateSource = sdf.parse(s); if(dateSource.compareTo(sysDate)>0){ Toast.makeToast("Selected worng date",Toast.SHOW_LONG).show(); }else{ Intent in = new Intent(MainActivity.this, sec.class); in.putExtra("TextBox", s.toString()); startActivity(in); } } catch (ParseException e) { Loger.log("Parse Exception " + e); e.printStackTrace(); } } } 

Edit

  • You need an xml view with the calendar defined in it. It can be an XML file with a fragment or activity.
  • Enjoy the view in your Activity class or snippet.

View _rootView = inflater.inflate ({your layout file}, container, false);

  1. Get appropriate java control from xml e.g.

cal = (CalendarView) _rootView.findViewById (R.id.calViewId);

  1. Now call the event listener for this cal object.
0
source

Try this line of code, this may help. Hooray !!!

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse(enteredDate); if (System.currentTimeMillis() > date.getTime()) { //Entered date is backdated from current date } else { //Entered date is updated from current date } } catch (ParseException e) { e.printStackTrace(); } 
0
source

All Articles