如何遍历Java中的date范围?
在我的脚本中,我需要通过date范围来执行一组操作,给定一个开始date和结束date。
请为我提供使用Java来实现这一目的的指导。
for ( currentDate = starDate; currentDate < endDate; currentDate++) { }
我知道上面的代码是根本不可能的,但我这样做是为了告诉你我想达到什么目的。
那么,你可以做这样的事情(使用Joda时间 )
for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1)) { ... }
我会彻底build议使用Joda时间在内置的Date
/ Calendar
类。
但是,为了完整性和/或如果您更喜欢API提供的设施,JodaTime是不错的select,下面是标准的API方法。
从下面的java.util.Date
实例开始时:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date startDate = formatter.parse("2010-12-20"); Date endDate = formatter.parse("2010-12-26");
如果您还不在Java8中,则使用传统的java.util.Calendar
方法:
Calendar start = Calendar.getInstance(); start.setTime(startDate); Calendar end = Calendar.getInstance(); end.setTime(endDate); for (Date date = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), date = start.getTime()) { // Do your job here with `date`. System.out.println(date); }
这里是Java8的java.time.LocalDate
方法,基本上就是JodaTime方法:
LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate end = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) { // Do your job here with `date`. System.out.println(date); }
如果您想迭代包含结束date,则分别使用!start.after(end)
和!date.isAfter(end)
。
Java 8风格,使用java.time类:
// Monday, February 29 is a leap day in 2016 (otherwise, February only has 28 days) LocalDate start = LocalDate.parse("2016-02-28"), end = LocalDate.parse("2016-03-02"); // 4 days between (end is inclusive in this example) Stream.iterate(start, date -> date.plusDays(1)) .limit(ChronoUnit.DAYS.between(start, end) + 1) .forEach(System.out::println);
输出:
2016-02-28 2016-02-29 2016-03-01 2016-03-02
替代scheme:
LocalDate next = start.minusDays(1); while ((next = next.plusDays(1)).isBefore(end.plusDays(1))) { System.out.println(next); }
这与BalusC给出的基本相同的答案,但是用一个while循环代替for循环可读性更好一些:
Calendar start = Calendar.getInstance(); start.setTime(startDate); Calendar end = Calendar.getInstance(); end.setTime(endDate); while( !start.after(end)){ Date targetDay = start.getTime(); // Do Work Here start.add(Calendar.DATE, 1); }
public static final void generateRange(final Date dateFrom, final Date dateTo) { final Calendar current = Calendar.getInstance(); current.setTime(dateFrom); while (!current.getTime().after(dateTo)) { // TODO current.add(Calendar.DATE, 1); } }
private static void iterateBetweenDates(Date startDate, Date endDate) { Calendar startCalender = Calendar.getInstance(); startCalender.setTime(startDate); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(endDate); for(; startCalender.compareTo(endCalendar)<=0; startCalender.add(Calendar.DATE, 1)) { // write your main logic here } }
为什么不使用时代和循环通过轻松。
long startDateEpoch = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(startDate).getTime() / 1000; long endDateEpoch = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(endDate).getTime() / 1000; long i; for(i=startDateEpoch ; i<=endDateEpoch; i+=86400){ System.out.println(i); }
Apache Commons
for (Date fromIter = fromDate; !fromIter.after(toDate); fromIter = DateUtils.addDays(fromIter, 1)) { // ... }
这将帮助您在30天前开始并循环到今天的date。 您可以轻松更改date和方向的范围。
private void iterateThroughDates() throws Exception { Calendar start = Calendar.getInstance(); start.add(Calendar.DATE, -30); Calendar end = Calendar.getInstance(); for (Calendar date = start; date.before(end); date.add(Calendar.DATE, 1)) { System.out.println(date.getTime()); } }
这是Java 8代码。 我认为这个代码将解决你的问题。 快乐的编码
LocalDate start = LocalDate.now(); LocalDate end = LocalDate.of(2016, 9, 1);//JAVA 9 release date Long duration = start.until(end, ChronoUnit.DAYS); System.out.println(duration); // Do Any stuff Here there after IntStream.iterate(0, i -> i + 1) .limit(duration) .forEach((i) -> {}); //old way of iteration for (int i = 0; i < duration; i++) System.out.print("" + i);// Do Any stuff Here