0

I want to display the arraylist like this:

[2013-11-01,2013-11-8,2013-11-15,2013-11-22,2013-11-29]

I am written the below code and i am passing the static values to that method:

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import java.util.List;


public class DateExample {


     Calendar cal = Calendar.getInstance();


    public static void main(String[] args) {



        DateExample date=new DateExample();
        date.getAllDaysInaMonth("2013",11,1);

    }

    public  List<java.sql.Date> getAllDaysInaMonth(String year,int month,int day){
        System.out.println(year+""+month+""+day);

        cal.set(Calendar.DAY_OF_MONTH, 1); 

        int utilyear=cal.get(cal.YEAR);
        String syear=   Integer.toString(utilyear);
        int utilmonth=cal.get(cal.MONTH);
        int utilday=cal.get(cal.DAY_OF_MONTH);



        List<java.sql.Date> arraylist=new ArrayList<java.sql.Date>();
        while (month==cal.get(Calendar.MONTH)) {
             System.out.println("while"+cal.getTime());

              SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                String dateofutil=format.format(cal.getTime());
                System.out.println("dateofutil"+dateofutil);
                try {
                    java.sql.Date sqldate=new java.sql.Date(format.parse(dateofutil).getTime());
                    arraylist.add(sqldate);


                } catch (ParseException e) {

                    e.printStackTrace();
                }

           cal.add(Calendar.DAY_OF_MONTH,1);

                }
         System.out.println("arraylist values"+arraylist);
        return arraylist;
    }





}

Here am passing static year,month,date to method as a parameters through that values am printing the dates like yyyy-MM-dd format when am passing 1 day then after 7 days date is printed But the above code is not working properly give me correct code

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2996174
  • 1,391
  • 4
  • 13
  • 20

3 Answers3

1

There are two things to be changed here.

First,

while (month==cal.get(Calendar.MONTH)) {

needs to be changed to

while (month==cal.get(Calendar.MONTH)+1) {

because the according to docs

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0;

and the second thing is to make your ArrayList of type String and not Date, because Date does not have a format. You can only get a formatted String representation of it.

List<String> arraylist = new ArrayList<String>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // This can go out of the `while` loop though.
String dateofutil = format.format(cal.getTime());
arraylist.add(dateofutil);
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

You need to make the change below:

From

while (month==cal.get(Calendar.MONTH)) {

to

while ((month-1)==cal.get(Calendar.MONTH)) {

See more in Calendar.class ==> NOVEMBER , it is 10 rather 11. That's why previously you did not get the expected result. Change it and go ahead.

public final static int NOVEMBER = 10;

/**
 * Value of the {@link #MONTH} field indicating the
 * twelfth month of the year.
 */
public final static int DECEMBER = 11;

/**
 * Value of the {@link #MONTH} field indicating the
 * thirteenth month of the year. Although <code>GregorianCalendar</code>
 * does not use this value, lunar calendars do.
 */
public final static int UNDECIMBER = 12;
Mengjun
  • 3,159
  • 1
  • 15
  • 21
0

The answer by R.J and the answer by MouseLearnJava are both correct.


This kind of date-time work is much easier using the Joda-Time library.

For one thing, Joda-Time counts from one unlike the zero-based silliness of java.util.Calendar. So the days of week are 1-7, months of year 1-12.

Here is some example code using Joda-Time 2.3 and Java 7.

// © 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.*;

// Data passed into method.
int year = 2014;
int month = 2;
int day = 2;

java.util.List<DateTime> dateTimes = new ArrayList<DateTime>();
DateTime start = new DateTime( year, month, day, 0, 0, 0 );
DateTime dateTime = start;

while( dateTime.monthOfYear().equals( start.monthOfYear() ) ) {
    dateTimes.add( dateTime ); // Collect each date-time object.
    dateTime = dateTime.plusDays( 7 );
}

System.out.println( "The list: " + dateTimes );

for( DateTime item : dateTimes ){
    System.out.println( ISODateTimeFormat.date().print( item ) );
}

When run…

The list: [2014-02-02T00:00:00.000-08:00, 2014-02-09T00:00:00.000-08:00, 2014-02-16T00:00:00.000-08:00, 2014-02-23T00:00:00.000-08:00]
2014-02-02
2014-02-09
2014-02-16
2014-02-23
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154