Skip to content

Instantly share code, notes, and snippets.

@iskigow
Created January 17, 2014 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iskigow/8473001 to your computer and use it in GitHub Desktop.
Save iskigow/8473001 to your computer and use it in GitHub Desktop.
Example of week intervals generation in Gregorian model using JodaTime and Guava;
package br.com.tecsinapse.examples;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.LocalDate;
import org.joda.time.YearMonth;
import com.google.common.base.Objects;
import com.google.common.collect.FluentIterable;
public class Example {
public static class Period implements Serializable {
private static final long serialVersionUID = 1L;
private LocalDate begin;
private LocalDate end;
public Period(LocalDate begin, LocalDate end) {
this.begin = begin;
this.end = end;
}
public LocalDate getBegin() {
return begin;
}
public void setBegin(LocalDate begin) {
this.begin = begin;
}
public LocalDate getEnd() {
return end;
}
public void setEnd(LocalDate end) {
this.end = end;
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("begin", getBegin())
.add("end", getEnd()).toString();
}
}
public static void main(String[] args) {
for (int month = 1; month < 13 ; month++) {
YearMonth yearMonth = new YearMonth(2014,month);
List<Period> periodos = new ArrayList<>();
LocalDate firstDayMonth = yearMonth.toLocalDate(1);
LocalDate lastDayMonth = firstDayMonth.dayOfMonth().withMaximumValue();
LocalDate firstDay = firstDayMonth;
LocalDate lastDay = firstDayMonth;
while (lastDay.isBefore(lastDayMonth)) {
lastDay = firstDay.plusDays(1).dayOfWeek().withMaximumValue().minusDays(1);
Period periodo = new Period(firstDay, lastDay);
periodos.add(periodo);
firstDay = lastDay.plusDays(1);
}
FluentIterable.from(periodos).last().get().setEnd(lastDayMonth);
System.out.println(periodos.size());
for (Period periodo : periodos) {
System.out.println(periodo);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment