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

The following examples show how to use javax.xml.datatype.Duration#getSign() . 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: JavaTimeCalendarType.java    From jdmn with Apache License 2.0 5 votes vote down vote up
protected TemporalAmount toTemporalDuration(Duration duration) {
    int days = normalize(duration.getDays());
    int hours = normalize(duration.getHours());
    int minutes = normalize(duration.getMinutes());
    int seconds = normalize(duration.getSeconds());

    java.time.Duration timeDuration = java.time.Duration.ofDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds);
    return duration.getSign() == -1 ? timeDuration.negated() : timeDuration;
}
 
Example 2
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 3
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 4
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 5
Source File: JavaTimeCalendarType.java    From jdmn with Apache License 2.0 4 votes vote down vote up
protected TemporalAmount toTemporalPeriod(Duration duration) {
    Period period = Period.of(duration.getYears(), duration.getMonths(), 0);
    return duration.getSign() == -1 ? period.negated() : period;
}
 
Example 6
Source File: DurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void durationAssertEquals(Duration duration, QName xmlSchemaType, boolean isPositive, int years, int months, int days, int hours, int minutes,
        int seconds, long milliseconds, String lexical) {

    final TimeZone GMT = TimeZone.getTimeZone("GMT");
    final GregorianCalendar EPOCH = new GregorianCalendar(GMT);
    EPOCH.clear();

    if (DEBUG) {
        System.out.println("Testing Duration: " + duration.toString());
    }

    // sign
    if (DEBUG) {
        boolean actual = (duration.getSign() == 1) ? true : false;
        System.out.println("sign:");
        System.out.println("    expected: \"" + isPositive + "\"");
        System.out.println("    actual:   \"" + actual + "\"");
    }

    if (DEBUG) {
        System.out.println("years:");
        System.out.println("    expected: \"" + years + "\"");
        System.out.println("    actual:   \"" + duration.getYears() + "\"");
    }

    if (DEBUG) {
        System.out.println("months:");
        System.out.println("    expected: \"" + months + "\"");
        System.out.println("    actual:   \"" + duration.getMonths() + "\"");
    }

    if (DEBUG) {
        System.out.println("days:");
        System.out.println("    expected: \"" + days + "\"");
        System.out.println("    actual:   \"" + duration.getDays() + "\"");
    }

    if (DEBUG) {
        System.out.println("hours:");
        System.out.println("    expected: \"" + hours + "\"");
        System.out.println("    actual:   \"" + duration.getHours() + "\"");
    }

    if (DEBUG) {
        System.out.println("minutes:");
        System.out.println("    expected: \"" + minutes + "\"");
        System.out.println("    actual:   \"" + duration.getMinutes() + "\"");
    }

    if (DEBUG) {
        System.out.println("seconds:");
        System.out.println("    expected: \"" + seconds + "\"");
        System.out.println("    actual:   \"" + duration.getSeconds() + "\"");
    }

    if (DEBUG) {
        System.out.println("milliseconds:");
        System.out.println("    expected: \"" + milliseconds + "\"");
        System.out.println("    actual:   \"" + duration.getTimeInMillis(EPOCH) + "\"");
    }

    if (DEBUG) {
        System.out.println("lexical:");
        System.out.println("    expected: \"" + lexical + "\"");
        System.out.println("    actual:   \"" + duration.toString() + "\"");
    }

}