Java检查一个月中没有天数并添加缺少的日期

时间:2011-07-30 09:05:37

标签: java

您好我有以下arraylist日期字符串

[ “2010-08-01”, “2010-08-02”, “2010-08-04”, “2010-08-05”, “2010-08-06”,“2010-08-07 ”, “2010-08-08”, “2010-08-09”, “2010-08-11”, “2010-08-12”, “2010-08-13”, “2010-08-14”, “2010-08-15”, “2010-08-17”, “2010-08-18”, “2010-08-20”, “2010-08-21”, “2010-08-26”,“2010 -08-28" , “2010-08-29”]

我有两个目标,我希望实现,

1)如何根据上面的年份和月份获得每个月的天数? 2)如何在上面的日期字符串的整个序列中添加缺少的日期(与上面相同的格式)?例如:2010-08-01,2010-08-02,2010-08-04 - >我应该在2010-08-02和2010-08-04之间添加2010-08-03

谢谢!

3 个答案:

答案 0 :(得分:1)

最简单的方法是解析所有日期以找到最大值和最小值。然后生成最小值和最大值之间所有日期的列表。

public static void main(String... args) throws ParseException {
    String[] dates = { "2010-08-01","2010-09-02","2010-07-28","2010-08-29" };
    String[] dates2 = fillInDates(dates, "yyyy-MM-dd");
    System.out.println(Arrays.toString(dates2));
}

private static final long MILLIS_PER_DAY = 24L * 3600 * 1000;

private static String[] fillInDates(String[] dates, String format) throws ParseException {
    if (dates == null || dates.length < 1) return dates;

    SimpleDateFormat sdf = new SimpleDateFormat(format);
    long min = Long.MAX_VALUE, max = Long.MIN_VALUE;
    for (String date : dates) {
        long time = sdf.parse(date).getTime();
        if(min > time) min = time;
        if(max < time) max = time;
    }
    String[] dates2 = new String[(int) ((max - min)/MILLIS_PER_DAY+1)];
    for(int i=0;i<dates2.length;i++)
        dates2[i] = sdf.format(new Date(min + i * MILLIS_PER_DAY));
    return dates2;
}

打印

[2010-07-28,2010-07-29,2010-07-30,2010-07-31,2010-08-01,2010-08-08,2010-08-08,2010-08- 04,2010-08-05,2010-08-06,2010-08-08,2010-08-08,2010-08-08,2010-08-08,2010-08-08,2010-08-08, 2010-08-13,2010-08-14,2010-08-15,2010-08-16,2010-08-17,2010-08-18,2010-08-08,2010-08-08,2010- 08-21,2010-08-22,2010-08-23,2010-08-24,2010-08-25,2010-08-08,2010-08-08,2010-08-08,20108-08- 29,2010-08-30,2010-08-31,2010-09-01,2010-09-02]

答案 1 :(得分:1)

很高兴看到您使用ISO 8601. ISO 8601的最大优势之一是什么?没错:你可以通过排序字符串来排序日期。

因此,在您的情况下,首先对您的数组进行排序(或进行复制并对其进行排序)。字符串排序将起作用。

现在开始遍历您的列表。对于每个项目,挑选年,月和日是微不足道的。每当您看到年份和月份时,请将其记录为当月的最低日期。当你走过阵列时,你会看到你可以填补的空白。当你从一个月过渡到另一个月时,你将能够记录当月的最大日期。

那里有一些簿记,你不能轻易地插入到数组的中间,所以如果我是你,我会将你的结果生成一个新的数组对象。

这需要几行代码,但请记住利用ISO 8601中字符串排序是日期排序的方式。

答案 2 :(得分:1)

完成第一项任务:

    List<String> strings = Arrays.asList(
            "2010-08-01", "2010-08-02", "2010-08-04", "2010-08-05", "2010-08-06", "2010-08-07", "2010-08-08",
            "2010-08-09", "2010-08-11", "2010-08-12", "2010-08-13", "2010-08-14", "2010-08-15", "2010-08-17",
            "2010-08-18", "2010-08-20", "2010-08-21", "2010-08-26", "2010-08-28", "2010-08-29"
    );

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    GregorianCalendar calendar = new GregorianCalendar();


    ArrayList<Date> dates = new ArrayList<Date>(strings.size());

    for (String string : strings) {
        dates.add(dateFormat.parse(string));
    }

    int[] countByMonth = new int[12];
    for (Date date : dates) {
        calendar.setTime(date);
        countByMonth[calendar.get(Calendar.MONTH)]++;
    }