13

I am taking current date using the code like below

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

And I am selecting 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 next activity as--

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

I want to check here if user selected previous date from current date then give a message and don't go on next activity.

RHertel
  • 23,412
  • 5
  • 38
  • 64
Soni Kumar
  • 121
  • 1
  • 2
  • 8

4 Answers4

25

Use SimpleDateFormat:

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

String my_date = "31/12/2014"

Then you need to convert it into 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;
}
Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
1

I am providing the modern answer.

java.time and ThreeTenABP

Use LocalDate from java.time, the modern Java date and time API.

To take the current date

    LocalDate currentDate = LocalDate.now(ZoneId.systemDefault());
    System.out.println(currentDate);

When I ran this code just now, the output was:

2020-01-05

To get selected date in your date picker

    int year = 2019;
    int month = Calendar.DECEMBER; // but don’t use `Calendar`
    int dayOfMonth = 30;

    LocalDate selectedDate = LocalDate.of(year, month + 1, dayOfMonth);

    System.out.println(selectedDate);

2019-12-30

Your date picker is using the same insane month numbering that the poorly designed and long outdated Calendar class is using. Only for this reason, in an attempt to produce readable code, I am using a constant from that class to initialize month. In your date picker you are getting the number given to you, so you have no reason to use Calendar. So don’t.

And for the same reason, just as in your own code I am adding 1 to month to get the correct month number (e.g., 12 for December).

Is the date in the past?

    if (selectedDate.isBefore(currentDate)) {
        System.out.println("" + selectedDate + " is in the past; not going to next activity");
    } else {
        System.out.println("" + selectedDate + " is OK; going to next activity");
    }

2019-12-30 is in the past; not going to next activity

Converting to String and back

If you need to convert your selected date to a string in order to pass it through the Intent (I don’t know whether this is a requirement), use the toString and parse methods of LocalDate:

    String dateAsString = selectedDate.toString();
    LocalDate recreatedLocalDate = LocalDate.parse(dateAsString);
    System.out.println(recreatedLocalDate);

2019-12-30

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Use below code,

  1. Create a date object using date formator.
  2. Compare date (There many way out to compare dates and one is mentioned here)
  3. Open intent or make toast as you said 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

  1. You need a view xml having the calender defined in it. It can be a fragment or activity view xml file
  2. Inflate the view in your Activity or fragment class.

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

  1. Get the respective control java object from the xml like

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

  1. Now call event listener on this cal object.
Umesh Aawte
  • 4,590
  • 7
  • 41
  • 51
0

Try this lines of code, this may help. Cheers!!!

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();
    }
Md. Rejaul Karim
  • 694
  • 7
  • 14