Pergunta

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");
        }
Foi útil?

Solução

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.

Outras dicas

Dont use the string representation. Instead use the Calendar object to do a before and after.

Calendar c1 = Calendar.getInstance();

Calendar min = \\ create your min calendar
Calendar max = \\ create your max calendar.

if ( c1.after(min) && c1.before(max) ) {
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top