Search in sources :

Example 21 with Period

use of java.time.Period in project drools by kiegroup.

the class TypeUtil method formatValue.

public static String formatValue(final Object val, final boolean wrapForCodeUsage) {
    if (val instanceof String) {
        return formatString(val.toString(), wrapForCodeUsage);
    } else if (val instanceof LocalDate) {
        return formatDate((LocalDate) val, wrapForCodeUsage);
    } else if (val instanceof LocalTime || val instanceof OffsetTime) {
        return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
    } else if (val instanceof LocalDateTime || val instanceof OffsetDateTime) {
        return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
    } else if (val instanceof ZonedDateTime) {
        TemporalAccessor ta = (TemporalAccessor) val;
        ZoneId zone = ta.query(TemporalQueries.zone());
        if (!(zone instanceof ZoneOffset)) {
            // it is a ZoneRegion
            return formatDateTimeString(DateAndTimeFunction.REGION_DATETIME_FORMATTER.format((TemporalAccessor) val), wrapForCodeUsage);
        } else {
            return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
        }
    } else if (val instanceof Duration) {
        return formatDuration((Duration) val, wrapForCodeUsage);
    } else if (val instanceof Period) {
        return formatPeriod((Period) val, wrapForCodeUsage);
    } else if (val instanceof TemporalAccessor) {
        TemporalAccessor ta = (TemporalAccessor) val;
        if (ta.query(TemporalQueries.localDate()) == null && ta.query(TemporalQueries.localTime()) != null && ta.query(TemporalQueries.zoneId()) != null) {
            return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
        } else {
            return String.valueOf(val);
        }
    } else if (val instanceof List) {
        return formatList((List) val, wrapForCodeUsage);
    } else if (val instanceof Range) {
        return formatRange((Range) val, wrapForCodeUsage);
    } else if (val instanceof Map) {
        return formatContext((Map) val, wrapForCodeUsage);
    } else {
        return String.valueOf(val);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) TemporalAccessor(java.time.temporal.TemporalAccessor) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) Period(java.time.Period) Duration(java.time.Duration) Range(org.kie.dmn.feel.runtime.Range) LocalDate(java.time.LocalDate) ZoneOffset(java.time.ZoneOffset) OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) OffsetTime(java.time.OffsetTime) List(java.util.List) Map(java.util.Map)

Example 22 with Period

use of java.time.Period in project drools by kiegroup.

the class DMNRuntimeTest method testYearsAndMonthsDuration.

@Test
public void testYearsAndMonthsDuration() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("yearMonthDuration.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_6eda1490-21ca-441e-8a26-ab3ca800e43c", "Drawing 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    BuiltInType feelType = (BuiltInType) BuiltInType.determineTypeFromName("yearMonthDuration");
    Period period = (Period) feelType.fromString("P2Y1M");
    DMNContext context = runtime.newContext();
    context.set("iDuration", period);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("How long"), is("Longer than a year"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) Period(java.time.Period) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 23 with Period

use of java.time.Period in project drbookings by DrBookings.

the class PeriodTest method testOneMonthPeriodDays01.

@Test
public void testOneMonthPeriodDays01() {
    final Period p = Period.between(LocalDate.of(2017, 06, 01), LocalDate.of(2017, 07, 01));
    assertEquals(0, p.getDays());
}
Also used : Period(java.time.Period) Test(org.junit.Test)

Example 24 with Period

use of java.time.Period in project jdbi by jdbi.

the class TestPeriod method testNegativePeriod.

@Test
public void testNegativePeriod() {
    handle.execute("insert into intervals(id, foo) values(?, interval '-3 years -1 month 2 days')", 7);
    final Period p = handle.createQuery("select foo from intervals where id=?").bind(0, 7).mapTo(Period.class).findOnly();
    assertThat(p).isEqualTo(Period.of(-3, -1, 2));
}
Also used : Period(java.time.Period) Test(org.junit.Test)

Example 25 with Period

use of java.time.Period in project learnjava8 by mkdika.

the class Period1 method main.

/*  Java 8 Date Time API:
        ---------------------       
        java.time.Period
     */
public static void main(String[] args) {
    LocalDate date1 = LocalDate.of(2015, 10, 13);
    LocalDate date2 = LocalDate.now();
    Period period1 = Period.between(date1, date2);
    System.out.println("Date1: " + date1);
    System.out.println("Date2: " + date2);
    System.out.println("Periode (date1 to date2): " + period1);
    System.out.println("\tYears: " + period1.getYears());
    System.out.println("\tMonths: " + period1.getMonths());
    System.out.println("\tDays: " + period1.getDays());
}
Also used : Period(java.time.Period) LocalDate(java.time.LocalDate)

Aggregations

Period (java.time.Period)108 Test (org.junit.Test)38 Test (org.testng.annotations.Test)27 LocalDate (java.time.LocalDate)20 Duration (java.time.Duration)9 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)8 LocalDateTime (java.time.LocalDateTime)7 ZonedDateTime (java.time.ZonedDateTime)7 Date (java.util.Date)6 LocalTime (java.time.LocalTime)5 ZoneId (java.time.ZoneId)5 DateTimeFormatter (java.time.format.DateTimeFormatter)5 List (java.util.List)5 Test (org.junit.jupiter.api.Test)5 Instant (java.time.Instant)4 DateTimeParseException (java.time.format.DateTimeParseException)4 Temporal (java.time.temporal.Temporal)4 TemporalAmount (java.time.temporal.TemporalAmount)4 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4