Example usage for org.joda.time Period getMonths

List of usage examples for org.joda.time Period getMonths

Introduction

In this page you can find the example usage for org.joda.time Period getMonths.

Prototype

public int getMonths() 

Source Link

Document

Gets the months field part of the period.

Usage

From source file:org.oscarehr.util.AgeCalculator.java

License:Open Source License

public static Age calculateAge(Calendar birthDate) {
    LocalDate birthdate = LocalDate.fromCalendarFields(birthDate);
    LocalDate now = new LocalDate(); //Today's date
    Period period = new Period(birthdate, now, PeriodType.yearMonthDay());

    return new Age(period.getDays(), period.getMonths(), period.getYears());
}

From source file:org.projectbuendia.client.ui.dialogs.EditPatientDialogFragment.java

License:Apache License

private void populateFields(Bundle args) {
    String idPrefix = "";
    String id = Utils.valueOrDefault(args.getString("id"), "");
    Matcher matcher = ID_PATTERN.matcher(id);
    if (matcher.matches()) {
        idPrefix = matcher.group(1);//  w w w . j  av  a  2s .  c om
        id = matcher.group(2);
    }
    if (idPrefix.isEmpty() && id.isEmpty()) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        idPrefix = pref.getString("last_id_prefix", "");
    }
    mIdPrefix.setText(idPrefix);
    mId.setText(id);
    mGivenName.setText(Utils.valueOrDefault(args.getString("givenName"), ""));
    mFamilyName.setText(Utils.valueOrDefault(args.getString("familyName"), ""));
    LocalDate birthdate = Utils.toLocalDate(args.getString("birthdate"));
    if (birthdate != null) {
        Period age = new Period(birthdate, LocalDate.now());
        mAgeYears.setText(String.valueOf(age.getYears()));
        mAgeMonths.setText(String.valueOf(age.getMonths()));
    }
    switch (args.getInt("gender", Patient.GENDER_UNKNOWN)) {
    case Patient.GENDER_FEMALE:
        mSexFemale.setChecked(true);
        break;
    case Patient.GENDER_MALE:
        mSexMale.setChecked(true);
        break;
    }
}

From source file:org.projectbuendia.client.ui.FunctionalTestCase.java

License:Apache License

/**
 * Adds a new patient using the new patient form.  Assumes that the UI is
 * in the location selection activity, and leaves the UI in the same
 * activity.  Note: this function will not work during {@link #setUp()}
 * as it relies on {@link #waitForProgressFragment()}.
 * @param delta an AppPatientDelta containing the data for the new patient;
 *     use Optional.absent() to leave fields unset
 * @param locationName the name of a location to assign to the new patient,
 *     or null to leave unset (assumes this name is unique among locations)
 *///  w  w  w  . j  a  v  a  2 s.c  o  m
protected void inLocationSelectionAddNewPatient(AppPatientDelta delta, String locationName) {
    LOG.i("Adding patient: %s (location %s)", delta.toContentValues().toString(), locationName);

    onView(withId(R.id.action_add)).perform(click());
    onView(withText("New Patient")).check(matches(isDisplayed()));
    if (delta.id.isPresent()) {
        onView(withId(R.id.patient_creation_text_patient_id)).perform(typeText(delta.id.get()));
    }
    if (delta.givenName.isPresent()) {
        onView(withId(R.id.patient_creation_text_patient_given_name)).perform(typeText(delta.givenName.get()));
    }
    if (delta.familyName.isPresent()) {
        onView(withId(R.id.patient_creation_text_patient_family_name))
                .perform(typeText(delta.familyName.get()));
    }
    if (delta.birthdate.isPresent()) {
        Period age = new Period(delta.birthdate.get().toLocalDate(), LocalDate.now());
        if (age.getYears() < 1) {
            onView(withId(R.id.patient_creation_text_age)).perform(typeText(Integer.toString(age.getMonths())));
            onView(withId(R.id.patient_creation_radiogroup_age_units_months)).perform(click());
        } else {
            onView(withId(R.id.patient_creation_text_age)).perform(typeText(Integer.toString(age.getYears())));
            onView(withId(R.id.patient_creation_radiogroup_age_units_years)).perform(click());
        }
    }
    if (delta.gender.isPresent()) {
        if (delta.gender.get() == AppPatient.GENDER_MALE) {
            onView(withId(R.id.patient_creation_radiogroup_age_sex_male)).perform(click());
        } else if (delta.gender.get() == AppPatient.GENDER_FEMALE) {
            onView(withId(R.id.patient_creation_radiogroup_age_sex_female)).perform(click());
        }
    }
    if (delta.admissionDate.isPresent()) {
        // TODO/completeness: Support admission date in addNewPatient().
        // The following code is broken -- hopefully fixed by Espresso 2.0.
        // onView(withId(R.id.patient_creation_admission_date)).perform(click());
        // selectDateFromDatePickerDialog(mDemoPatient.admissionDate.get());
    }
    if (delta.firstSymptomDate.isPresent()) {
        // TODO/completeness: Support first symptoms date in addNewPatient().
        // The following code is broken -- hopefully fixed by Espresso 2.0.
        // onView(withId(R.id.patient_creation_symptoms_onset_date)).perform(click());
        // selectDateFromDatePickerDialog(mDemoPatient.firstSymptomDate.get());
    }
    if (delta.assignedLocationUuid.isPresent()) {
        // TODO/completeness: Support assigned location in addNewPatient().
        // A little tricky as we need to select by UUID.
        // onView(withId(R.id.patient_creation_button_change_location)).perform(click());
    }
    if (locationName != null) {
        onView(withId(R.id.patient_creation_button_change_location)).perform(click());
        onView(withText(locationName)).perform(click());
    }

    EventBusIdlingResource<SingleItemCreatedEvent<AppPatient>> resource = new EventBusIdlingResource<>(
            UUID.randomUUID().toString(), mEventBus);

    onView(withId(R.id.patient_creation_button_create)).perform(click());
    Espresso.registerIdlingResources(resource); // wait for patient to be created
}

From source file:org.projectbuendia.client.utils.date.Dates.java

License:Apache License

/** Converts a birthdate to a string describing age in months or years. */
public static String birthdateToAge(LocalDate birthdate) {
    // TODO: Localization
    Period age = new Period(birthdate, LocalDate.now());
    if (age.getYears() >= 2) {
        return "" + age.getYears() + " y";
    } else {//from   w w  w. j a  va 2s .  c  o  m
        return "" + (age.getYears() * 12 + age.getMonths()) + " mo";
    }
}

From source file:org.projectbuendia.client.utils.Utils.java

License:Apache License

/** Converts a birthdate to a string describing age in months or years. */
public static String birthdateToAge(LocalDate birthdate, Resources resources) {
    Period age = new Period(birthdate, LocalDate.now());
    int years = age.getYears(), months = age.getMonths();
    return years >= 5 ? resources.getString(R.string.abbrev_n_years, years)
            : resources.getString(R.string.abbrev_n_months, months + years * 12);
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

License:Open Source License

/**
 * This method is used to get the breakdown of the duration between 2 days/timestamps in terms of years,
 * months, days, hours, minutes and seconds
 *
 * @param fromDate Start timestamp of the duration
 * @param toDate   End timestamp of the duration
 * @return A map containing the breakdown
 * @throws APIMgtUsageQueryServiceClientException when there is an error during date parsing
 *//*  w  w  w . java  2 s.  c  o m*/
private Map<String, Integer> getDurationBreakdown(String fromDate, String toDate)
        throws APIMgtUsageQueryServiceClientException {
    Map<String, Integer> durationBreakdown = new HashMap<String, Integer>();

    DateTimeFormatter formatter = DateTimeFormat
            .forPattern(APIUsageStatisticsClientConstants.TIMESTAMP_PATTERN);
    LocalDateTime startDate = LocalDateTime.parse(fromDate, formatter);
    LocalDateTime endDate = LocalDateTime.parse(toDate, formatter);
    Period period = new Period(startDate, endDate);
    int numOfYears = period.getYears();
    int numOfMonths = period.getMonths();
    int numOfWeeks = period.getWeeks();
    int numOfDays = period.getDays();
    if (numOfWeeks > 0) {
        numOfDays += numOfWeeks * 7;
    }
    int numOfHours = period.getHours();
    int numOfMinutes = period.getMinutes();
    int numOfSeconds = period.getSeconds();
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_YEARS, numOfYears);
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_MONTHS, numOfMonths);
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_DAYS, numOfDays);
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_WEEKS, numOfWeeks);
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_HOURS, numOfHours);
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_MINUTES, numOfMinutes);
    durationBreakdown.put(APIUsageStatisticsClientConstants.DURATION_SECONDS, numOfSeconds);
    return durationBreakdown;
}

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

License:Open Source License

private int findOffset(final Lesson oldLesson) {
    final GenericPair<YearMonthDay, YearMonthDay> maxLessonsPeriod = oldLesson.getExecutionCourse()
            .getMaxLessonsPeriod();/*  w w  w. j av a2  s . c  o m*/
    final LessonInstance lessonInstance = oldLesson.getFirstLessonInstance();
    final Period period;
    if (lessonInstance != null) {
        period = new Period(maxLessonsPeriod.getLeft(), lessonInstance.getDay());
    } else if (oldLesson.getPeriod() != null) {
        final YearMonthDay start = oldLesson.getPeriod().getStartYearMonthDay();
        period = new Period(maxLessonsPeriod.getLeft(), start);
    } else {
        period = null;
    }
    return period == null ? 0 : period.getMonths() * 4 + period.getWeeks() + (period.getDays() / 7);
}

From source file:uk.org.rbc1b.roms.controller.volunteer.ldc.SubmitLDCFormVolunteerEmailGenerator.java

License:Open Source License

/**
 * Generate email for a volunteer with the correct message.
 *
 * @param volunteer the volunteer/*  ww  w  .  ja  v a  2  s . c  om*/
 * @return Email object
 * @throws IOException if we can't find the email template
 * @throws TemplateException if Freemarker's had enough
 */
public Email generateEmailForVolunteers(Volunteer volunteer) throws IOException, TemplateException {
    Configuration conf = emailFreemarkerConfigurer.getConfiguration();
    Map<String, Object> model = new HashMap<>();
    model.put("volunteer", volunteer);

    Date date = volunteer.getFormDate();
    if (date == null) {
        model.put("message", SubmitLDCFormEmailMessageConstants.FORM_DATE_UNKNOWN.getMessage());
    } else {
        DateTime formDate = DataConverterUtil.toDateTime(date);
        DateTime todayDate = new DateTime();
        Period period = new Period(formDate, todayDate);

        if ((period.getYears() == 2) && (period.getMonths() >= 6)) {
            model.put("message", SubmitLDCFormEmailMessageConstants.FORM_DATE_TWO_HALF_YRS.getMessage());
        } else if (period.getYears() >= 3) {
            model.put("message", SubmitLDCFormEmailMessageConstants.FORM_DATE_THREE_YRS.getMessage());
        }
    }

    Email email = new Email();
    email.setRecipient(volunteer.getPerson().getEmail());
    email.setSubject(SUBJECT);
    email.setText(FreeMarkerTemplateUtils.processTemplateIntoString(conf.getTemplate(SUBMIT_LDC_FORM_TEMPLATE),
            model));

    return email;
}

From source file:utilities.time.DateAndTimes.java

License:Open Source License

/**
 *
 * This will calculate out and return a String the length of a period in easy to read format: Y
 *
 * @param duration//w w w.j  a v  a  2  s . com
 * @param yearsSeparatorFormat
 * @param monthsSeparatorFormat
 * @param weeksSeparatorFormat
 * @param daysSeparatorFormat
 * @param hoursSeparatorFormat
 * @param minutesSeparatorFormat
 * @param secondsSeparatorFormat
 * @return
 */
public static String getPeriodFormattedFromMilliseconds(long duration, String yearsSeparatorFormat,
        String monthsSeparatorFormat, String weeksSeparatorFormat, String daysSeparatorFormat,
        String hoursSeparatorFormat, String minutesSeparatorFormat, String secondsSeparatorFormat) {
    Period period = new Period(duration);
    if (period.getYears() > 0) {
        return getPeriodFormat(period, yearsSeparatorFormat, monthsSeparatorFormat, weeksSeparatorFormat,
                daysSeparatorFormat, hoursSeparatorFormat, minutesSeparatorFormat, secondsSeparatorFormat);
    } else if (period.getMonths() > 0) {
        return getPeriodFormat(period, monthsSeparatorFormat, weeksSeparatorFormat, daysSeparatorFormat,
                hoursSeparatorFormat, minutesSeparatorFormat, secondsSeparatorFormat);
    } else if (period.getWeeks() > 0) {
        return getPeriodFormat(period, weeksSeparatorFormat, daysSeparatorFormat, hoursSeparatorFormat,
                minutesSeparatorFormat, secondsSeparatorFormat);
    } else if (period.getDays() > 0) {
        return getPeriodFormat(period, daysSeparatorFormat, hoursSeparatorFormat, minutesSeparatorFormat,
                secondsSeparatorFormat);
    } else if (period.getHours() > 0) {
        return getPeriodFormat(period, hoursSeparatorFormat, minutesSeparatorFormat, secondsSeparatorFormat);
    } else if (period.getMinutes() > 0) {
        return getPeriodFormat(period, minutesSeparatorFormat, secondsSeparatorFormat);
    } else if (period.getSeconds() > 0) {
        return getPeriodFormat(period, secondsSeparatorFormat);
    } else {
        return null;
    }
}