Java存储数组中每年的每个日期

时间:2013-12-04 10:19:44

标签: java date calendar

我想创建每年的ArrayLists,我希望那一年中的每个日期都保存在相应的ArrayList中。我怎么能这样做?可以使用日历吗?

编辑:

所以现在我有了这个,有人知道它为什么只打印2013/12/31而不是那一年的所有日期?

Calendar cal = Calendar.getInstance();
int current = cal.get(Calendar.YEAR);
ArrayList<String> current_year = getDatesOfYear(current);

方法:

private ArrayList<String> getDatesOfYear(int year){
    ArrayList<String> dates = new ArrayList<String>();
    Calendar cal = Calendar.getInstance();
    cal.set(year+1,0,0);
    while(cal.get(Calendar.YEAR)==year){
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        String date = df.format(cal.getTime());
        dates.add(date);
        System.out.println(date);
        cal.add(Calendar.DATE, 1);
    }
    return dates;
}

2 个答案:

答案 0 :(得分:2)

编辑:

回复你的[编辑](我没有50个声誉直接在你的留言下写评论)

将日值设置为零实际上意味着(当前日期 - 1天)。如果你认为它确实有意义,因为在任何一个月都不会有第0天。

这可以按预期工作:

    cal.set(year,0,1);

不幸的是,Calendar.set()方法的javadoc不包含有关此案例的任何信息。但是我们可以运行几个测试来证明这一点:

    cal.set(2013, 1, 0); // 2013-FEB-01 - (1 DAY) = 2013-JAN-31
    cal.get(Calendar.YEAR); // 2013
    cal.get(Calendar.MONTH); // 0 = JAN
    cal.get(Calendar.DAY_OF_MONTH); // 31

    cal.set(2010, 0, 0); // 2010-JAN-01 - (1 DAY) = 2009-DEC-31
    cal.get(Calendar.YEAR); // 2009
    cal.get(Calendar.MONTH); // 11 = DEC
    cal.get(Calendar.DAY_OF_MONTH); // 31

所以你的代码实际上是有效的。只需将日期设置为1而不是0

我希望这有帮助!


上一个回答:

这应该有效。我添加了一些注释来帮助理解代码。这不是实现这一目标的最有效方法,但它非常易读。每年的日期都存储为字符串,但您可以修改代码以存储日历实例(可能不是一个好主意,因为它效率低下)。

    Map<Integer, ArrayList<String>> data = new LinkedHashMap<Integer, ArrayList<String>>();

    Calendar calendar = Calendar.getInstance();
    int year_from = 2000;
    int year_to = 2010;

    for (int year = year_from; year <= year_to; year++) {
        // make sure an array list for a given year is present
        if(! data.containsKey(year)) {
            data.put(year, new ArrayList<String>());
        }

        ArrayList<String> dates = data.get(year);

        for (int month = 1; month <= 12; month++) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month-1); // since 0 = January
            int maxDaysInMonth = calendar.getActualMaximum(Calendar.DATE);

            for (int day = 1; day <= maxDaysInMonth; day++) {
                // format date in such a way, that month and days are padded with zeroes up to 2 digits
                String dateAsString = String.format("%d-%02d-%02d", year, month, day);
                dates.add(dateAsString);
            }
        }
    }

    System.out.println(data.keySet()); // list of years present
    System.out.println(data.get(2003)); // last day of Feb: 28th
    System.out.println(data.get(2004)); // last day of Feb: 29th

控制台输出:

    /* OUTPUT:
     * 
    [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010]
    [2003-01-01, 2003-01-02, 2003-01-03, 2003-01-04, 2003-01-05, 2003-01-06, 2003-01-07, 2003-01-08, 2003-01-09, 2003-01-10, 2003-01-11, 2003-01-12, 2003-01-13, 2003-01-14, 2003-01-15, 2003-01-16, 2003-01-17, 2003-01-18, 2003-01-19, 2003-01-20, 2003-01-21, 2003-01-22, 2003-01-23, 2003-01-24, 2003-01-25, 2003-01-26, 2003-01-27, 2003-01-28, 2003-01-29, 2003-01-30, 2003-01-31, 2003-02-01, 2003-02-02, 2003-02-03, 2003-02-04, 2003-02-05, 2003-02-06, 2003-02-07, 2003-02-08, 2003-02-09, 2003-02-10, 2003-02-11, 2003-02-12, 2003-02-13, 2003-02-14, 2003-02-15, 2003-02-16, 2003-02-17, 2003-02-18, 2003-02-19, 2003-02-20, 2003-02-21, 2003-02-22, 2003-02-23, 2003-02-24, 2003-02-25, 2003-02-26, 2003-02-27, 2003-02-28, 2003-03-01, 2003-03-02, 2003-03-03, 2003-03-04, 2003-03-05, 2003-03-06, 2003-03-07, 2003-03-08, 2003-03-09, 2003-03-10, 2003-03-11, 2003-03-12, 2003-03-13, 2003-03-14, 2003-03-15, 2003-03-16, 2003-03-17, 2003-03-18, 2003-03-19, 2003-03-20, 2003-03-21, 2003-03-22, 2003-03-23, 2003-03-24, 2003-03-25, 2003-03-26, 2003-03-27, 2003-03-28, 2003-03-29, 2003-03-30, 2003-03-31, 2003-04-01, 2003-04-02, 2003-04-03, 2003-04-04, 2003-04-05, 2003-04-06, 2003-04-07, 2003-04-08, 2003-04-09, 2003-04-10, 2003-04-11, 2003-04-12, 2003-04-13, 2003-04-14, 2003-04-15, 2003-04-16, 2003-04-17, 2003-04-18, 2003-04-19, 2003-04-20, 2003-04-21, 2003-04-22, 2003-04-23, 2003-04-24, 2003-04-25, 2003-04-26, 2003-04-27, 2003-04-28, 2003-04-29, 2003-04-30, 2003-05-01, 2003-05-02, 2003-05-03, 2003-05-04, 2003-05-05, 2003-05-06, 2003-05-07, 2003-05-08, 2003-05-09, 2003-05-10, 2003-05-11, 2003-05-12, 2003-05-13, 2003-05-14, 2003-05-15, 2003-05-16, 2003-05-17, 2003-05-18, 2003-05-19, 2003-05-20, 2003-05-21, 2003-05-22, 2003-05-23, 2003-05-24, 2003-05-25, 2003-05-26, 2003-05-27, 2003-05-28, 2003-05-29, 2003-05-30, 2003-05-31, 2003-06-01, 2003-06-02, 2003-06-03, 2003-06-04, 2003-06-05, 2003-06-06, 2003-06-07, 2003-06-08, 2003-06-09, 2003-06-10, 2003-06-11, 2003-06-12, 2003-06-13, 2003-06-14, 2003-06-15, 2003-06-16, 2003-06-17, 2003-06-18, 2003-06-19, 2003-06-20, 2003-06-21, 2003-06-22, 2003-06-23, 2003-06-24, 2003-06-25, 2003-06-26, 2003-06-27, 2003-06-28, 2003-06-29, 2003-06-30, 2003-07-01, 2003-07-02, 2003-07-03, 2003-07-04, 2003-07-05, 2003-07-06, 2003-07-07, 2003-07-08, 2003-07-09, 2003-07-10, 2003-07-11, 2003-07-12, 2003-07-13, 2003-07-14, 2003-07-15, 2003-07-16, 2003-07-17, 2003-07-18, 2003-07-19, 2003-07-20, 2003-07-21, 2003-07-22, 2003-07-23, 2003-07-24, 2003-07-25, 2003-07-26, 2003-07-27, 2003-07-28, 2003-07-29, 2003-07-30, 2003-07-31, 2003-08-01, 2003-08-02, 2003-08-03, 2003-08-04, 2003-08-05, 2003-08-06, 2003-08-07, 2003-08-08, 2003-08-09, 2003-08-10, 2003-08-11, 2003-08-12, 2003-08-13, 2003-08-14, 2003-08-15, 2003-08-16, 2003-08-17, 2003-08-18, 2003-08-19, 2003-08-20, 2003-08-21, 2003-08-22, 2003-08-23, 2003-08-24, 2003-08-25, 2003-08-26, 2003-08-27, 2003-08-28, 2003-08-29, 2003-08-30, 2003-08-31, 2003-09-01, 2003-09-02, 2003-09-03, 2003-09-04, 2003-09-05, 2003-09-06, 2003-09-07, 2003-09-08, 2003-09-09, 2003-09-10, 2003-09-11, 2003-09-12, 2003-09-13, 2003-09-14, 2003-09-15, 2003-09-16, 2003-09-17, 2003-09-18, 2003-09-19, 2003-09-20, 2003-09-21, 2003-09-22, 2003-09-23, 2003-09-24, 2003-09-25, 2003-09-26, 2003-09-27, 2003-09-28, 2003-09-29, 2003-09-30, 2003-10-01, 2003-10-02, 2003-10-03, 2003-10-04, 2003-10-05, 2003-10-06, 2003-10-07, 2003-10-08, 2003-10-09, 2003-10-10, 2003-10-11, 2003-10-12, 2003-10-13, 2003-10-14, 2003-10-15, 2003-10-16, 2003-10-17, 2003-10-18, 2003-10-19, 2003-10-20, 2003-10-21, 2003-10-22, 2003-10-23, 2003-10-24, 2003-10-25, 2003-10-26, 2003-10-27, 2003-10-28, 2003-10-29, 2003-10-30, 2003-10-31, 2003-11-01, 2003-11-02, 2003-11-03, 2003-11-04, 2003-11-05, 2003-11-06, 2003-11-07, 2003-11-08, 2003-11-09, 2003-11-10, 2003-11-11, 2003-11-12, 2003-11-13, 2003-11-14, 2003-11-15, 2003-11-16, 2003-11-17, 2003-11-18, 2003-11-19, 2003-11-20, 2003-11-21, 2003-11-22, 2003-11-23, 2003-11-24, 2003-11-25, 2003-11-26, 2003-11-27, 2003-11-28, 2003-11-29, 2003-11-30, 2003-12-01, 2003-12-02, 2003-12-03, 2003-12-04, 2003-12-05, 2003-12-06, 2003-12-07, 2003-12-08, 2003-12-09, 2003-12-10, 2003-12-11, 2003-12-12, 2003-12-13, 2003-12-14, 2003-12-15, 2003-12-16, 2003-12-17, 2003-12-18, 2003-12-19, 2003-12-20, 2003-12-21, 2003-12-22, 2003-12-23, 2003-12-24, 2003-12-25, 2003-12-26, 2003-12-27, 2003-12-28, 2003-12-29, 2003-12-30, 2003-12-31]
    [2004-01-01, 2004-01-02, 2004-01-03, 2004-01-04, 2004-01-05, 2004-01-06, 2004-01-07, 2004-01-08, 2004-01-09, 2004-01-10, 2004-01-11, 2004-01-12, 2004-01-13, 2004-01-14, 2004-01-15, 2004-01-16, 2004-01-17, 2004-01-18, 2004-01-19, 2004-01-20, 2004-01-21, 2004-01-22, 2004-01-23, 2004-01-24, 2004-01-25, 2004-01-26, 2004-01-27, 2004-01-28, 2004-01-29, 2004-01-30, 2004-01-31, 2004-02-01, 2004-02-02, 2004-02-03, 2004-02-04, 2004-02-05, 2004-02-06, 2004-02-07, 2004-02-08, 2004-02-09, 2004-02-10, 2004-02-11, 2004-02-12, 2004-02-13, 2004-02-14, 2004-02-15, 2004-02-16, 2004-02-17, 2004-02-18, 2004-02-19, 2004-02-20, 2004-02-21, 2004-02-22, 2004-02-23, 2004-02-24, 2004-02-25, 2004-02-26, 2004-02-27, 2004-02-28, 2004-02-29, 2004-03-01, 2004-03-02, 2004-03-03, 2004-03-04, 2004-03-05, 2004-03-06, 2004-03-07, 2004-03-08, 2004-03-09, 2004-03-10, 2004-03-11, 2004-03-12, 2004-03-13, 2004-03-14, 2004-03-15, 2004-03-16, 2004-03-17, 2004-03-18, 2004-03-19, 2004-03-20, 2004-03-21, 2004-03-22, 2004-03-23, 2004-03-24, 2004-03-25, 2004-03-26, 2004-03-27, 2004-03-28, 2004-03-29, 2004-03-30, 2004-03-31, 2004-04-01, 2004-04-02, 2004-04-03, 2004-04-04, 2004-04-05, 2004-04-06, 2004-04-07, 2004-04-08, 2004-04-09, 2004-04-10, 2004-04-11, 2004-04-12, 2004-04-13, 2004-04-14, 2004-04-15, 2004-04-16, 2004-04-17, 2004-04-18, 2004-04-19, 2004-04-20, 2004-04-21, 2004-04-22, 2004-04-23, 2004-04-24, 2004-04-25, 2004-04-26, 2004-04-27, 2004-04-28, 2004-04-29, 2004-04-30, 2004-05-01, 2004-05-02, 2004-05-03, 2004-05-04, 2004-05-05, 2004-05-06, 2004-05-07, 2004-05-08, 2004-05-09, 2004-05-10, 2004-05-11, 2004-05-12, 2004-05-13, 2004-05-14, 2004-05-15, 2004-05-16, 2004-05-17, 2004-05-18, 2004-05-19, 2004-05-20, 2004-05-21, 2004-05-22, 2004-05-23, 2004-05-24, 2004-05-25, 2004-05-26, 2004-05-27, 2004-05-28, 2004-05-29, 2004-05-30, 2004-05-31, 2004-06-01, 2004-06-02, 2004-06-03, 2004-06-04, 2004-06-05, 2004-06-06, 2004-06-07, 2004-06-08, 2004-06-09, 2004-06-10, 2004-06-11, 2004-06-12, 2004-06-13, 2004-06-14, 2004-06-15, 2004-06-16, 2004-06-17, 2004-06-18, 2004-06-19, 2004-06-20, 2004-06-21, 2004-06-22, 2004-06-23, 2004-06-24, 2004-06-25, 2004-06-26, 2004-06-27, 2004-06-28, 2004-06-29, 2004-06-30, 2004-07-01, 2004-07-02, 2004-07-03, 2004-07-04, 2004-07-05, 2004-07-06, 2004-07-07, 2004-07-08, 2004-07-09, 2004-07-10, 2004-07-11, 2004-07-12, 2004-07-13, 2004-07-14, 2004-07-15, 2004-07-16, 2004-07-17, 2004-07-18, 2004-07-19, 2004-07-20, 2004-07-21, 2004-07-22, 2004-07-23, 2004-07-24, 2004-07-25, 2004-07-26, 2004-07-27, 2004-07-28, 2004-07-29, 2004-07-30, 2004-07-31, 2004-08-01, 2004-08-02, 2004-08-03, 2004-08-04, 2004-08-05, 2004-08-06, 2004-08-07, 2004-08-08, 2004-08-09, 2004-08-10, 2004-08-11, 2004-08-12, 2004-08-13, 2004-08-14, 2004-08-15, 2004-08-16, 2004-08-17, 2004-08-18, 2004-08-19, 2004-08-20, 2004-08-21, 2004-08-22, 2004-08-23, 2004-08-24, 2004-08-25, 2004-08-26, 2004-08-27, 2004-08-28, 2004-08-29, 2004-08-30, 2004-08-31, 2004-09-01, 2004-09-02, 2004-09-03, 2004-09-04, 2004-09-05, 2004-09-06, 2004-09-07, 2004-09-08, 2004-09-09, 2004-09-10, 2004-09-11, 2004-09-12, 2004-09-13, 2004-09-14, 2004-09-15, 2004-09-16, 2004-09-17, 2004-09-18, 2004-09-19, 2004-09-20, 2004-09-21, 2004-09-22, 2004-09-23, 2004-09-24, 2004-09-25, 2004-09-26, 2004-09-27, 2004-09-28, 2004-09-29, 2004-09-30, 2004-10-01, 2004-10-02, 2004-10-03, 2004-10-04, 2004-10-05, 2004-10-06, 2004-10-07, 2004-10-08, 2004-10-09, 2004-10-10, 2004-10-11, 2004-10-12, 2004-10-13, 2004-10-14, 2004-10-15, 2004-10-16, 2004-10-17, 2004-10-18, 2004-10-19, 2004-10-20, 2004-10-21, 2004-10-22, 2004-10-23, 2004-10-24, 2004-10-25, 2004-10-26, 2004-10-27, 2004-10-28, 2004-10-29, 2004-10-30, 2004-10-31, 2004-11-01, 2004-11-02, 2004-11-03, 2004-11-04, 2004-11-05, 2004-11-06, 2004-11-07, 2004-11-08, 2004-11-09, 2004-11-10, 2004-11-11, 2004-11-12, 2004-11-13, 2004-11-14, 2004-11-15, 2004-11-16, 2004-11-17, 2004-11-18, 2004-11-19, 2004-11-20, 2004-11-21, 2004-11-22, 2004-11-23, 2004-11-24, 2004-11-25, 2004-11-26, 2004-11-27, 2004-11-28, 2004-11-29, 2004-11-30, 2004-12-01, 2004-12-02, 2004-12-03, 2004-12-04, 2004-12-05, 2004-12-06, 2004-12-07, 2004-12-08, 2004-12-09, 2004-12-10, 2004-12-11, 2004-12-12, 2004-12-13, 2004-12-14, 2004-12-15, 2004-12-16, 2004-12-17, 2004-12-18, 2004-12-19, 2004-12-20, 2004-12-21, 2004-12-22, 2004-12-23, 2004-12-24, 2004-12-25, 2004-12-26, 2004-12-27, 2004-12-28, 2004-12-29, 2004-12-30, 2004-12-31]
     *
     */

答案 1 :(得分:0)

这非常简单 - 您只需要添加日期(是的,您可以使用日历)。

考虑到这一点,你知道每年的第一天是1月1日,你知道这些日子在一年中单调进行。所以在伪代码中:

date = Jan 1st $year
while (date.year == $year)
    result.add(date)
    date = addOneDay(date)