javaandroiddateif-statementadk

ADK Java checking if the date equal to defined date interval


Id like to check if current date is equal(or contains in) to some defined date interval. I have something like this:

        Calendar c1 =  Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");

        Date lowerBound = null;
        Date upperBound = null;
        try {
            lowerBound = sdf1.parse("29-10-2013");
            upperBound = sdf1.parse("30-12-2013");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        TextView txtdate1 = (TextView) findViewById(R.id.textView1);

        if (c1.getTime().before(upperBound) && c1.getTime().after(lowerBound)) {
            txtdate1.setText("Is up to date");
        } else {
            txtdate1.setText("Is not up to date");
        }

Solution

  • You could use the functions before and after to check if your date is in a certain range.

        Calendar c1 =  Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
    
        Date lowerBound = sdf1.parse("20-10-2013");
        Date upperBound = sdf1.parse("30-12-2013");
    
        TextView txtdate1 = (TextView) findViewById(R.id.textView1);
    
        if (c1.getTime().before(upperBound) && c1.getTime().after(lowerBound)) {
            txtdate1.setText("Is up to date");
        } else {
            txtdate1.setText("Is not up to date");
        }
    

    The cool thing is that you can define some functions that will checks if the data is in some defined range by creating predefined functions. See this for example.