Iewicz Iewicz - 2 months ago 11
Java Question

How to get another Calendar result from same instance?

I'm trying to make an ArrayList with Holidays and dates (both Strings), then convert date to Calendar, get the day of week (using DAY_OF_WEEK) and/or sort by date/name, but for some reason I get the same result (Calendar) for every item on the list.

This is my code:

    public static ArrayList  listOfHolidays = new ArrayList();

Holidays.holidaysList.add(new Holidays("Sukkot", "09/10/2014"));
Holidays.holidaysList.add(new Holidays("Hanukkah", "17/12/2014"));
Holidays.holidaysList.add(new Holidays("Purim", "16/03/2014"));


Holidays class:



Calendar calendar = Calendar.getInstance();

@Override
public String toString() {
//DayOfWeek is just an enum of days (strings)
return holidayName + " falls on "
+ DayOfWeek.values()[Calendar.DAY_OF_WEEK - 1]; // Here i get the same day each time
}

@Override
public int compareTo(Holidays another) {
if(MainActivity.sortByName == true) {
return holidayName.compareToIgnoreCase(another.holidayName);
}
return convertStringToCal() - another.convertStringToCal();
}

private int convertStringToCal() {
int year, month, day;
day = Integer.valueOf(holidayDate.substring(0, 2));
month = Integer.valueOf(holidayDate.substring(3, 5));
year = Integer.valueOf(holidayDate.substring(6, 10));
calendar = Calendar.getInstance();
calendar.set(year, month, day);
return (int) calendar.getTimeInMillis();
}



I call
Collections.sort()
from within a radioButton method to sort.

Answer

I see multiple reasons why it may not work.

First i don't know if you omitted part of the code of you Holidays class but you never actually set the calendar object aside from the convertStringToCal() method but i don't see if this method is also called in the constructor.

Secondly and most likely your problem:

@Override
    public String toString() {
    //DayOfWeek is just an enum of days (strings)
        return holidayName + " falls on "
                                        // Here you are using the constant Calendar.DAY_OF_WEEK
                    + DayOfWeek.values()[Calendar.DAY_OF_WEEK - 1]; // Here i get the same day each time
    }

It should actually look something like this:

@Override
    public String toString() {
    //DayOfWeek is just an enum of days (strings)
        return holidayName + " falls on "
                    + DayOfWeek.values()[calendar.get(Calendar.DAY_OF_WEEK)]; // Now you won't get the same day every time. :)
    }

Some additional notes:

I would use Date objects to hold the dates, and use SimpleDateFormat to convert Strings to Dates and the other way around.

If you use these two your Holidays class would look something like this:

public class Holidays {

        private final Calendar calendar = Calendar.getInstance();
        private final String holidayName;

        public Holidays(String holidayName, Date date) {
            this.holidayName = holidayName;
            this.calendar.setTime(date);
        }

        @Override
        public int compareTo(Holidays another) {
            // Generally you should avoid passing information statically between Activities
            if(MainActivity.sortByName == true) {
                    return holidayName.compareToIgnoreCase(another.holidayName);
            }
            return getTimeInMillis() - another.getTimeInMillis();
        }

        public long getTimeInMillis() {
            return this.calendar.getTimeInMillis();
        }

        @Override
        public String toString() {
        //DayOfWeek is just an enum of days (strings)
            DayOfWeek day = DayOfWeek.values()[calendar.get(Calendar.DAY_OF_WEEK)]; // Now you won't get the same day every time. :)
            return String.format("%s falls of %s", this.holidayName, day);
        }

    }

You can create Date objects like this:

// Create date objects with calendar.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 3);
calendar.set(Calendar.DAY_OF_MONTH, 27);
Date date = calendar.getTime();

The Constructors of the Date object like new Date(year, month, day) are deprecated and should not be used. Always use a Calendar instance to create date objects.

With SimpleDateFormat you can convert String and Date objects like this:

// The String pattern defines how date strings are parsed and formated
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

String dateString = "09/10/2014";

// Convert a String to Date
Date dateFromDateString = sdf.parse(dateString);

// Convert a Date to a String
String dateStringFromDate = sdf.format(dateFromDateString);

If you need more than just simple conversion of Date and String objects you can use DateFormat. That is pretty much the Rolls Royce of Date String conversion. It can parse and format Strings in a much more general way without actually requiring a pattern and it automatically accounts for locale and much more.

Or you could just use JodaTime :)

Comments