Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java检查一个月的天数并添加缺少的日期_Java - Fatal编程技术网

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

Java检查一个月的天数并添加缺少的日期,java,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-

嗨,我有以下的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-012010-08-022010-08-04-->我应该在2010-08-02和2010-08-04之间添加2010-08-03


谢谢

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

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;
}
publicstaticvoidmain(String…args)抛出异常{
字符串[]日期={“2010-08-01”、“2010-09-02”、“2010-07-28”、“2010-08-29”};
字符串[]日期2=填充日期(日期,“yyyy-MM-dd”);
System.out.println(Arrays.toString(dates2));
}
专用静态最终长毫单位/天=24L*3600*1000;
私有静态字符串[]FillingDates(字符串[]日期,字符串格式)引发异常{
如果(dates==null | | dates.length<1)返回日期;
SimpleDataFormat sdf=新的SimpleDataFormat(格式);
long min=long.MAX_值,MAX=long.min_值;
用于(字符串日期:日期){
longtime=sdf.parse(date.getTime();
如果(分钟>时间)分钟=时间;
如果(最大<时间)最大=时间;
}
字符串[]日期2=新字符串[(int)((max-min)/MILLIS_/天+1)];

对于(int i=0;i而言,最简单的方法是解析所有日期以找到最大值和最小值,然后生成最小值和最大值之间所有日期的列表

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;
}
publicstaticvoidmain(String…args)抛出异常{
字符串[]日期={“2010-08-01”、“2010-09-02”、“2010-07-28”、“2010-08-29”};
字符串[]日期2=填充日期(日期,“yyyy-MM-dd”);
System.out.println(Arrays.toString(dates2));
}
专用静态最终长毫单位/天=24L*3600*1000;
私有静态字符串[]FillingDates(字符串[]日期,字符串格式)引发异常{
如果(dates==null | | dates.length<1)返回日期;
SimpleDataFormat sdf=新的SimpleDataFormat(格式);
long min=long.MAX_值,MAX=long.min_值;
用于(字符串日期:日期){
longtime=sdf.parse(date.getTime();
如果(分钟>时间)分钟=时间;
如果(最大<时间)最大=时间;
}
字符串[]日期2=新字符串[(int)((max-min)/MILLIS_/天+1)];

对于(int i=0;i很高兴看到您使用ISO 8601。ISO 8601的最大优点是什么?没错:您可以通过对字符串排序来对日期进行排序

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

现在开始遍历您的列表。对于每个项目,选择年、月和日都很简单。每当您看到新的一年和一个月时,请将其记录为该月的最短日期。当您遍历数组时,您将看到可以填补的空白。当您从一年一个月过渡到另一年一个月时,您将能够记录该月的最长日期r这个月

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


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

很高兴看到您使用ISO 8601。ISO 8601最大的优点是什么?没错:您可以通过对字符串排序来对日期进行排序

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

现在开始遍历您的列表。对于每个项目,选择年、月和日都很简单。每当您看到新的一年和一个月时,请将其记录为该月的最短日期。当您遍历数组时,您将看到可以填补的空白。当您从一年一个月过渡到另一年一个月时,您将能够记录该月的最长日期r这个月

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

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

对于您的第一个任务:

    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)]++;
    }
List 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"
);
SimpleDataFormat dateFormat=新SimpleDataFormat(“yyyy-MM-dd”);
格里高利安日历=新的格里高利安日历();
ArrayList日期=新的ArrayList(strings.size());
for(字符串:字符串){
add(dateFormat.parse(string));
}
int[]countByMonth=新int[12];
用于(日期:日期){
日历。设置时间(日期);
countByMonth[calendar.get(calendar.MONTH)]++;
}
对于您的第一项任务:

    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)]++;
    }
List 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"
);
SimpleDataFormat dateFormat=新SimpleDataFormat(“yyyy-MM-dd”);
格里高利安日历=新的格里高利安日历();
ArrayList日期=新的ArrayList(strings.size());
用于(字符串)