Example usage for java.time.temporal ChronoUnit MONTHS

List of usage examples for java.time.temporal ChronoUnit MONTHS

Introduction

In this page you can find the example usage for java.time.temporal ChronoUnit MONTHS.

Prototype

ChronoUnit MONTHS

To view the source code for java.time.temporal ChronoUnit MONTHS.

Click Source Link

Document

Unit that represents the concept of a month.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDate a = LocalDate.of(2014, 6, 30);

    long p = a.until(LocalDate.of(2015, 6, 30), ChronoUnit.MONTHS);

    System.out.println(p);/*from w  w w .j  a v a 2  s.c  om*/
}

From source file:com.ikanow.aleph2.data_model.utils.TimeUtils.java

/** The simplest date parsing utility - only handles daily/hourly/monthly type strings (1d, d, daily, day - etc). Note "m" is ambiguous and not supported, use "min" or "month"
 * @param human_readable_period - daily/hourly/monthly type strings (1d, d, daily, day - etc). Note "m" is ambiguous and not supported, use "min" or "month"
 * @return a ChronoUnit if successful, else a generic error string
 *///ww  w .j  a va2 s . co m
public static Validation<String, ChronoUnit> getTimePeriod(final String human_readable_period) {
    return Patterns
            .match(Optional.ofNullable(human_readable_period).orElse("").toLowerCase().replaceAll("\\s+", ""))
            .<Validation<String, ChronoUnit>>andReturn()
            .when(d -> d.equals("1d"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("d"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("1day"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("day"), __ -> Validation.success(ChronoUnit.DAYS))
            .when(d -> d.equals("daily"), __ -> Validation.success(ChronoUnit.DAYS))

            .when(d -> d.equals("1w"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("w"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("1wk"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("wk"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("1week"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("week"), __ -> Validation.success(ChronoUnit.WEEKS))
            .when(d -> d.equals("weekly"), __ -> Validation.success(ChronoUnit.WEEKS))

            .when(d -> d.equals("1month"), __ -> Validation.success(ChronoUnit.MONTHS))
            .when(d -> d.equals("month"), __ -> Validation.success(ChronoUnit.MONTHS))
            .when(d -> d.equals("monthly"), __ -> Validation.success(ChronoUnit.MONTHS))

            .when(d -> d.equals("1sec"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("sec"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("1s"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("s"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("1second"), __ -> Validation.success(ChronoUnit.SECONDS))
            .when(d -> d.equals("second"), __ -> Validation.success(ChronoUnit.SECONDS))

            .when(d -> d.equals("1min"), __ -> Validation.success(ChronoUnit.MINUTES))
            .when(d -> d.equals("min"), __ -> Validation.success(ChronoUnit.MINUTES))
            .when(d -> d.equals("1minute"), __ -> Validation.success(ChronoUnit.MINUTES))
            .when(d -> d.equals("minute"), __ -> Validation.success(ChronoUnit.MINUTES))

            .when(d -> d.equals("1h"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("h"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("1hour"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("hour"), __ -> Validation.success(ChronoUnit.HOURS))
            .when(d -> d.equals("hourly"), __ -> Validation.success(ChronoUnit.HOURS))

            .when(d -> d.equals("1y"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("y"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("1year"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("year"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("1yr"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("yr"), __ -> Validation.success(ChronoUnit.YEARS))
            .when(d -> d.equals("yearly"), __ -> Validation.success(ChronoUnit.YEARS))

            .otherwise(__ -> Validation
                    .fail(ErrorUtils.get(ErrorUtils.INVALID_DATETIME_FORMAT, human_readable_period)));
}

From source file:devbury.dewey.plugins.RemindMe.java

private Date notifyAt(long amount, String units) {
    ChronoUnit chronoUnit = ChronoUnit.SECONDS;
    switch (units) {
    case "weeks":
    case "week":
        chronoUnit = ChronoUnit.WEEKS;
        break;//from   w  w  w .j av a  2 s.c  om
    case "months":
    case "month":
        chronoUnit = ChronoUnit.MONTHS;
        break;
    case "days":
    case "day":
        chronoUnit = ChronoUnit.DAYS;
        break;
    case "hours":
    case "hour":
        chronoUnit = ChronoUnit.HOURS;
        break;
    case "minutes":
    case "minute":
        chronoUnit = ChronoUnit.MINUTES;
        break;
    }
    return Date.from(Instant.now().plus(amount, chronoUnit));
}

From source file:com.ikanow.aleph2.data_model.utils.TimeUtils.java

/** Returns the suffix of a time-based index given the grouping period
 * @param grouping_period - the grouping period
 * @param lowest_granularity//from  ww w . j  a va 2  s  . c om
 * @return the index suffix, ie added to the base index
 */
public static String getTimeBasedSuffix(final ChronoUnit grouping_period,
        final Optional<ChronoUnit> lowest_granularity) {
    return lowest_granularity
            .map(lg -> grouping_period.compareTo(lg) < 0 ? getTimeBasedSuffix(lg, Optional.empty()) : null)
            .orElse(Patterns.match(grouping_period).<String>andReturn()
                    .when(p -> ChronoUnit.SECONDS == p, __ -> "yyyy.MM.dd.HH:mm:ss")
                    .when(p -> ChronoUnit.MINUTES == p, __ -> "yyyy.MM.dd.HH:mm")
                    .when(p -> ChronoUnit.HOURS == p, __ -> "yyyy.MM.dd.HH")
                    .when(p -> ChronoUnit.DAYS == p, __ -> "yyyy.MM.dd")
                    .when(p -> ChronoUnit.WEEKS == p, __ -> "YYYY-ww") // (deliberately 'Y' (week-year) not 'y' since 'w' is week-of-year 
                    .when(p -> ChronoUnit.MONTHS == p, __ -> "yyyy.MM")
                    .when(p -> ChronoUnit.YEARS == p, __ -> "yyyy").otherwise(__ -> ""));
}

From source file:com.ikanow.aleph2.core.shared.utils.TimeSliceDirUtils.java

/** Low level util because java8 time "plus" is odd
 * @param to_adjust/*from ww w  .j a va  2 s . c  o  m*/
 * @param increment
 * @return
 */
private static Date adjustTime(Date to_adjust, ChronoUnit increment) {
    return Patterns.match(increment).<Date>andReturn()
            .when(t -> t == ChronoUnit.SECONDS, __ -> DateUtils.addSeconds(to_adjust, 1))
            .when(t -> t == ChronoUnit.MINUTES, __ -> DateUtils.addMinutes(to_adjust, 1))
            .when(t -> t == ChronoUnit.HOURS, __ -> DateUtils.addHours(to_adjust, 1))
            .when(t -> t == ChronoUnit.DAYS, __ -> DateUtils.addDays(to_adjust, 1))
            .when(t -> t == ChronoUnit.WEEKS, __ -> DateUtils.addWeeks(to_adjust, 1))
            .when(t -> t == ChronoUnit.MONTHS, __ -> DateUtils.addMonths(to_adjust, 1))
            .when(t -> t == ChronoUnit.YEARS, __ -> DateUtils.addYears(to_adjust, 1)).otherwiseAssert();
}

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

public double getDuration(final IScope scope, final TimeUnitConstantExpression exp, final Double number) {
    final String name = exp.getName();
    // final boolean isTimeDependent = !exp.isConst();
    final boolean month = name.startsWith("m");
    final GamaDate next = this.plus(number, month ? ChronoUnit.MONTHS : ChronoUnit.YEARS);
    final double result = this.until(next, ChronoUnit.MILLIS) / 1000d;
    // DEBUG.LOG("Computation of " + number + " " + exp.getName() + " = " + result + "s or "
    // + this.until(next, ChronoUnit.DAYS) + " days");

    return result;
}

From source file:com.ccserver.digital.service.LOSService.java

private String getDurationFormat(LocalDateTime localDateTime) {
    if (localDateTime == null) {
        return StringUtils.EMPTY;
    }//from w  ww  . ja  va2  s  .  c o m
    return String.valueOf(ChronoUnit.MONTHS.between(localDateTime, LocalDateTime.now()));
}

From source file:org.silverpeas.core.calendar.Recurrence.java

private Temporal computeDateForMonthlyFrequencyFrom(final Temporal source, DayOfWeekOccurrence dayOfWeek) {
    Temporal current = source;//  w  w  w  .j  a  v a2  s. c  o  m
    if (dayOfWeek.nth() > 1) {
        current = current.with(ChronoField.ALIGNED_WEEK_OF_MONTH, dayOfWeek.nth());
    } else if (dayOfWeek.nth() < 0) {
        current = current.with(ChronoField.DAY_OF_MONTH, 1).plus(1, ChronoUnit.MONTHS).minus(1, ChronoUnit.DAYS)
                .plus(dayOfWeek.nth(), ChronoUnit.WEEKS).with(dayOfWeek.dayOfWeek());
    }
    return current;
}

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

@Override
public List<Parameter> getParameters() {
    List<Parameter> parameters = super.getParameters();

    parameters.add(SelectParameter.Builder.builder() //
            .name(TIME_UNIT_PARAMETER) //
            .item(ChronoUnit.YEARS.name(), "years") //
            .item(ChronoUnit.MONTHS.name(), "months") //
            .item(ChronoUnit.DAYS.name(), "days") //
            .item(ChronoUnit.HOURS.name(), "hours") //
            .defaultValue(ChronoUnit.HOURS.name()) //
            .build());/* ww  w .j  av  a 2  s.c  o m*/

    parameters.add(SelectParameter.Builder.builder() //
            .name(SINCE_WHEN_PARAMETER) //
            .canBeBlank(false) //
            .item(NOW_SERVER_SIDE_MODE, NOW_SERVER_SIDE_MODE) //
            .item(SPECIFIC_DATE_MODE, SPECIFIC_DATE_MODE, new Parameter(SPECIFIC_DATE_PARAMETER, //
                    ParameterType.DATE, //
                    StringUtils.EMPTY, //
                    false, //
                    false)) //
            .item(OTHER_COLUMN_MODE, OTHER_COLUMN_MODE, new Parameter(SELECTED_COLUMN_PARAMETER, //
                    ParameterType.COLUMN, //
                    StringUtils.EMPTY, //
                    false, //
                    false)) //
            .defaultValue(NOW_SERVER_SIDE_MODE) //
            .build());

    return ActionsBundle.attachToAction(parameters, this);
}

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

@Test
public void should_compute_twice_diff_units() throws IOException {
    //given//  w  w w . ja  v  a2  s .c  o  m
    final String date = "07/15/2014 02:00";
    final String resultInMonth = computeTimeSince(date, "M/d/yyyy HH:mm", ChronoUnit.MONTHS);
    final String resultInYears = computeTimeSince(date, "M/d/yyyy HH:mm", ChronoUnit.YEARS);

    final DataSetRow row = getDefaultRow("statistics_MM_dd_yyyy_HH_mm.json");
    row.set("0001", date);

    final Map<String, String> expectedValues = new TreeMap<>();
    expectedValues.put("0000", "lorem bacon");
    expectedValues.put("0001", date);
    expectedValues.put("0004", resultInMonth);
    expectedValues.put("0003", resultInYears);
    expectedValues.put("0002", "Bacon");

    //when
    parameters.put(TIME_UNIT_PARAMETER, YEARS.name());
    ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters));

    parameters.put(TIME_UNIT_PARAMETER, MONTHS.name());
    ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters));

    //then
    assertEquals(expectedValues, row.values());
}