0

I have list of date on my java program. And i want make a method to return the count of day I pass in the parameter.

Example : I pass string "Tue" to this method. And then this method returned int 20. Assume that the int 20 is the count of date that have Tuesday as their day name.

Sorry for my english. as it isn't my natural language

elan
  • 5
  • 3
  • 2
    Your English is fine, the problem is in the question. I don't understand it at all. – Luiggi Mendoza Jun 10 '13 at 09:14
  • You can find the answer here: [http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date][1] [1]: http://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date – zargarf Jun 10 '13 at 09:18
  • 1
    What have you tried so far? Looks easy, iterate through the dates in your list and count how many of them are Tuesdays. – nakosspy Jun 10 '13 at 09:19
  • @zargarf I don't understand how can you pass `Tue` and get 20 (not 13, not 27, just 20). By the way, the links you've posted seems to go the other way around: you send the date and receive the name of the day. – Luiggi Mendoza Jun 10 '13 at 09:21
  • @nakosspy yes it may looks so easy. But technically to write it on java code confused me a little bit – elan Jun 10 '13 at 09:51

2 Answers2

1

You can convert the date into a calendar instance:

int groupByDays(List<Date> dateList, int dayToMatch) {   //dayToMatch can be defined as Calendar.TUESDAY or Calendar.MONDAY ...
    int counter = 0;
    for (Date date : dateList) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        if (day == dayToMatch)
           counter++;
        System.out.println("Day is : "+day); //This will return the day index compare with actual value to get the DAY in string format
    }
    return counter;
}

Indices equivalent to Day (This is picked directly from java.util.Calendar):

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Sunday.
 */
public final static int SUNDAY = 1;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Monday.
 */
public final static int MONDAY = 2;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Tuesday.
 */
public final static int TUESDAY = 3;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Wednesday.
 */
public final static int WEDNESDAY = 4;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Thursday.
 */
public final static int THURSDAY = 5;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Friday.
 */
public final static int FRIDAY = 6;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Saturday.
 */
public final static int SATURDAY = 7;

Using these info you can group the details.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
0

This should do it:

    SimpleDateFormat sdf =  new SimpleDateFormat("dd/MM/yyyy");
    ArrayList<Date> dates = new ArrayList<Date>();

    dates.add(sdf.parse("11/06/2013"));//Tuesday
    dates.add(sdf.parse("15/06/2013"));//Saturday
    dates.add(sdf.parse("17/06/2013"));//Monday
    dates.add(sdf.parse("18/06/2013"));//Tuesday
    dates.add(sdf.parse("22/06/2013"));//Saturday
    dates.add(sdf.parse("25/06/2013"));//Tuesday

    System.out.println(checkDay(dates , "Mon"));
    System.out.println(checkDay(dates , "Tue"));
    System.out.println(checkDay(dates , "Wed"));
    System.out.println(checkDay(dates , "Sat"));



public int checkDay(ArrayList<Date> list, String day)
{
    int count = 0;
    SimpleDateFormat sdf =  new SimpleDateFormat("EE");
    for(Date d : list)
    {
        if(sdf.format(d).equals(day))
            count++;
    }
    return count;
}

See it working here:

http://ideone.com/SdM5Tr

If you plan to do it multiple times however, you'd be better running it once and storing the counts in a hashmap:

    SimpleDateFormat sdf =  new SimpleDateFormat("dd/MM/yyyy");
    ArrayList<Date> dates = new ArrayList<Date>();

    dates.add(sdf.parse("11/06/2013"));//Tuesday
    dates.add(sdf.parse("15/06/2013"));//Saturday
    dates.add(sdf.parse("17/06/2013"));//Monday
    dates.add(sdf.parse("18/06/2013"));//Tuesday
    dates.add(sdf.parse("22/06/2013"));//Saturday
    dates.add(sdf.parse("25/06/2013"));//Tuesday

    HashMap counts = getCounts(dates);
    System.out.println(counts.get("Mon"));
    System.out.println(counts.get("Tue"));


public static HashMap getCounts(ArrayList<Date> list)
{
    HashMap counts = new HashMap();
    SimpleDateFormat sdf =  new SimpleDateFormat("EE");
    for(Date d : list)
    {
        String form = sdf.format(d);
        if(counts.containsKey(form))
            counts.put(form,((int)counts.get(form))+1);
        else
            counts.put(form,1);
    }
    return counts;
}

See it here:

http://ideone.com/SdM5Tr

MasNotsram
  • 2,105
  • 18
  • 28