Example usage for java.time.temporal TemporalUnit between

List of usage examples for java.time.temporal TemporalUnit between

Introduction

In this page you can find the example usage for java.time.temporal TemporalUnit between.

Prototype

long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive);

Source Link

Document

Calculates the amount of time between two temporal objects.

Usage

From source file:msi.gama.util.GamaDate.java

@Override
public long until(final Temporal endExclusive, final TemporalUnit unit) {
    return unit.between(internal, endExclusive);
}

From source file:org.talend.dataprep.transformation.actions.date.ComputeTimeSince.java

@Override
public void applyOnColumn(DataSetRow row, ActionContext context) {
    RowMetadata rowMetadata = context.getRowMetadata();
    Map<String, String> parameters = context.getParameters();
    String columnId = context.getColumnId();

    final String newColumnId = context.column("result");

    TemporalUnit unit = ChronoUnit.valueOf(parameters.get(TIME_UNIT_PARAMETER).toUpperCase());
    String newValue;/* w  w  w  .  j  a v  a 2s.c  om*/
    try {
        String mode = context.get(SINCE_WHEN_PARAMETER);
        LocalDateTime since;
        switch (mode) {
        case OTHER_COLUMN_MODE:
            ColumnMetadata selectedColumn = rowMetadata.getById(parameters.get(SELECTED_COLUMN_PARAMETER));
            String dateToCompare = row.get(selectedColumn.getId());
            since = Providers.get().parse(dateToCompare, selectedColumn);
            break;
        case SPECIFIC_DATE_MODE:
        case NOW_SERVER_SIDE_MODE:
        default:
            since = context.get(SINCE_DATE_PARAMETER);
            break;
        }

        // parse the date
        if (since == null) {
            newValue = StringUtils.EMPTY;
        } else {
            String value = row.get(columnId);
            LocalDateTime temporalAccessor = Providers.get().parse(value,
                    context.getRowMetadata().getById(columnId));
            Temporal valueAsDate = LocalDateTime.from(temporalAccessor);
            newValue = String.valueOf(unit.between(valueAsDate, since));
        }
    } catch (DateTimeException e) {
        LOGGER.trace("Error on dateTime parsing", e);
        // Nothing to do: in this case, temporalAccessor is left null
        newValue = StringUtils.EMPTY;
    }
    row.set(newColumnId, newValue);
}