List of usage examples for org.joda.time Period Period
private Period(int[] values, PeriodType type)
From source file:org.libreplan.importers.MPXJProjectFileConversor.java
License:Open Source License
/** * Private Method/* ww w. j a v a 2 s . c om*/ * * Get {@link CalendarExceptionDTO} from a ProjectCalendarException * * @param projectCalendar * ProjectCalendarException to extract data from. * @return List<CalendarExceptionDTO> with the calendar exceptions that we want to import. */ private static List<CalendarExceptionDTO> toCalendarExceptionDTOs( ProjectCalendarException projectCalendarException) { List<CalendarExceptionDTO> calendarExceptionDTOs = new ArrayList<CalendarExceptionDTO>(); Date fromDate = projectCalendarException.getFromDate(); Date toDate = projectCalendarException.getToDate(); Period period = new Period(new DateTime(fromDate), new DateTime(toDate)); boolean working = projectCalendarException.getWorking(); int day = period.getDays(); Calendar cal = Calendar.getInstance(); cal.setTime(fromDate); List<Integer> duration = toHours(projectCalendarException); int hours; int minutes; if (duration != null) { hours = duration.get(0); minutes = duration.get(1); } else { if (working) { hours = 8; } else { hours = 0; } minutes = 0; } while (day > -1) { if (day == 0) { calendarExceptionDTOs.add(toCalendarExceptionDTO(cal.getTime(), hours, minutes, working)); } else { calendarExceptionDTOs.add(toCalendarExceptionDTO(cal.getTime(), hours, minutes, working)); cal.add(Calendar.DAY_OF_MONTH, +1); } day--; } return calendarExceptionDTOs; }
From source file:org.libreplan.importers.MPXJProjectFileConversor.java
License:Open Source License
/** * Private Method/* w w w . ja v a2 s. c o m*/ * * Get the number of hours of a ProjectCalendarHours * * @param projectCalendarDateRanges * ProjectCalendarDateRanges to extract data from. * @return Integer with the total number of hours or null if the projectCalendarHours is null. */ private static List<Integer> toHours(ProjectCalendarDateRanges projectCalendarDateRanges) { if (projectCalendarDateRanges != null) { List<Integer> duration = new ArrayList<Integer>(); int hours = 0; int minutes = 0; for (DateRange dateRange : projectCalendarDateRanges) { DateTime start = new DateTime(dateRange.getStart()); DateTime end = new DateTime(dateRange.getEnd()); Period period = new Period(start, end); int days = period.getDays(); if (period.getDays() != 0) { hours += 24 * days; } hours += period.getHours(); minutes += period.getMinutes(); } duration.add(hours); duration.add(minutes); return duration; } else { return null; } }
From source file:org.libreplan.importers.MPXJProjectFileConverter.java
License:Open Source License
/** * Get {@link CalendarExceptionDTO} from a ProjectCalendarException. * * @param projectCalendarException/*from w w w . jav a 2 s . c o m*/ * ProjectCalendarException to extract data from. * @return List<CalendarExceptionDTO> with the calendar exceptions that we want to import. */ private static List<CalendarExceptionDTO> toCalendarExceptionDTOs( ProjectCalendarException projectCalendarException) { List<CalendarExceptionDTO> calendarExceptionDTOs = new ArrayList<>(); Date fromDate = projectCalendarException.getFromDate(); Date toDate = projectCalendarException.getToDate(); Period period = new Period(new DateTime(fromDate), new DateTime(toDate)); boolean working = projectCalendarException.getWorking(); int day = period.getDays(); Calendar cal = Calendar.getInstance(); cal.setTime(fromDate); List<Integer> duration = toHours(projectCalendarException); int hours; int minutes; if (duration != null) { hours = duration.get(0); minutes = duration.get(1); } else { if (working) { hours = 8; } else { hours = 0; } minutes = 0; } while (day > -1) { if (day == 0) { calendarExceptionDTOs.add(toCalendarExceptionDTO(cal.getTime(), hours, minutes, working)); } else { calendarExceptionDTOs.add(toCalendarExceptionDTO(cal.getTime(), hours, minutes, working)); cal.add(Calendar.DAY_OF_MONTH, +1); } day--; } return calendarExceptionDTOs; }
From source file:org.libreplan.importers.MPXJProjectFileConverter.java
License:Open Source License
/** * Get the number of hours of a ProjectCalendarHours. * * @param projectCalendarDateRanges/* ww w .j av a 2 s. co m*/ * ProjectCalendarDateRanges to extract data from. * @return Integer with the total number of hours or null if the projectCalendarHours is null. */ private static List<Integer> toHours(ProjectCalendarDateRanges projectCalendarDateRanges) { if (projectCalendarDateRanges != null) { List<Integer> duration = new ArrayList<>(); int hours = 0; int minutes = 0; for (DateRange dateRange : projectCalendarDateRanges) { DateTime start = new DateTime(dateRange.getStart()); DateTime end = new DateTime(dateRange.getEnd()); Period period = new Period(start, end); int days = period.getDays(); if (period.getDays() != 0) { hours += 24 * days; } hours += period.getHours(); minutes += period.getMinutes(); } duration.add(hours); duration.add(minutes); return duration; } else { return null; } }
From source file:org.n52.sos.ds.hibernate.SosCacheFeederDAO.java
License:Open Source License
private void logCacheLoadTime(long startTime) { Period cacheLoadPeriod = new Period(startTime, System.currentTimeMillis()); LOGGER.info("Cache load finished in {} ({} seconds)", PeriodFormat.getDefault().print(cacheLoadPeriod.normalizedStandard()), cacheLoadPeriod.toStandardSeconds()); }
From source file:org.neotree.ui.view.PeriodFieldView.java
License:Open Source License
private Period recalculateValue() { if (TextUtils.isEmpty(getField().calculation)) { return null; }/*from w w w .j av a 2 s . c o m*/ // Always recalculate field value String cleanedExpression = getField().calculation.replaceAll("[\\s$]*", ""); String[] keys = cleanedExpression.split("-"); if (keys.length == 0) { Log.w(logTag(), String.format( "Invalid expression for period field. Reference value [%s] for period field is null. Ignoring calculation", keys[0])); return null; } else if (keys.length == 1) { DateTime refValue = getScriptPlayer().getValue(keys[0]); if (refValue == null) { Log.w(logTag(), String.format("Reference value [%s] for period field is null. Ignoring calculation", keys[0])); return null; } return new Period(refValue, DateTime.now()); } else if (keys.length == 2) { DateTime v0 = getScriptPlayer().getValue(keys[0]); DateTime v1 = getScriptPlayer().getValue(keys[1]); if (v0 == null || v1 == null) { Log.w(logTag(), String.format( "Reference value [key1=%s, key2=%s]for period field is null. Ignoring calculation", keys[0], keys[1])); return null; } return new Period(v0, v1); } return null; }
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 www . j a v 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. ja v a2 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 {/* w ww . j av a 2 s . c om*/ return "" + (age.getYears() * 12 + age.getMonths()) + " mo"; } }
From source file:org.projectbuendia.client.utils.date.RelativeDateTimeFormatter.java
License:Apache License
/** Returns a formatted representation of {@code other}, relative to {@code now}. */ public String format(DateTime now, DateTime other) { if (other.isEqual(now)) { return mCasing.rightNow; }/*from ww w . j a v a 2 s . co m*/ if (other.isAfter(now)) { return mCasing.inTheFuture; } int daysAgo = new Period(other, now).toStandardDays().getDays(); if (daysAgo == 0) { return mCasing.today; } else if (daysAgo == 1) { return mCasing.yesterday; } else { return String.format(mCasing.daysAgo, daysAgo); } }