Example usage for org.joda.time Period getYears

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

Introduction

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

Prototype

public int getYears() 

Source Link

Document

Gets the years field part of the period.

Usage

From source file:org.jw.service.entity.Contact.java

@Transient
public String getAge() {
    if (this.birthdate == null)
        return "<No Birthdate>";

    LocalDate dob = LocalDate.fromDateFields(birthdate);
    LocalDate now = LocalDate.now();

    Period period = new Period(dob, now, PeriodType.yearMonthDay());

    return String.format("%d years & %d months", period.getYears(), period.getMonths());
}

From source file:org.kalypso.commons.time.PeriodUtils.java

License:Open Source License

/**
 * @return {@link Integer#MAX_VALUE} if the amount could not be determined.
 */// w ww  . j a  va2s .c o m
public static int findCalendarAmount(final Period period) {
    final int fieldCount = countNonZeroFields(period);
    if (fieldCount > 1)
        throw new IllegalArgumentException(
                "Unable to find calendar amount for periods with more than one field: " + period); //$NON-NLS-1$

    if (period.getDays() != 0)
        return period.getDays();

    if (period.getHours() != 0)
        return period.getHours();

    if (period.getMillis() != 0)
        return period.getMillis();

    if (period.getMinutes() != 0)
        return period.getMinutes();

    if (period.getMonths() != 0)
        return period.getMonths();

    if (period.getSeconds() != 0)
        return period.getSeconds();

    if (period.getWeeks() != 0)
        return period.getWeeks();

    if (period.getYears() != 0)
        return period.getYears();

    return Integer.MAX_VALUE;
}

From source file:org.kalypso.commons.time.PeriodUtils.java

License:Open Source License

public static FIELD findCalendarField(final Period period) {
    final int fieldCount = countNonZeroFields(period);

    if (fieldCount > 1)
        throw new IllegalArgumentException(
                "Unable to find calendar field for periods with more than one field: " + period); //$NON-NLS-1$

    if (period.getDays() != 0)
        return FIELD.DAY_OF_MONTH;

    if (period.getHours() != 0)
        return FIELD.HOUR_OF_DAY;

    if (period.getMillis() != 0)
        return FIELD.MILLISECOND;

    if (period.getMinutes() != 0)
        return FIELD.MINUTE;

    if (period.getMonths() != 0)
        return FIELD.MONTH;

    if (period.getSeconds() != 0)
        return FIELD.SECOND;

    if (period.getWeeks() != 0)
        return FIELD.WEEK_OF_YEAR;

    if (period.getYears() != 0)
        return FIELD.YEAR;

    return null;/*from w ww. j av  a  2 s. co  m*/
}

From source file:org.kalypso.ogc.sensor.metadata.MetadataHelper.java

License:Open Source License

public static void setTimestep(final MetadataList mdl, final Period timestep) {
    final int[] values = timestep.getValues();
    int fieldCount = 0;
    for (final int value : values) {
        if (value != 0)
            fieldCount++;//w  w w .  ja v  a2  s  . c om
    }

    if (fieldCount > 1)
        throw new IllegalArgumentException(Messages.getString("MetadataHelper_2") + timestep); //$NON-NLS-1$

    int amount = -1;
    int calendarField = -1;

    if (timestep.getDays() != 0) {
        amount = timestep.getDays();
        calendarField = Calendar.DAY_OF_MONTH;
    } else if (timestep.getHours() != 0) {
        amount = timestep.getHours();
        calendarField = Calendar.HOUR_OF_DAY;
    } else if (timestep.getMillis() != 0) {
        amount = timestep.getMillis();
        calendarField = Calendar.MILLISECOND;
    } else if (timestep.getMinutes() != 0) {
        amount = timestep.getMinutes();
        calendarField = Calendar.MINUTE;
    } else if (timestep.getMonths() != 0) {
        amount = timestep.getMonths();
        calendarField = Calendar.MONTH;
    } else if (timestep.getSeconds() != 0) {
        amount = timestep.getSeconds();
        calendarField = Calendar.SECOND;
    } else if (timestep.getWeeks() != 0) {
        amount = timestep.getWeeks();
        calendarField = Calendar.WEEK_OF_YEAR;
    } else if (timestep.getYears() != 0) {
        amount = timestep.getYears();
        calendarField = Calendar.YEAR;
    }

    if (amount == -1)
        throw new IllegalArgumentException(Messages.getString("MetadataHelper_3")); //$NON-NLS-1$

    setTimestep(mdl, calendarField, amount);

    return;
}

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);//from w  w w .  j av  a  2s  .c  o m
        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)
 *//*from w  w w. j a  v a2s  . co 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 {/* w  ww .ja  v  a 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
 *//*from w  ww .ja v a2s  . 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;
}