Java Code Examples for javax.xml.datatype.Duration#getMonths()

The following examples show how to use javax.xml.datatype.Duration#getMonths() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DoubleDurationType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public Duration durationDivide(Duration first, Double second) {
    if (first == null || second == null) {
        return null;
    }

    try {
        if (isYearsAndMonths(first)) {
            long months = (first.getYears() * 12 + first.getMonths()) / second.intValue();
            return this.dataTypeFactory.newDurationYearMonth(String.format("P%dM", months));
        } else if (isDaysAndTime(first)) {
            long hours = 24L * first.getDays() + first.getHours();
            long minutes = 60L * hours + first.getMinutes();
            long seconds = 60L * minutes + first.getSeconds();
            seconds = seconds / second.intValue();
            return this.dataTypeFactory.newDurationDayTime(seconds * 1000L);
        } else {
            throw new DMNRuntimeException(String.format("Cannot divide '%s' by '%s'", first, second));
        }
    } catch (Exception e) {
        String message = String.format("durationDivide(%s, %s)", first, second);
        logError(message, e);
        return null;
    }
}
 
Example 2
Source File: DefaultDurationType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public Duration durationDivide(Duration first, BigDecimal second) {
    if (first == null || second == null) {
        return null;
    }

    try {
        if (isYearsAndMonths(first)) {
            long months = (first.getYears() * 12 + first.getMonths()) / second.intValue();
            return this.dataTypeFactory.newDurationYearMonth(String.format("P%dM", months));
        } else if (isDaysAndTime(first)) {
            long hours = 24L * first.getDays() + first.getHours();
            long minutes = 60L * hours + first.getMinutes();
            long seconds = 60L * minutes + first.getSeconds();
            seconds = seconds / second.intValue();
            return this.dataTypeFactory.newDurationDayTime(seconds * 1000L);
        } else {
            throw new DMNRuntimeException(String.format("Cannot divide '%s' by '%s'", first, second));
        }
    } catch (Exception e) {
        String message = String.format("durationDivide(%s, %s)", first, second);
        logError(message, e);
        return null;
    }
}
 
Example 3
Source File: ContextInfo.java    From SeaCloudsPlatform with Apache License 2.0 6 votes vote down vote up
public static Validity parse(String duration) {
    try {
        if (dtFactory == null) {
            dtFactory = DatatypeFactory.newInstance();
        }
        Duration d = dtFactory.newDuration(duration);
        Validity result = new Validity(
                d.getYears(),
                d.getMonths(),
                d.getDays()
        );
        return result;
    } catch (DatatypeConfigurationException e) {
        throw new SlaGeneratorException("Could not instantiate DatatypeFactory", e);
    }
}
 
Example 4
Source File: DefaultDurationLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public Integer months(Duration duration) {
    if (duration == null) {
        return null;
    }

    return duration.getMonths();
}
 
Example 5
Source File: Utils.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * converts duration, e.g. PTxxxM to minute int xxx
 * 
 * @param d
 * @return
 */
public static int duration2Minutes(Duration d) {
	long millis = d.getYears() * 12;
	millis = (millis + d.getMonths()) * 31;
	millis = (millis + d.getDays()) * 24;
	millis = (millis + d.getHours()) * 60;
	millis = (millis + d.getMinutes()) * 60;
	millis = (millis + d.getSeconds()) * 1000;

	int minutes = (int) (millis / 60 / 1000) * d.getSign();
	// logger.debug("Duration: "+d+" = "+minutes+" min");
	return minutes;
}
 
Example 6
Source File: DurationTypeInfoCompiler.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public JSAssignmentExpression createValue(MappingCompiler<T, C> mappingCompiler, String item) {
	final JSCodeModel codeModel = mappingCompiler.getCodeModel();
	final JSObjectLiteral result = codeModel.object();
	final Duration duration = datatypeFactory.newDuration(item);
	if (duration.getSign() <= 0) {
		result.append("sign", codeModel.integer(duration.getSign()));
	}
	if (duration.getYears() > 0) {
		result.append("years", codeModel.integer(duration.getYears()));
	}
	if (duration.getMonths() > 0) {
		result.append("months", codeModel.integer(duration.getMonths()));
	}
	if (duration.getDays() > 0) {
		result.append("days", codeModel.integer(duration.getDays()));
	}
	if (duration.getHours() > 0) {
		result.append("hours", codeModel.integer(duration.getHours()));
	}
	if (duration.getMinutes() > 0) {
		result.append("minutes", codeModel.integer(duration.getMinutes()));
	}
	if (duration.getSeconds() > 0) {
		result.append("seconds", codeModel.integer(duration.getSeconds()));
	}
	return result;
}
 
Example 7
Source File: DurationTypeInfoProducer.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public JsonValue createValue(JsonSchemaMappingCompiler<T, C> mappingCompiler, String item) {
	final JsonObjectBuilder objectBuilder = mappingCompiler.getJsonBuilderFactory().createObjectBuilder();
	final Duration duration = datatypeFactory.newDuration(item);
	if (duration.getSign() <= 0) {
		objectBuilder.add("sign", duration.getSign());
	}
	if (duration.getYears() > 0) {
		objectBuilder.add("years", duration.getYears());
	}
	if (duration.getMonths() > 0) {
		objectBuilder.add("months", duration.getMonths());
	}
	if (duration.getDays() > 0) {
		objectBuilder.add("days", duration.getDays());
	}
	if (duration.getHours() > 0) {
		objectBuilder.add("hours", duration.getHours());
	}
	if (duration.getMinutes() > 0) {
		objectBuilder.add("minutes", duration.getMinutes());
	}
	if (duration.getSeconds() > 0) {
		objectBuilder.add("seconds", duration.getSeconds());
	}
	return objectBuilder.build();
}
 
Example 8
Source File: TM_PeriodDuration.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given Java XML duration into an ISO 19108 duration.
 */
static PeriodDuration toISO(final Duration duration) {
    if (duration != null) try {
        final TemporalFactory factory = TemporalUtilities.getTemporalFactory();
        InternationalString years = null;
        int value;
        if ((value = duration.getYears()) != 0) {
            years = new SimpleInternationalString(Integer.toString(value));
        }
        InternationalString months = null;
        if ((value = duration.getMonths()) != 0) {
            months = new SimpleInternationalString(Integer.toString(value));
        }
        InternationalString weeks = null;                   // No weeks in javax.xml.datatype.Duration
        InternationalString days = null;
        if ((value = duration.getDays()) != 0) {
            days = new SimpleInternationalString(Integer.toString(value));
        }
        InternationalString hours = null;
        if ((value = duration.getHours()) != 0) {
            hours = new SimpleInternationalString(Integer.toString(value));
        }
        InternationalString minutes = null;
        if ((value = duration.getMinutes()) != 0) {
            minutes = new SimpleInternationalString(Integer.toString(value));
        }
        InternationalString seconds = null;
        if ((value = duration.getSeconds()) != 0) {
            seconds = new SimpleInternationalString(Integer.toString(value));
        }
        return factory.createPeriodDuration(years, months, weeks, days, hours, minutes, seconds);
    } catch (UnsupportedOperationException e) {
        warningOccured("toISO", e);
    }
    return null;
}
 
Example 9
Source File: DatatypeFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test {@link DatatypeFactory.newDurationDayTime(long milliseconds)}.
 */
@Test
public final void testNewDurationDayTime() {

    /**
     * Millisecond test values to test.
     */
    final long[] TEST_VALUES_MILLISECONDS = { 0L, 1L, -1L, 2678400000L, // 31
                                                                        // days,
                                                                        // e.g.
                                                                        // 1
                                                                        // month
            -2678400000L, 5270400000L, // 61 days, e.g. 2 months
            -5270400000L, 31622400000L, // 366 days, e.g. 1 year
            -31622400000L, 34300800000L, // 397 days, e.g. 1 year, 1 month
            -34300800000L };

    /**
     * Millisecond test value results of test.
     */
    final String[] TEST_VALUES_MILLISECONDS_RESULTS = { "P0Y0M0DT0H0M0.000S", "P0Y0M0DT0H0M0.001S", "-P0Y0M0DT0H0M0.001S", "P0Y1M", "-P0Y1M", "P0Y2M",
            "-P0Y2M", "P1Y0M", "-P1Y0M", "P1Y1M", "-P1Y1M" };

    DatatypeFactory datatypeFactory = null;
    try {
        datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException datatypeConfigurationException) {
        Assert.fail(datatypeConfigurationException.toString());
    }

    if (DEBUG) {
        System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
    }

    // test each value
    for (int onTestValue = 0; onTestValue < TEST_VALUES_MILLISECONDS.length; onTestValue++) {

        if (DEBUG) {
            System.err.println("testing value: \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\", expecting: \""
                    + TEST_VALUES_MILLISECONDS_RESULTS[onTestValue] + "\"");
        }

        try {
            Duration duration = datatypeFactory.newDurationDayTime(TEST_VALUES_MILLISECONDS[onTestValue]);

            if (DEBUG) {
                System.err.println("Duration created: \"" + duration.toString() + "\"");
            }

            // was this expected to fail?
            if (TEST_VALUES_MILLISECONDS_RESULTS[onTestValue].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\" is invalid yet it created the Duration \"" + duration.toString()
                        + "\"");
            }

            // does it have the right value?
            if (!TEST_VALUES_MILLISECONDS_RESULTS[onTestValue].equals(duration.toString())) {
                // TODO: this is bug that should be fixed
                if (false) {
                    Assert.fail("Duration created with \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\" was expected to be \""
                            + TEST_VALUES_MILLISECONDS_RESULTS[onTestValue] + "\" and has the value \"" + duration.toString() + "\"");
                } else {
                    System.err.println("Please fix this bug: " + "Duration created with \"" + TEST_VALUES_MILLISECONDS[onTestValue]
                            + "\" was expected to be \"" + TEST_VALUES_MILLISECONDS_RESULTS[onTestValue] + "\" and has the value \"" + duration.toString()
                            + "\"");
                }
            }

            // only day, hour, minute, and second should have values
            QName xmlSchemaType = duration.getXMLSchemaType();
            int years = duration.getYears();
            int months = duration.getMonths();

            if (!xmlSchemaType.equals(DatatypeConstants.DURATION_DAYTIME) || years != 0 || months != 0) {
                // TODO: this is bug that should be fixed
                if (false) {
                    Assert.fail("xdt:dayTimeDuration created without discarding remaining milliseconds: " + " XMLSchemaType = " + xmlSchemaType
                            + ", years = " + years + ", months = " + months);
                } else {
                    System.err.println("Please fix this bug: " + "xdt:dayTimeDuration created without discarding remaining milliseconds: "
                            + " XMLSchemaType = " + xmlSchemaType + ", years = " + years + ", months = " + months);
                }
            }

            // Duration created with correct values
        } catch (Exception exception) {

            if (DEBUG) {
                System.err.println("Exception in creating duration: \"" + exception.toString() + "\"");
            }

            // was this expected to succed?
            if (!TEST_VALUES_MILLISECONDS_RESULTS[onTestValue].equals(TEST_VALUE_FAIL)) {
                Assert.fail("the value \"" + TEST_VALUES_MILLISECONDS[onTestValue] + "\" is valid yet it failed with \"" + exception.toString() + "\"");
            }
            // expected failure
        }
    }
}