本文整理汇总了Java中javax.xml.datatype.XMLGregorianCalendar.setHour方法的典型用法代码示例。如果您正苦于以下问题:Java XMLGregorianCalendar.setHour方法的具体用法?Java XMLGregorianCalendar.setHour怎么用?Java XMLGregorianCalendar.setHour使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.datatype.XMLGregorianCalendar
的用法示例。
在下文中一共展示了XMLGregorianCalendar.setHour方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toXMLCalendar
import javax.xml.datatype.XMLGregorianCalendar; //导入方法依赖的package包/类
public XMLGregorianCalendar toXMLCalendar(Date d) throws Exception {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
XMLGregorianCalendar xml = DatatypeFactory.newInstance()
.newXMLGregorianCalendar();
xml.setYear(gc.get(Calendar.YEAR));
xml.setMonth(gc.get(Calendar.MONTH) + 1);
xml.setDay(gc.get(Calendar.DAY_OF_MONTH));
xml.setHour(gc.get(Calendar.HOUR_OF_DAY));
xml.setMinute(gc.get(Calendar.MINUTE));
xml.setSecond(gc.get(Calendar.SECOND));
return xml;
}
示例2: testSetHour
import javax.xml.datatype.XMLGregorianCalendar; //导入方法依赖的package包/类
@Test
public final void testSetHour() {
/**
* Hour values to test and expected result.
*/
final int[] TEST_VALUES = {
// setTime(H, M, S), hour override, expected result
0, 0, 0, 0, TEST_VALUE_PASS, 0, 0, 0, 23, TEST_VALUE_PASS, 0, 0, 0, 24, TEST_VALUE_PASS,
// creates invalid state
0, 0, 0, DatatypeConstants.FIELD_UNDEFINED, TEST_VALUE_FAIL,
// violates Schema Errata
0, 0, 1, 24, TEST_VALUE_FAIL };
// create DatatypeFactory
DatatypeFactory datatypeFactory = null;
try {
datatypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException datatypeConfigurationException) {
Assert.fail(datatypeConfigurationException.toString());
}
if (DEBUG) {
System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
}
// create XMLGregorianCalendar
XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar();
// test each value
for (int onTestValue = 0; onTestValue < TEST_VALUES.length; onTestValue = onTestValue + 5) {
if (DEBUG) {
System.err.println("testing values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
+ ", " + TEST_VALUES[onTestValue + 3] + ") expected (0=fail, 1=pass): " + TEST_VALUES[onTestValue + 4]);
}
try {
// set time to known valid value
xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]);
// now explicitly set hour
xmlGregorianCalendar.setHour(TEST_VALUES[onTestValue + 3]);
if (DEBUG) {
System.err.println("XMLGregorianCalendar created: \"" + xmlGregorianCalendar.toString() + "\"");
}
// was this expected to fail?
if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_FAIL) {
Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", "
+ TEST_VALUES[onTestValue + 3] + ") are invalid, " + "yet it created the XMLGregorianCalendar \"" + xmlGregorianCalendar.toString()
+ "\"");
}
} catch (Exception exception) {
if (DEBUG) {
System.err.println("Exception in creating XMLGregorianCalendar: \"" + exception.toString() + "\"");
}
// was this expected to succed?
if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_PASS) {
Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", "
+ TEST_VALUES[onTestValue + 3] + ") are valid yet it failed with \"" + exception.toString() + "\"");
}
// expected failure
}
}
}