Search in sources :

Example 16 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project Bytecoder by mirkosertic.

the class Period method from.

// -----------------------------------------------------------------------
/**
 * Obtains an instance of {@code Period} from a temporal amount.
 * <p>
 * This obtains a period based on the specified amount.
 * A {@code TemporalAmount} represents an  amount of time, which may be
 * date-based or time-based, which this factory extracts to a {@code Period}.
 * <p>
 * The conversion loops around the set of units from the amount and uses
 * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS}
 * and {@link ChronoUnit#DAYS DAYS} units to create a period.
 * If any other units are found then an exception is thrown.
 * <p>
 * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology.
 *
 * @param amount  the temporal amount to convert, not null
 * @return the equivalent period, not null
 * @throws DateTimeException if unable to convert to a {@code Period}
 * @throws ArithmeticException if the amount of years, months or days exceeds an int
 */
public static Period from(TemporalAmount amount) {
    if (amount instanceof Period) {
        return (Period) amount;
    }
    if (amount instanceof ChronoPeriod) {
        if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) {
            throw new DateTimeException("Period requires ISO chronology: " + amount);
        }
    }
    Objects.requireNonNull(amount, "amount");
    int years = 0;
    int months = 0;
    int days = 0;
    for (TemporalUnit unit : amount.getUnits()) {
        long unitAmount = amount.get(unit);
        if (unit == ChronoUnit.YEARS) {
            years = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.MONTHS) {
            months = Math.toIntExact(unitAmount);
        } else if (unit == ChronoUnit.DAYS) {
            days = Math.toIntExact(unitAmount);
        } else {
            throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit);
        }
    }
    return create(years, months, days);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) TemporalUnit(java.time.temporal.TemporalUnit) ChronoPeriod(java.time.chrono.ChronoPeriod)

Example 17 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_getChronology.

@Test()
@UseDataProvider("data_of_calendars")
public void test_getChronology(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    assertEquals(period.getChronology(), chrono);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 18 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_getUnits.

@Test()
@UseDataProvider("data_of_calendars")
public void test_getUnits(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    assertEquals(period.getUnits().size(), 3);
    assertEquals(period.getUnits().get(0), YEARS);
    assertEquals(period.getUnits().get(1), MONTHS);
    assertEquals(period.getUnits().get(2), DAYS);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 19 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_isZero_isNegative.

@Test()
@UseDataProvider("data_of_calendars")
public void test_isZero_isNegative(Chronology chrono) {
    ChronoPeriod periodPositive = chrono.period(1, 2, 3);
    assertEquals(periodPositive.isZero(), false);
    assertEquals(periodPositive.isNegative(), false);
    ChronoPeriod periodZero = chrono.period(0, 0, 0);
    assertEquals(periodZero.isZero(), true);
    assertEquals(periodZero.isNegative(), false);
    ChronoPeriod periodNegative = chrono.period(-1, 0, 0);
    assertEquals(periodNegative.isZero(), false);
    assertEquals(periodNegative.isNegative(), true);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 20 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project j2objc by google.

the class TCKChronoPeriod method test_serialization.

// -----------------------------------------------------------------------
// Test Serialization of Calendars
// -----------------------------------------------------------------------
@Test()
@UseDataProvider("data_of_calendars")
public void test_serialization(Chronology chrono) throws Exception {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(period);
    out.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream in = new ObjectInputStream(bais);
    ChronoPeriod ser = (ChronoPeriod) in.readObject();
    assertEquals("deserialized ChronoPeriod is wrong", ser, period);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

ChronoPeriod (java.time.chrono.ChronoPeriod)55 Test (org.testng.annotations.Test)26 Test (org.junit.Test)21 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 ChronoLocalDate (java.time.chrono.ChronoLocalDate)6 MinguoDate (java.time.chrono.MinguoDate)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4 Period (java.time.Period)4 ChronoUnit (java.time.temporal.ChronoUnit)4 Temporal (java.time.temporal.Temporal)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Predicate (java.util.function.Predicate)4 Before (org.junit.Before)4 ComparablePeriod (org.kie.dmn.feel.lang.types.impl.ComparablePeriod)4 Duration (java.time.Duration)3 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)3 TemporalUnit (java.time.temporal.TemporalUnit)3