按天名称确定日期计数

时间:2013-06-10 09:13:51

标签: java date

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

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

抱歉我的英文。因为它不是我的自然语言

2 个答案:

答案 0 :(得分: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;

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

答案 1 :(得分: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

如果你计划多次这样做,你最好运行一次并将计数存储在一个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;
}

在此处查看:

http://ideone.com/SdM5Tr