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:org.medcada.android.db.DatabaseHandler.java

License:Open Source License

public List<String> getGlucoseDatetimesByWeek() {
    JodaTimeAndroid.init(mContext);/*w ww  .  j a va  2 s . c  om*/

    DateTime maxDateTime = new DateTime(realm.where(GlucoseReading.class).maximumDate("created").getTime());
    DateTime minDateTime = new DateTime(realm.where(GlucoseReading.class).minimumDate("created").getTime());

    DateTime currentDateTime = minDateTime;
    DateTime newDateTime = minDateTime;

    ArrayList<String> finalWeeks = new ArrayList<String>();

    // The number of weeks is at least 1 since we do have average for the current week even if incomplete
    int weeksNumber = Weeks.weeksBetween(minDateTime, maxDateTime).getWeeks() + 1;

    DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    for (int i = 0; i < weeksNumber; i++) {
        newDateTime = currentDateTime.plusWeeks(1);
        finalWeeks.add(inputFormat.format(newDateTime.toDate()));
        currentDateTime = newDateTime;
    }
    return finalWeeks;
}

From source file:org.openmrs.module.kenyaemr.calculation.library.mchcs.NeedsPcrTestCalculation.java

License:Open Source License

Integer getAge(Date birtDate, Date context) {
    DateTime d1 = new DateTime(birtDate.getTime());
    DateTime d2 = new DateTime(context.getTime());
    return Weeks.weeksBetween(d1, d2).getWeeks();
}

From source file:org.openmrs.module.kenyaemr.calculation.library.mchms.LateEnrollmentCalculation.java

License:Open Source License

/**
 * @return true if the given patient's gestation at enrollment was greater than 28 weeks at enrollment and false
 * otherwise./*from  w w w  .  j av a2s . c  o m*/
 * */
protected boolean gestationAtEnrollmentWasGreaterThan28Weeks(Integer patientId, CalculationResultMap crm) {
    Encounter lastMchEnrollment = (Encounter) crm.get(patientId).getValue();
    EncounterWrapper wrapper = new EncounterWrapper(lastMchEnrollment);

    Obs lmpObs = wrapper.firstObs(Dictionary.getConcept(Dictionary.LAST_MONTHLY_PERIOD));
    if (lmpObs != null) {
        Weeks weeks = Weeks.weeksBetween(new DateTime(lmpObs.getValueDate()),
                new DateTime(lastMchEnrollment.getDateCreated()));
        if (weeks.getWeeks() > 28) {
            return true;
        }
    }
    return false;
}

From source file:org.openmrs.module.kenyaemr.calculation.library.mchms.NotOnArtCalculation.java

License:Open Source License

/**
 * @return true if the given patient's gestation is greater than 14 weeks and false otherwise
 *//*from   w  ww. jav  a 2 s  .co  m*/
protected boolean gestationIsGreaterThan14Weeks(Date lmpDate) {
    if (lmpDate != null) {
        Weeks weeks = Weeks.weeksBetween(new DateTime(lmpDate), new DateTime(new Date()));
        if (weeks.getWeeks() > 14) {
            return true;
        }
    }
    return false;
}

From source file:org.openmrs.module.kenyaemr.fragment.controller.program.mchms.MchmsCarePanelFragmentController.java

License:Open Source License

public void controller(@FragmentParam("patient") Patient patient, @FragmentParam("complete") Boolean complete,
        FragmentModel model) {//from   w  ww .  j a v a 2  s.  c om
    Map<String, Object> calculations = new HashMap<String, Object>();

    PatientWrapper patientWrapper = new PatientWrapper(patient);

    Encounter lastMchEnrollment = patientWrapper.lastEncounter(
            MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_ENROLLMENT));
    EncounterWrapper lastMchEnrollmentWrapped = new EncounterWrapper(lastMchEnrollment);

    Obs lmpObs = lastMchEnrollmentWrapped.firstObs(Dictionary.getConcept(Dictionary.LAST_MONTHLY_PERIOD));
    if (lmpObs != null) {
        Weeks weeks = Weeks.weeksBetween(new DateTime(lmpObs.getValueDate()), new DateTime(new Date()));
        calculations.put("gestation", weeks.getWeeks());
    }

    Obs hivStatusObs = lastMchEnrollmentWrapped.firstObs(Dictionary.getConcept(Dictionary.HIV_STATUS));
    if (hivStatusObs != null) {
        calculations.put("hivStatus", hivStatusObs.getValueCoded());
    } else {
        calculations.put("hivStatus", "Not Specified");
    }

    Encounter lastMchConsultation = patientWrapper.lastEncounter(
            MetadataUtils.existing(EncounterType.class, MchMetadata._EncounterType.MCHMS_CONSULTATION));

    if (lastMchConsultation != null) {
        EncounterWrapper lastMchConsultationWrapped = new EncounterWrapper(lastMchConsultation);

        Obs arvUseObs = lastMchConsultationWrapped
                .firstObs(Dictionary.getConcept(Dictionary.ANTIRETROVIRAL_USE_IN_PREGNANCY));
        if (arvUseObs != null) {
            Concept concept = arvUseObs.getValueCoded();
            if (concept.equals(Dictionary.getConcept(Dictionary.MOTHER_ON_PROPHYLAXIS))
                    || concept.equals(Dictionary.getConcept(Dictionary.MOTHER_ON_HAART))) {
                String regimen = "Regimen not specified";
                List<Obs> drugObsList = lastMchConsultationWrapped
                        .allObs(Dictionary.getConcept(Dictionary.ANTIRETROVIRAL_USED_IN_PREGNANCY));
                if (!drugObsList.isEmpty()) {
                    String rgmn = "";
                    for (Obs obs : drugObsList) {
                        if (obs != null) {
                            rgmn += obs.getValueCoded().getName().getName();
                            if (!obs.equals(drugObsList.get(drugObsList.size() - 1))) {
                                rgmn += " + ";
                            }
                        }
                    }
                    if (!rgmn.isEmpty()) {
                        regimen = rgmn;
                    }
                }
                if (concept.equals(Dictionary.getConcept(Dictionary.MOTHER_ON_PROPHYLAXIS))) {
                    calculations.put("onProhylaxis", "Yes (" + regimen + ")");
                    calculations.put("onHaart", "No");
                } else if (concept.equals(Dictionary.getConcept(Dictionary.MOTHER_ON_HAART))) {
                    calculations.put("onProhylaxis", "No");
                    calculations.put("onHaart", "Yes (" + regimen + ")");
                }
            } else {
                calculations.put("onProhylaxis", "No");
                calculations.put("onHaart", "No");
            }
        } else {
            calculations.put("onProhylaxis", "Not specified");
            calculations.put("onHaart", "Not specified");
        }
    } else {
        calculations.put("onProhylaxis", "Not specified");
        calculations.put("onHaart", "Not specified");
    }
    model.addAttribute("calculations", calculations);
}

From source file:org.openmrs.module.kenyaemr.fragment.controller.program.mchms.MchmsEnrollmentSummaryFragmentController.java

License:Open Source License

public String controller(@FragmentParam("patientProgram") PatientProgram patientProgram,
        @FragmentParam(value = "encounter", required = false) Encounter encounter,
        @FragmentParam("showClinicalData") boolean showClinicalData, FragmentModel model) {

    Map<String, Object> dataPoints = new LinkedHashMap<String, Object>();
    dataPoints.put("Enrolled", patientProgram.getDateEnrolled());

    Enrollment enrollment = new Enrollment(patientProgram);

    Obs ancNoObs = enrollment.firstObs(Dictionary.getConcept(Dictionary.ANTENATAL_CASE_NUMBER));
    if (ancNoObs != null) {
        dataPoints.put("ANC No", ancNoObs.getValueNumeric().intValue());
    }/* w  w w  .  ja  va 2s .c om*/

    EncounterType mchMsConsultation = MetadataUtils.existing(EncounterType.class,
            MchMetadata._EncounterType.MCHMS_CONSULTATION);
    Form delivery = MetadataUtils.existing(Form.class, MchMetadata._Form.MCHMS_DELIVERY);
    Encounter deliveryEncounter = enrollment.encounterByForm(mchMsConsultation, delivery);

    Obs lmpObs = enrollment.firstObs(Dictionary.getConcept(Dictionary.LAST_MONTHLY_PERIOD));
    if (lmpObs != null) {
        if (deliveryEncounter == null) {
            Weeks weeks = Weeks.weeksBetween(new DateTime(lmpObs.getValueDate()), new DateTime(new Date()));
            dataPoints.put("Gestation (weeks)", weeks.getWeeks());
            dataPoints.put("LMP", lmpObs.getValueDate());
            dataPoints.put("EDD (LMP)", CoreUtils.dateAddDays(lmpObs.getValueDate(), 280));
        }
    }

    Obs eddUsoundObs = enrollment.firstObs(Dictionary.getConcept(Dictionary.EXPECTED_DATE_OF_DELIVERY));
    if (eddUsoundObs != null) {
        if (deliveryEncounter == null) {
            dataPoints.put("EDD (Ultrasound)", eddUsoundObs.getValueDate());
        }
    }

    Obs gravidaObs = enrollment.firstObs(Dictionary.getConcept(Dictionary.GRAVIDA));
    if (gravidaObs != null) {
        dataPoints.put("Gravida", gravidaObs.getValueNumeric().intValue());
    }

    Obs parityTermObs = enrollment.firstObs(Dictionary.getConcept(Dictionary.PARITY_TERM));
    Obs parityAbortionObs = enrollment.firstObs(Dictionary.getConcept(Dictionary.PARITY_ABORTION));

    if (parityTermObs != null && parityAbortionObs != null) {
        dataPoints.put("Parity", parityTermObs.getValueNumeric().intValue() + " + "
                + parityAbortionObs.getValueNumeric().intValue());
    }

    model.put("dataPoints", dataPoints);
    return "view/dataPoints";
}

From source file:org.openmrs.module.maternalsummary.impl.MaternalSummaryServiceImpl.java

License:Open Source License

private void calcGestationalAge(ObsHistory dst) {
    if (dst.getDateOfLMP() == null)
        return;/*from ww w .  j  av  a2s.  c  om*/

    DateTime from = new DateTime(dst.getDateOfLMP());
    DateTime to = new DateTime();
    dst.setGestationalAge(Weeks.weeksBetween(from, to).getWeeks() + 1);

    if (dst.getGestationalAge() <= 0 || dst.getGestationalAge() > 46)
        dst.setGestationalAge(null);
}

From source file:org.openmrs.module.pihmalawi.reporting.definition.data.converter.DurationConverter.java

License:Open Source License

/**
 * @see DataConverter#convert(Object)// ww w .jav a 2s.  c  o  m
 */
public Object convert(Object original) {
    if (original != null) {
        DateTime from = new DateTime((Date) original);
        if (durationToDate == null) {
            durationToDate = new Date();
        }
        DateTime to = new DateTime(durationToDate);
        if (durationUnit == DurationUnit.YEARS) {
            return Years.yearsBetween(from, to).getYears();
        } else if (durationUnit == DurationUnit.MONTHS) {
            return Months.monthsBetween(from, to).getMonths();
        } else if (durationUnit == DurationUnit.WEEKS) {
            return Weeks.weeksBetween(from, to).getWeeks();
        } else if (durationUnit == DurationUnit.DAYS) {
            return Days.daysBetween(from, to).getDays();
        } else if (durationUnit == DurationUnit.HOURS) {
            return Hours.hoursBetween(from, to).getHours();
        } else if (durationUnit == DurationUnit.MINUTES) {
            return Minutes.minutesBetween(from, to).getMinutes();
        } else if (durationUnit == DurationUnit.SECONDS) {
            return Seconds.secondsBetween(from, to).getSeconds();
        } else {
            throw new IllegalArgumentException("Unable to convert with duration unit: " + durationUnit);
        }
    }
    return null;
}

From source file:org.openvpms.archetype.i18n.time.DateDurationFormatter.java

License:Open Source License

/**
 * Formats the duration between two timestamps.
 * <p/>//from  www.ja  va2  s . c  o  m
 * NOTE: this currently doesn't do anything sensible for from > to. Possible solution would be to simply
 * reverse the times, and then prepend a - between each field using  the
 *
 * @param from the starting time
 * @param to   the ending time
 * @return the formatted duration
 */
protected String format(DateTime from, DateTime to) {
    int years = 0;
    int months = 0;
    int weeks = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;

    DateTime start = from;
    if (showYears) {
        years = Years.yearsBetween(start, to).getYears();
        start = start.plusYears(years);
    }
    if (showMonths) {
        months = Months.monthsBetween(start, to).getMonths();
        start = start.plusMonths(months);
    }
    if (showWeeks) {
        weeks = Weeks.weeksBetween(start, to).getWeeks();
        start = start.plusWeeks(weeks);
    }
    if (showDays) {
        days = Days.daysBetween(start, to).getDays();
        start = start.plusDays(days);
    }
    if (showHours) {
        hours = Hours.hoursBetween(start, to).getHours();
        start = start.plusHours(hours);
    }
    if (showMinutes) {
        minutes = Minutes.minutesBetween(start, to).getMinutes();
    }

    Period period = new Period(years, months, weeks, days, hours, minutes, 0, 0);
    return formatter.print(period);
}

From source file:pt.ist.fenix.task.exportData.santanderCardGeneration.CreateAndInitializeExecutionCourses.java

License:Open Source License

public Stream<Integer> getWeeks(final Lesson lesson) {
    final SortedSet<Integer> weeks = new TreeSet<Integer>();
    final ExecutionCourse executionCourse = lesson.getExecutionCourse();
    final YearMonthDay firstPossibleLessonDay = executionCourse.getMaxLessonsPeriod().getLeft();
    for (final Interval interval : lesson.getAllLessonIntervals()) {
        final Integer week = Weeks.weeksBetween(firstPossibleLessonDay, interval.getStart().toLocalDate())
                .getWeeks() + 1;/*from w w  w.ja va  2 s . co m*/
        weeks.add(week);
    }
    return weeks.stream();
}