0

我的 java 程序上有日期列表。我想创建一个方法来返回我在参数中传递的天数。

示例:我将字符串“Tue”传递给此方法。然后此方法返回 int 20。假设 int 20 是以星期二为日期名称的日期计数。

对不起我的英语不好。因为这不是我的自然语言

4

2 回答 2

1

您可以将日期转换为日历实例:

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;
}

等效于 Day 的索引(直接从 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;

使用这些信息,您可以对详细信息进行分组。

于 2013-06-10T09:40:24.183 回答
0

这应该这样做:

    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;
}

看到它在这里工作:

http://ideone.com/SdM5Tr

但是,如果您打算多次执行此操作,则最好运行一次并将计数存储在哈希图中:

    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;
}

在这里看到它:

http://ideone.com/SdM5Tr

于 2013-06-10T09:47:07.380 回答