Example usage for org.joda.time Weeks weeksBetween

List of usage examples for org.joda.time Weeks weeksBetween

Introduction

In this page you can find the example usage for org.joda.time Weeks weeksBetween.

Prototype

public static Weeks weeksBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Weeks representing the number of whole weeks between the two specified partial datetimes.

Usage

From source file:influent.server.utilities.DateRangeBuilder.java

License:MIT License

public static FL_DateRange getBigChartDateRange(DateTime startDate, DateTime endDate) {
    if (startDate == null || endDate == null) {
        return null;
    }//from w  w w .  ja va2s  .c o m

    Days days = Days.daysBetween(startDate, endDate);
    if (days.getDays() <= 14) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.DAYS, 14L);
    }
    if (days.getDays() <= 30) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.DAYS, 30L);
    }
    if (days.getDays() <= 60) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.DAYS, 60L);
    }
    Weeks weeks = Weeks.weeksBetween(startDate, endDate);
    if (weeks.getWeeks() <= 16) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.DAYS, 112L);
    }
    if (weeks.getWeeks() <= 32) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.WEEKS, 32L);
    }
    Months months = Months.monthsBetween(startDate, endDate);
    if (months.getMonths() <= 12) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.WEEKS, 52);
    }
    if (months.getMonths() <= 16) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.WEEKS, 70L);
    }
    if (months.getMonths() <= 24) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 24L);
    }
    if (months.getMonths() <= 32) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 32L);
    }
    Years years = Years.yearsBetween(startDate, endDate);
    if (years.getYears() <= 4) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 48L);
    }
    if (years.getYears() <= 5) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 60L);
    }
    if (years.getYears() <= 6) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 72L);
    }
    if (years.getYears() <= 7) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 84L);
    }
    if (years.getYears() <= 8) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.MONTHS, 96L);
    }
    if (years.getYears() <= 16) {
        return new ConstrainedDateRange(startDate, FL_DateInterval.QUARTERS, 64L);
    }

    throw new RuntimeException("Unsupported chart date range: " + startDate + " to " + endDate);
}

From source file:influent.server.utilities.DateRangeBuilder.java

License:MIT License

public static int determineInterval(DateTime date, DateTime startDate, FL_DateInterval interval,
        int numIntervalsPerBin) {
    switch (interval) {
    case SECONDS:
        Seconds seconds = Seconds.secondsBetween(startDate, date);
        return seconds.getSeconds() / numIntervalsPerBin;
    case HOURS://  w w w  . ja  va 2s  .  co m
        Hours hours = Hours.hoursBetween(startDate, date);
        return hours.getHours() / numIntervalsPerBin;
    case DAYS:
        Days days = Days.daysBetween(startDate, date);
        return days.getDays() / numIntervalsPerBin;
    case WEEKS:
        Weeks weeks = Weeks.weeksBetween(startDate, date);
        return weeks.getWeeks() / numIntervalsPerBin;
    case MONTHS:
        Months months = Months.monthsBetween(startDate, date);
        return months.getMonths() / numIntervalsPerBin;
    case QUARTERS:
        months = Months.monthsBetween(startDate, date);
        return months.getMonths() / 3 / numIntervalsPerBin;
    case YEARS:
        Years years = Years.yearsBetween(startDate, date);
        return years.getYears() / numIntervalsPerBin;
    }
    return 0;
}

From source file:it.webappcommon.lib.DateUtils.java

License:Open Source License

public static int differenzaInSettimane(Date start, Date end) {
    int res = 0;/*  w  ww .ja va  2  s .c o  m*/

    DateTime startDate = new DateTime(start);
    DateTime endDate = new DateTime(end);

    Weeks settimane = Weeks.weeksBetween(startDate, endDate);
    res = settimane.getWeeks();

    return res;
}

From source file:net.sf.jasperreports.functions.standard.DateTimeFunctions.java

License:Open Source License

/**
 * Returns the number of weeks between two dates.
 *///from www  .j a  v a  2  s  .  c om
@Function("WEEKS")
@FunctionParameters({ @FunctionParameter("startDate"), @FunctionParameter("endDate") })
public Integer WEEKS(Object startDate, Object endDate) {
    Date startDateObj = convertDateObject(startDate);
    if (startDateObj == null) {
        logCannotConvertToDate();
        return null;
    }
    Date endDateObj = convertDateObject(endDate);
    if (endDateObj == null) {
        logCannotConvertToDate();
        return null;
    } else {
        DateTime dt1 = new DateTime(startDateObj);
        DateTime dt2 = new DateTime(endDateObj);
        return Weeks.weeksBetween(dt1, dt2).getWeeks();
    }
}

From source file:net.sourceforge.fenixedu.dataTransferObject.InfoLessonInstanceAggregation.java

License:Open Source License

public SortedSet<Integer> getWeeks(final Interval lessonInterval) {
    final SortedSet<Integer> weeks = new TreeSet<Integer>();
    final LocalDate firstPossibleLessonDay = lessonInterval.getStart().toLocalDate();
    for (final LocalDate localDate : dates) {
        final Integer week = Weeks.weeksBetween(firstPossibleLessonDay, localDate).getWeeks() + 1;
        weeks.add(week);//from  ww w .  ja v  a  2  s  .c o  m
    }
    return weeks;
}

From source file:net.sourceforge.fenixedu.domain.Lesson.java

License:Open Source License

public String getOccurrenceWeeksAsString() {
    final SortedSet<Integer> weeks = new TreeSet<Integer>();

    final ExecutionCourse executionCourse = getExecutionCourse();
    final YearMonthDay firstPossibleLessonDay = executionCourse.getMaxLessonsPeriod().getLeft();
    final YearMonthDay lastPossibleLessonDay = executionCourse.getMaxLessonsPeriod().getRight();
    for (final Interval interval : getAllLessonIntervals()) {
        final Integer week = Weeks.weeksBetween(firstPossibleLessonDay, interval.getStart().toLocalDate())
                .getWeeks() + 1;//from w  w w  . j a  v a 2 s .c o m
        weeks.add(week);
    }

    final StringBuilder builder = new StringBuilder();
    final Integer[] weeksA = weeks.toArray(new Integer[0]);
    for (int i = 0; i < weeksA.length; i++) {
        if (i == 0) {
            builder.append(weeksA[i]);
        } else if (i == weeksA.length - 1 || (weeksA[i]) + 1 != (weeksA[i + 1])) {
            final String seperator = (weeksA[i - 1]) + 1 == (weeksA[i]) ? " - " : ", ";
            builder.append(seperator);
            builder.append(weeksA[i]);
        } else if ((weeksA[i - 1]) + 1 != weeksA[i]) {
            builder.append(", ");
            builder.append(weeksA[i]);
        }
    }
    return builder.toString();
}

From source file:org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java

License:Apache License

public BigDecimal calculatePeriodsBetweenDates(final LocalDate startDate, final LocalDate endDate) {
    BigDecimal numberOfPeriods = BigDecimal.ZERO;
    switch (this.repaymentPeriodFrequencyType) {
    case DAYS:// www  .jav a2  s .c  om
        int numberOfDays = Days.daysBetween(startDate, endDate).getDays();
        numberOfPeriods = BigDecimal.valueOf((double) numberOfDays);
        break;
    case WEEKS:
        int numberOfWeeks = Weeks.weeksBetween(startDate, endDate).getWeeks();
        int daysLeftAfterWeeks = Days.daysBetween(startDate.plusWeeks(numberOfWeeks), endDate).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfWeeks))
                .add(BigDecimal.valueOf((double) daysLeftAfterWeeks / 7));
        break;
    case MONTHS:
        int numberOfMonths = Months.monthsBetween(startDate, endDate).getMonths();
        LocalDate startDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths);
        LocalDate endDateAfterConsideringMonths = startDate.plusMonths(numberOfMonths + 1);
        int daysLeftAfterMonths = Days.daysBetween(startDateAfterConsideringMonths, endDate).getDays();
        int daysInPeriodAfterMonths = Days
                .daysBetween(startDateAfterConsideringMonths, endDateAfterConsideringMonths).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfMonths))
                .add(BigDecimal.valueOf((double) daysLeftAfterMonths / daysInPeriodAfterMonths));
        break;
    case YEARS:
        int numberOfYears = Years.yearsBetween(startDate, endDate).getYears();
        LocalDate startDateAfterConsideringYears = startDate.plusYears(numberOfYears);
        LocalDate endDateAfterConsideringYears = startDate.plusYears(numberOfYears + 1);
        int daysLeftAfterYears = Days.daysBetween(startDateAfterConsideringYears, endDate).getDays();
        int daysInPeriodAfterYears = Days
                .daysBetween(startDateAfterConsideringYears, endDateAfterConsideringYears).getDays();
        numberOfPeriods = numberOfPeriods.add(BigDecimal.valueOf(numberOfYears))
                .add(BigDecimal.valueOf((double) daysLeftAfterYears / daysInPeriodAfterYears));
        break;
    default:
        break;
    }
    return numberOfPeriods;
}

From source file:org.davidmendoza.esu.service.impl.InicioServiceImpl.java

License:Open Source License

@Cacheable(value = "diaCache")
@Transactional(readOnly = true)//  ww w  .j  a v  a2  s .  c o m
@Override
public Inicio inicio(Calendar hoy) {
    log.debug("Hoy: {}", hoy.getTime());

    if (hoy.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && hoy.get(Calendar.HOUR_OF_DAY) < 12) {
        hoy.add(Calendar.DAY_OF_WEEK, -1);
    }

    Trimestre trimestre = trimestreService.obtiene(hoy.getTime());

    if (trimestre != null) {
        DateTime a = new DateTime(trimestre.getInicia());
        DateTime b = new DateTime(hoy);
        Weeks c = Weeks.weeksBetween(a, b);
        int weeks = c.getWeeks() + 1;
        log.debug("Weeks: {}", weeks);
        StringBuilder leccion = new StringBuilder();
        leccion.append("l").append(dosDigitos.format(weeks));

        Inicio inicio = new Inicio();
        inicio.setAnio(trimestre.getNombre().substring(0, 4));
        inicio.setTrimestre(trimestre.getNombre().substring(4));
        inicio.setLeccion(leccion.toString());
        inicio.setDia(obtieneDia(hoy.get(Calendar.DAY_OF_WEEK)));
        return inicio;
    } else {
        return null;
    }

}

From source file:org.davidmendoza.esu.service.impl.InicioServiceImpl.java

License:Open Source License

@Override
public Inicio ayer(Inicio inicio) {
    Inicio ayer = new Inicio();
    Integer anio = new Integer(inicio.getAnio());
    String trimestre = inicio.getTrimestre();
    String leccion = inicio.getLeccion();
    String dia = inicio.getDia();
    if (StringUtils.isBlank(dia)) {
        dia = obtieneDia(new GregorianCalendar(Locale.ENGLISH).get(Calendar.DAY_OF_WEEK));
    }//from w ww  .  ja va 2s.  c o  m
    Trimestre t = trimestreService.obtiene(anio + trimestre);
    if (t != null) {
        try {
            Calendar cal = new GregorianCalendar(Locale.ENGLISH);
            cal.setTime(t.getInicia());
            cal.add(Calendar.SECOND, 1);
            cal.set(Calendar.DAY_OF_WEEK, obtieneDia(dia));
            int weeks = ((Long) nf.parse(leccion.substring(1))).intValue();
            if (dia.equals("sabado")) {
                weeks--;
            }
            cal.add(Calendar.WEEK_OF_YEAR, weeks);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            Date hoy = cal.getTime();

            ayer.setHoy(hoy);

            t = trimestreService.obtiene(hoy);

            DateTime a = new DateTime(t.getInicia());
            DateTime b = new DateTime(hoy);

            Weeks c = Weeks.weeksBetween(a, b);
            weeks = c.getWeeks();
            weeks++;
            leccion = "l" + dosDigitos.format(weeks);
            ayer.setAnio(t.getNombre().substring(0, 4));
            ayer.setTrimestre(t.getNombre().substring(4));
            ayer.setLeccion(leccion);
            ayer.setDia(obtieneDia(cal.get(Calendar.DAY_OF_WEEK)));

            return ayer;
        } catch (ParseException e) {
            log.error("No pude poner la fecha de ayer", e);
        }
    }
    return null;
}

From source file:org.davidmendoza.esu.service.impl.InicioServiceImpl.java

License:Open Source License

@Override
public Inicio manana(Inicio inicio) {
    Inicio manana = new Inicio();
    Integer anio = new Integer(inicio.getAnio());
    String trimestre = inicio.getTrimestre();
    String leccion = inicio.getLeccion();
    String dia = inicio.getDia();
    if (StringUtils.isBlank(dia)) {
        dia = obtieneDia(new GregorianCalendar(Locale.ENGLISH).get(Calendar.DAY_OF_WEEK));
    }/*from   w  ww.  j a  v a  2s  .co  m*/
    Trimestre t = trimestreService.obtiene(anio + trimestre);
    if (t != null) {
        try {
            Calendar cal = new GregorianCalendar(Locale.ENGLISH);
            cal.setTime(t.getInicia());
            cal.add(Calendar.SECOND, 1);
            cal.set(Calendar.DAY_OF_WEEK, obtieneDia(dia));
            int weeks = ((Long) nf.parse(leccion.substring(1))).intValue();
            if (dia.equals("sabado")) {
                weeks--;
            }
            cal.add(Calendar.WEEK_OF_YEAR, weeks);
            cal.add(Calendar.DAY_OF_MONTH, +1);
            Date hoy = cal.getTime();

            manana.setHoy(hoy);

            t = trimestreService.obtiene(hoy);

            DateTime a = new DateTime(t.getInicia());
            DateTime b = new DateTime(hoy);

            Weeks c = Weeks.weeksBetween(a, b);
            weeks = c.getWeeks();
            weeks++;
            leccion = "l" + dosDigitos.format(weeks);
            manana.setAnio(t.getNombre().substring(0, 4));
            manana.setTrimestre(t.getNombre().substring(4));
            manana.setLeccion(leccion);
            manana.setDia(obtieneDia(cal.get(Calendar.DAY_OF_WEEK)));

            return manana;
        } catch (ParseException e) {
            log.error("No pude poner la fecha de manana", e);
        }
    }
    return null;
}