Question

Aim: to fetch the next seven days (including the current day). I am using add(int field, int value) method of the Calendar class.

Code:

Calendar cal = Calendar.getInstance();

for (int i = 0; i < 7; i++){

    int index = cal.get(Calendar.DAY_OF_WEEK) - 1;
    String text = cal.get(Calendar.DATE) + " " +cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
    cal.add(cal.get(Calendar.DAY_OF_MONTH), 1);
    Log.d(TAG, text);

}

Throws IllegalArgumentException. According to the docs, this exception is thrown if the field is ZONE_OFFSET or DST_OFFSET.

Logcat:

E/AndroidRuntime(17841): Caused by: java.lang.IllegalArgumentException
E/AndroidRuntime(17841):    at java.util.GregorianCalendar.add(GregorianCalendar.java:357)
Was it helpful?

Solution

You should be using

cal.add(Calendar.DAY_OF_MONTH, 1);

The first parameter of that method is the date field to add to.

You were passing the value of that field as the field identifier. It was probably over the value of ZONE_OFFSET.

OTHER TIPS

The answer by Sotirios Delimanolis is correct.


FYI, this kind of date-time work is easier with the Joda-Time library.

Here is some example code using Joda-Time 2.3 and Java 7. I used a Paris time zone and a France locale, but you can replace with your own.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

// Specify time zone rather than rely on default.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );

// Joda-Time offers many formatters, plus you may define your own.
// Using "forStyle" approach, pass first letter of the word Short/Medium/Long/Full, first for date and then for time.
DateTimeFormatter formatter = DateTimeFormat.forStyle( "MM" ).withLocale( Locale.FRANCE ).withZone( timeZone );

DateTime now = new DateTime(  timeZone );
for (int i = 0; i < 7; i++){
    DateTime dateTime = now.plusDays( i );

    // Extract descriptions.
    String dayOfWeekAsText = dateTime.dayOfWeek().getAsText( Locale.FRANCE );
    String monthAsText = dateTime.monthOfYear().getAsText( Locale.FRANCE );
    String dateTimeAsText = formatter.print( dateTime );

    // Dump to console.
    System.out.println( "dayOfWeekAsText: " + dayOfWeekAsText );
    System.out.println( "monthAsText: " + monthAsText );
    System.out.println( "dateTimeAsText: " + dateTimeAsText );
    System.out.println(); // Blank line.
}

When run…

dayOfWeekAsText: lundi
monthAsText: janvier
dateTimeAsText: 6 janv. 2014 18:35:18

dayOfWeekAsText: mardi
monthAsText: janvier
dateTimeAsText: 7 janv. 2014 18:35:18

dayOfWeekAsText: mercredi
monthAsText: janvier
dateTimeAsText: 8 janv. 2014 18:35:18

dayOfWeekAsText: jeudi
monthAsText: janvier
dateTimeAsText: 9 janv. 2014 18:35:18

dayOfWeekAsText: vendredi
monthAsText: janvier
dateTimeAsText: 10 janv. 2014 18:35:18

dayOfWeekAsText: samedi
monthAsText: janvier
dateTimeAsText: 11 janv. 2014 18:35:18

dayOfWeekAsText: dimanche
monthAsText: janvier
dateTimeAsText: 12 janv. 2014 18:35:18

I was Suffering on the same issue before I found simple way of doing it, just use the Roll method of Calendar Instance and it will increment date by one or you can specify amount for increment, in steady of true put day interval, negative for decrements. in kotlin is done like this #codes below

 val calendar = Calendar.getInstance()
               for(i in 0..6){
                   calendar.roll(Calendar.DATE,true)
                   println(getDateInstance().format(calendar.time))
               }

and the result is like

I/System.out: Jun 11, 2019
I/System.out: Jun 12, 2019
I/System.out: Jun 13, 2019
I/System.out: Jun 14, 2019
I/System.out: Jun 15, 2019
I/System.out: Jun 16, 2019
I/System.out: Jun 17, 2019

hope it helps

use following code

 class CalenderService extends AsyncTask
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        DayList=new ArrayList<>();
    }

    @Override
    protected Object doInBackground(Object[] params) {

        SimpleDateFormat sdf = new SimpleDateFormat("EEE");
        Date d = new Date();
        for (int i=0;i<7;i++)
        {
            Calendar cal=Calendar.getInstance();
            cal.setTime(d);
            cal.add(Calendar.DAY_OF_WEEK,i);

            Date dayweek=cal.getTime();

            String NextDay=sdf.format(dayweek);
            int nxtday=dayweek.getDate();



            ED=new Everydays(nxtday,NextDay);
            DayList.add(ED);

        }
        return null;
    }


    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        for (int i=0;i<DayList.size();i++)
        {
            Toast.makeText(getApplicationContext(),String.valueOf(DayList.get(i).nxtday)+" "+DayList.get(i).nextDay,Toast.LENGTH_SHORT).show();


        }

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top