java 已知两个日期,遍历出两个日期之间所有的日期,重点是::包括第一个日期!!
迷茫
迷茫 2017-04-18 10:30:22
0
5
700

String timeStart = "2016-12-11";
String timeEnd = "2016-12-20";

已知两个日期,怎么循环出 [timeStart - timeEnd] 的所有日期,包括开始和结束时间。
并存入一个List<String>的集合中

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(5)
PHPzhong

Haha, it’s a date problem again, then I definitely recommend you to use the new time API of Java 8, and your time string is still in this formatyyyy-MM-dd,直接LocalDate.parseThe method can convert the string into a LocalDate object

Furthermore, if this involves a series of regular time collections, you must consider Stream. It is very convenient and fast to use Stream to construct your collection. The following is the sample code:

   /**
     * 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
     * @param timeStart
     * @param timeEnd
     * @return
     */
    public static List<String> collectLocalDates(String timeStart, String timeEnd){
        return collectLocalDates(LocalDate.parse(timeStart), LocalDate.parse(timeEnd));
    }

    /**
     * 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
     * @param start
     * @param end
     * @return
     */
    public static List<String> collectLocalDates(LocalDate start, LocalDate end){
        // 用起始时间作为流的源头,按照每次加一天的方式创建一个无限流
        return Stream.iterate(start, localDate -> localDate.plusDays(1))
                     // 截断无限流,长度为起始时间和结束时间的差+1个
                     .limit(ChronoUnit.DAYS.between(start, end) + 1)
                     // 由于最后要的是字符串,所以map转换一下
                     .map(LocalDate::toString)
                     // 把流收集为List
                     .collect(Collectors.toList());
    }

Then the test code:

        String timeStart = "2016-12-11";
        String timeEnd = "2016-12-20";

        collectLocalDates(timeStart, timeEnd).forEach(System.out::println);

The following is the print result:

Easy to use~~Perfectly elegant and easy to understand Java8~Haha

阿神

You calculate the number of days difference between two dates and add 1 in the loop. If the added date is equal to the later date, it will be fine

洪涛
 思路就是这样:
 public static List<Integer> getYear(int startYear,int endYear) {
        List<Integer> years = new ArrayList<Integer>();
        while (startYear <= endYear) {
            years.add(startYear);
            startYear++;
        }
        return years;
    }
Ty80

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    try {
        Date begin = sdf.parse("2016-12-11");
        Date end = sdf.parse("2016-12-20");
        List<String> l = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        while (begin.before(end)) {
            l.add(sdf.format(begin));
            cal.setTime(begin);
            cal.add(Calendar.DAY_OF_MONTH, 1);
            begin = cal.getTime();
        }
        for(String s:l){
            System.out.println(s);
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
伊谢尔伦

You need Apache’s lang package, which has the following API

static Date addDays(Date date, int amount) Returns a new Date object after adding amount days to a date time object
static Date addHours(Date date, int amount) Returns a new Date object after adding amount h to a date time object
static Date addMilliseconds(Date date, int amount) Returns a new Date object after adding amount milliseconds to a date time object
static Date addMinutes(Date date, int amount) Returns a new Date object after adding amount minutes to a date time object
static Date addMonths(Date date, int amount) Returns a new Date object after adding amount months to a date time object
static Date addSeconds(Date date, int amount) Returns a new Date object after adding amount seconds to a date time object
static Date addWeeks(Date date, int amount) Returns a new Date object after adding amount weeks to a date time object
static Date addYears(Date date, int amount) Returns a new Date object after adding amount years to a date time object

Article address

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template