Example usage for org.joda.time Period Period

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

Introduction

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

Prototype

public Period(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis) 

Source Link

Document

Create a period from a set of field values using the standard set of fields.

Usage

From source file:com.android.calendar.recurrencepicker.RecurrencePickerDialog.java

License:Open Source License

@NonNull
private Recurring getRecurring() {
    final SparseBooleanArray checked = new SparseBooleanArray();
    final int type = (mForDue ? 2 : 0) + mIntervalType.getSelectedItemPosition();
    for (int i = 0; i < mWeekByDayButtons.length; i++) {
        if (mWeekByDayButtons[i].isChecked()) {
            checked.put(TIME_DAY_TO_CALENDAR_DAY[i], (type == 2) && mWeekByDayButtons[i].isChecked());
        }//from  w w w.  java 2s.co  m
    }

    Log.d(TAG, "TYPE: " + type);
    int intervalMonths = 0;
    int intervalYears = 0;
    int intervalDays = 0;
    int intervalHours = 0;
    int intervalMinutes = 0;
    switch (type) {
    case 0:
        intervalMinutes = mIntervalValue;
        break;
    case 1:
        intervalHours = mIntervalValue;
        break;
    case 2:
        //weekdays, handled above
        break;
    case 3:
        intervalDays = mIntervalValue;
        break;
    case 4:
        intervalMonths = mIntervalValue;
        break;
    case 5:
        intervalYears = mIntervalValue;
        break;
    default:
        throw new IllegalStateException("Implement another case of intervall type");
    }
    return Recurring.newRecurring("",
            new Period(intervalYears, intervalMonths, 0, intervalDays, intervalHours, intervalMinutes, 0, 0),
            mForDue, mStartDate, mEndDate, true, mUseExact.isChecked(), checked);
}

From source file:com.indeed.imhotep.sql.parser.PeriodParser.java

License:Apache License

/**
 * Returns a JodaTime Period object representing the provided string period value.
 * Only first symbol of each field tag is mandatory, the rest of the tag is optional. e.g. 1d = 1 day
 * Spacing between the numbers and tags is optional. e.g. 1d = 1 d
 * Having a tag with no number implies quantity of 1. e.g. d = 1d
 * 'ago' suffix is optional.//from w ww.jav a 2  s.  c  o m
 * Commas can optionally separate the period parts.
 */
@Nullable
public static Period parseString(String value) {
    final String cleanedValue = Strings.nullToEmpty(value).toLowerCase();
    Matcher matcher = relativeDatePattern.matcher(cleanedValue);
    if (!matcher.matches()) {
        return null;
    }
    int years = getValueFromMatch(matcher, 1);
    int months = getValueFromMatch(matcher, 2);
    int weeks = getValueFromMatch(matcher, 3);
    int days = getValueFromMatch(matcher, 4);
    int hours = getValueFromMatch(matcher, 5);
    int minutes = getValueFromMatch(matcher, 6);
    int seconds = getValueFromMatch(matcher, 7);
    return new Period(years, months, weeks, days, hours, minutes, seconds, 0);
}

From source file:com.marand.thinkmed.medications.business.impl.MedicationsEhrUtils.java

License:Open Source License

public static OrderActivity createEmptyOrderActivityFor(final MedicationInstructionInstruction instruction) {
    final OrderActivity orderActivity = new OrderActivity();
    final DvParsable timing = new DvParsable();
    timing.setValue(ISOPeriodFormat.standard().print(new Period(0, 0, 0, 1, 0, 0, 0, 0)));
    timing.setFormalism(IspekEhrUtils.TIMING_FORMALISM);
    orderActivity.setTiming(timing);/*from  ww w  .  j  av  a 2  s .  c  o  m*/
    orderActivity.setActionArchetypeId("[openEHR-EHR-ACTION.medication.v1]");
    instruction.getOrder().add(orderActivity);
    return orderActivity;
}

From source file:ddf.catalog.filter.proxy.adapter.PeriodParser.java

License:Open Source License

public static Period parse(@NotNull String periodText, Pattern regex) {

    Matcher matcher = regex.matcher(periodText);
    if (matcher.matches()) {
        long[] unitDurationsInSeconds = getUnitDurationsInSeconds();
        BigDecimal[] fullDecimalValues = parseFullDecimalValues(periodText, matcher);
        int[] roundedValues = roundValuesDownToNearestInt(fullDecimalValues);
        int sumOfFractionalValuesInSeconds = sumOfFractionalValuesInSeconds(fullDecimalValues,
                unitDurationsInSeconds);

        Period period = new Period(roundedValues[0], roundedValues[1], roundedValues[2], roundedValues[3],
                roundedValues[4], roundedValues[5], roundedValues[6], 0);

        return period.plusSeconds(sumOfFractionalValuesInSeconds);
    } else {//from   w  w w.j av  a  2  s . com
        throw new IllegalArgumentException("Period not in valid format: " + periodText);
    }
}

From source file:de.azapps.mirakel.helper.CompatibilityHelper.java

License:Open Source License

private static Period getInterval(final int years, final int months, final int days, final int hours,
        final int minutes) {
    return new Period(years, months, 0, days, hours, minutes, 0, 0);
}

From source file:de.azapps.mirakel.helper.export_import.AnyDoImport.java

License:Open Source License

private static List<Pair<Integer, String>> parseTask(final JsonObject jsonTask,
        final SparseIntArray listMapping, final List<Pair<Integer, String>> contents, final Context ctx)
        throws NoSuchListException {
    final String name = jsonTask.get("title").getAsString();
    if (jsonTask.has("parentId")) {
        contents.add(new Pair<Integer, String>(jsonTask.get("parentId").getAsInt(), name));
        return contents;
    }/*from  w  w  w.  j  a v  a2  s  .  co  m*/
    final int list_id = jsonTask.get("categoryId").getAsInt();
    final Optional<ListMirakel> listMirakel = ListMirakel.get(listMapping.get(list_id));
    if (!listMirakel.isPresent()) {
        throw new NoSuchListException("Task:" + jsonTask.get("id").getAsInt());
    }
    final Task t = Task.newTask(name, listMirakel.get());
    taskMapping.put(jsonTask.get("id").getAsInt(), (int) t.getId());
    if (jsonTask.has("dueDate")) {
        final long dueMs = jsonTask.get("dueDate").getAsLong();
        if (dueMs > 0L) {
            t.setDue(of(new DateTime(dueMs)));
        }
    }
    if (jsonTask.has("priority")) {
        int prio = 0;
        if ("High".equals(jsonTask.get("priority").getAsString())) {
            prio = 2;
        }
        t.setPriority(prio);
    }
    if (jsonTask.has("status")) {
        boolean done = false;
        final String status = jsonTask.get("status").getAsString();
        if ("DONE".equals(status) || "CHECKED".equals(status)) {
            done = true;
        } else if (status.equals("UNCHECKED")) {
            done = false;
        }
        t.setDone(done);
    }
    if (jsonTask.has("repeatMethod")) {
        final String repeat = jsonTask.get("repeatMethod").getAsString();
        if (!repeat.equals("TASK_REPEAT_OFF")) {
            Optional<Recurring> recurringOptional = absent();
            switch (repeat) {
            case "TASK_REPEAT_DAY":
                recurringOptional = Recurring.get(1, 0, 0);
                if (!recurringOptional.isPresent()) {
                    recurringOptional = of(Recurring.newRecurring(ctx.getString(R.string.daily),
                            new Period(0, 0, 0, 1, 0, 0, 0, 0), true, Optional.<DateTime>absent(),
                            Optional.<DateTime>absent(), false, false, new SparseBooleanArray()));
                }
                break;
            case "TASK_REPEAT_WEEK":
                recurringOptional = Recurring.get(7, 0, 0);
                if (!recurringOptional.isPresent()) {
                    recurringOptional = of(Recurring.newRecurring(ctx.getString(R.string.weekly),
                            new Period(0, 0, 1, 0, 0, 0, 0, 0), true, Optional.<DateTime>absent(),
                            Optional.<DateTime>absent(), false, false, new SparseBooleanArray()));
                }
                break;
            case "TASK_REPEAT_MONTH":
                recurringOptional = Recurring.get(0, 1, 0);
                if (!recurringOptional.isPresent()) {
                    recurringOptional = of(Recurring.newRecurring(ctx.getString(R.string.monthly),
                            new Period(0, 1, 0, 0, 0, 0, 0, 0), true, Optional.<DateTime>absent(),
                            Optional.<DateTime>absent(), false, false, new SparseBooleanArray()));
                }
                break;
            case "TASK_REPEAT_YEAR":
                recurringOptional = Recurring.get(0, 0, 1);
                if (!recurringOptional.isPresent()) {
                    recurringOptional = of(Recurring.newRecurring(ctx.getString(R.string.yearly),
                            new Period(1, 0, 0, 0, 0, 0, 0, 0), true, Optional.<DateTime>absent(),
                            Optional.<DateTime>absent(), false, false, new SparseBooleanArray()));
                }
                break;
            }
            t.setRecurrence(recurringOptional);
        }
    }
    t.save(false);
    return contents;
}

From source file:de.azapps.mirakel.helper.export_import.WunderlistImport.java

License:Open Source License

private static void parseTask(final JsonObject jsonTask) {
    final String name = jsonTask.get("title").getAsString();
    final int list_id_string = jsonTask.get("list_id").getAsInt();
    final Long listId = listMapping.get(list_id_string).getId();
    Optional<ListMirakel> listMirakelOptional = ListMirakel.get(listId);
    final ListMirakel list;
    if (listMirakelOptional.isPresent()) {
        list = listMirakelOptional.get();
    } else {/*w ww  .  j a va2 s  .c  o  m*/
        list = ListMirakel.safeFirst();
    }
    final Task t = Task.newTask(name, list);
    taskMapping.put(jsonTask.get("id").getAsInt(), t);
    if (jsonTask.has("due_date")) {
        try {
            final DateTime due = new DateTime(DateTimeHelper.parseDate(jsonTask.get("due_date").getAsString()));
            t.setDue(of(due));
        } catch (final ParseException e) {
            Log.e(TAG, "cannot parse date", e);
        }
    }
    if (jsonTask.has("note")) {
        t.setContent(jsonTask.get("note").getAsString());
    }
    if (jsonTask.has("completed_at")) {
        t.setDone(true);
    }
    if (jsonTask.has("starred") && jsonTask.get("starred").getAsBoolean()) {
        t.setPriority(2);
    }
    if (jsonTask.has("parent_id")) {
        subtasks.add(new Pair<>(t, jsonTask.get("parent_id").getAsInt()));
    }
    if (jsonTask.has("recurrence_type") && jsonTask.has("recurrence_count")) {
        final int rec_count = jsonTask.get("recurrence_count").getAsInt();
        final Recurring r;
        final String type = jsonTask.get("recurrence_type").getAsString();
        switch (type) {
        case "year":
            r = Recurring.newRecurring(type, new Period(rec_count, 0, 0, 0, 0, 0, 0, 0), true,
                    Optional.<DateTime>absent(), Optional.<DateTime>absent(), true, true,
                    new SparseBooleanArray());
            break;
        case "month":
            r = Recurring.newRecurring(type, new Period(0, rec_count, 0, 0, 0, 0, 0, 0), true,
                    Optional.<DateTime>absent(), Optional.<DateTime>absent(), true, true,
                    new SparseBooleanArray());
            break;
        case "week":
            r = Recurring.newRecurring(type, new Period(0, 0, rec_count, 0, 0, 0, 0, 0), true,
                    Optional.<DateTime>absent(), Optional.<DateTime>absent(), true, true,
                    new SparseBooleanArray());
            break;
        case "day":
            r = Recurring.newRecurring(type, new Period(0, 0, 0, rec_count, 0, 0, 0, 0), true,
                    Optional.<DateTime>absent(), Optional.<DateTime>absent(), true, true,
                    new SparseBooleanArray());
            break;
        default:
            throw new JsonParseException("Unknown recurring " + type);
        }
        t.setRecurrence(of(r));
    }
    t.save(false);
}

From source file:de.azapps.mirakel.model.recurring.Recurring.java

License:Open Source License

public Recurring(final @NonNull CursorGetter c) {
    super(c.getLong(ID), c.getString(LABEL));
    final SparseBooleanArray weekdays = new SparseBooleanArray();
    weekdays.put(DateTimeConstants.MONDAY, c.getBoolean(MONDAY));
    weekdays.put(DateTimeConstants.TUESDAY, c.getBoolean(TUESDAY));
    weekdays.put(DateTimeConstants.WEDNESDAY, c.getBoolean(WEDNESDAY));
    weekdays.put(DateTimeConstants.THURSDAY, c.getBoolean(THURSDAY));
    weekdays.put(DateTimeConstants.FRIDAY, c.getBoolean(FRIDAY));
    weekdays.put(DateTimeConstants.SATURDAY, c.getBoolean(SATURDAY));
    weekdays.put(DateTimeConstants.SUNDAY, c.getBoolean(SUNDAY));
    setInterval(new Period(c.getInt(YEARS), c.getInt(MONTHS), 0, c.getInt(DAYS), c.getInt(HOURS),
            c.getInt(MINUTES), 0, 0));//from w w w.  j  a v  a 2s .co m
    setForDue(c.getBoolean(FOR_DUE));
    setStartDate(c.getOptional(START_DATE, DateTime.class));
    setEndDate(c.getOptional(END_DATE, DateTime.class));
    setTemporary(c.getBoolean(TEMPORARY));
    setExact(c.getBoolean(EXACT));
    setWeekdays(weekdays);
    setDerivedFrom(c.getOptional(DERIVED, Long.class));
}

From source file:de.azapps.mirakelandroid.test.RandomHelper.java

License:Open Source License

public static Period getRandomPeriod() {
    return new Period(getRandomint(2), getRandomint(12), getRandomint(10), getRandomint(5), getRandomint(5),
            getRandomint(5), 0, 0);// ww  w .j  a  v a2 s  .  c  om
}

From source file:net.longfalcon.view.DateView.java

License:Open Source License

public Period roundPeriod(Period period) {
    int fieldCount = 0;
    int years = period.getYears();
    int months = period.getMonths();
    int weeks = period.getWeeks();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();
    if (years > 0) {
        fieldCount++;/*w  w  w  . j a  v  a  2s  .  co m*/
    }
    if (months > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(years, months, 0, 0, 0, 0, 0, 0);
    }

    if (weeks > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, months, weeks, 0, 0, 0, 0, 0);
    }

    if (days > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, weeks, days, 0, 0, 0, 0);
    }

    if (hours > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, 0, days, hours, 0, 0, 0);
    }

    if (minutes > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, 0, 0, hours, minutes, 0, 0);
    }

    return new Period(0, 0, 0, 0, 0, minutes, seconds, 0);
}