本文整理汇总了Java中javax.xml.datatype.Duration类的典型用法代码示例。如果您正苦于以下问题:Java Duration类的具体用法?Java Duration怎么用?Java Duration使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Duration类属于javax.xml.datatype包,在下文中一共展示了Duration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDurationAndField
import javax.xml.datatype.Duration; //导入依赖的package包/类
@DataProvider(name = "duration-field")
public Object[][] getDurationAndField() {
Function<Duration, Integer> getyears = duration -> duration.getYears();
Function<Duration, Integer> getmonths = duration -> duration.getMonths();
Function<Duration, Integer> getdays = duration -> duration.getDays();
Function<Duration, Integer> gethours = duration -> duration.getHours();
Function<Duration, Integer> getminutes = duration -> duration.getMinutes();
Function<Duration, Integer> getseconds = duration -> duration.getSeconds();
return new Object[][] {
{ "P1Y1M1DT1H1M1S", getyears, 1 },
{ "P1M1DT1H1M1S", getyears, 0 },
{ "P1Y1M1DT1H1M1S", getmonths, 1 },
{ "P1Y1DT1H1M1S", getmonths, 0 },
{ "P1Y1M1DT1H1M1S", getdays, 1 },
{ "P1Y1MT1H1M1S", getdays, 0 },
{ "P1Y1M1DT1H1M1S", gethours, 1 },
{ "P1Y1M1DT1M1S", gethours, 0 },
{ "P1Y1M1DT1H1M1S", getminutes, 1 },
{ "P1Y1M1DT1H1S", getminutes, 0 },
{ "P1Y1M1DT1H1M1S", getseconds, 1 },
{ "P1Y1M1DT1H1M", getseconds, 0 },
{ "P1Y1M1DT1H1M100000000S", getseconds, 100000000 }, };
}
示例2: testDurationAndCalendar1
import javax.xml.datatype.Duration; //导入依赖的package包/类
@Test
public void testDurationAndCalendar1() {
int year = 1;
int month = 2;
int day = 3;
int hour = 4;
int min = 5;
int sec = 6;
String lexicalRepresentation = "P" + year + "Y" + month + "M" + day + "DT" + hour + "H" + min + "M" + sec + "S";
try {
Duration dur = DatatypeFactory.newInstance().newDuration(lexicalRepresentation);
System.out.println(dur.toString());
AssertJUnit.assertTrue("year should be 1", dur.getYears() == year);
AssertJUnit.assertTrue("month should be 2", dur.getMonths() == month);
AssertJUnit.assertTrue("day should be 3", dur.getDays() == day);
AssertJUnit.assertTrue("hour should be 4", dur.getHours() == hour);
AssertJUnit.assertTrue("minute should be 5", dur.getMinutes() == min);
AssertJUnit.assertTrue("second should be 6", dur.getSeconds() == sec);
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
}
示例3: normalizeToTimezone
import javax.xml.datatype.Duration; //导入依赖的package包/类
/**
* <p>Normalize this instance to UTC.</p>
*
* <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
* <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
*/
private XMLGregorianCalendar normalizeToTimezone(int timezone) {
int minutes = timezone;
XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();
// normalizing to UTC time negates the timezone offset before
// addition.
minutes = -minutes;
Duration d = new DurationImpl(minutes >= 0, // isPositive
0, //years
0, //months
0, //days
0, //hours
minutes < 0 ? -minutes : minutes, // absolute
0 //seconds
);
result.add(d);
// set to zulu UTC time.
result.setTimezone(0);
return result;
}
示例4: testNewDurationYearMonthLexicalRepresentation
import javax.xml.datatype.Duration; //导入依赖的package包/类
@Test
public void testNewDurationYearMonthLexicalRepresentation() throws DatatypeConfigurationException {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationYearMonth("P20Y15M");
int years = d.getYears();
Assert.assertTrue(years == 21, "Return value should be normalized");
}
示例5: isDateTimeOrDuration
import javax.xml.datatype.Duration; //导入依赖的package包/类
private boolean isDateTimeOrDuration(Object value)
{
return value instanceof Duration || value instanceof XMLGregorianCalendar;
}
示例6: getInputArgument
import javax.xml.datatype.Duration; //导入依赖的package包/类
private Object getInputArgument(Class<?> clazz,
boolean testSettersAndGetters) throws Exception {
if (clazz.isAssignableFrom(String.class)) {
return new String("Dummy Value String");
} else if (clazz.isAssignableFrom(XMLGregorianCalendar.class)) {
return DatatypeFactory.newInstance().newXMLGregorianCalendar();
} else if (clazz.isAssignableFrom(Boolean.class)) {
return new Boolean(true);
} else if (clazz.isAssignableFrom(Integer.class)) {
return new Integer(5);
} else if (clazz.isAssignableFrom(BigInteger.class)) {
return new BigInteger("5");
} else if (clazz.isAssignableFrom(byte[].class)) {
return new byte[5];
} else if (clazz.isAssignableFrom(Duration.class)) {
return DatatypeFactory.newInstance().newDuration(100L);
} else if (clazz.isEnum()) {
return clazz.getEnumConstants()[0];
} else {
Object typeObject = createTypeFromObjectFactory(clazz);
if (testSettersAndGetters) {
testSettersAndGetters(typeObject);
}
return typeObject;
}
}
示例7: getDuration
import javax.xml.datatype.Duration; //导入依赖的package包/类
protected Duration getDuration(DateTimeData date) {
int sign = 1;
if (date.day<0 || date.hour<0 || date.minute<0 || date.second<0) {
sign = -1;
}
return datatypeFactory.newDuration(sign == 1, null, null,
date.day != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.day):null,
date.hour != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.hour):null,
date.minute != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.minute):null,
date.second != DatatypeConstants.FIELD_UNDEFINED?new BigDecimal(String.valueOf(sign*date.second)):null);
}
示例8: add
import javax.xml.datatype.Duration; //导入依赖的package包/类
private Date add(Date date, Duration duration) {
if (!useBusinessTime) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
duration.addTo(calendar);
return calendar.getTime();
}
return businessCalendar.add(date, duration, useBusinessTime);
}
示例9: durationToTimespan
import javax.xml.datatype.Duration; //导入依赖的package包/类
/** @return kdb {@link Timespan} equivalent to XML duration */
public static Timespan durationToTimespan(Duration xmlDuration) {
if(xmlDuration == null)
return new Timespan(0l);
return new Timespan(NANO_SECONDS_IN_1_MS * xmlDuration.getTimeInMillis(DateTime.now().toDate()));
}
示例10: testNewDurationYearMonthMilliseconds
import javax.xml.datatype.Duration; //导入依赖的package包/类
@Test
public void testNewDurationYearMonthMilliseconds() throws DatatypeConfigurationException {
DatatypeFactory dtf = DatatypeFactory.newInstance();
Duration d = dtf.newDurationYearMonth(671976000000L);
int years = d.getYears();
System.out.println("Years: " + years);
Assert.assertTrue(years == 21, "Return value should be normalized");
}
示例11: getDuration
import javax.xml.datatype.Duration; //导入依赖的package包/类
protected Duration getDuration(DateTimeData date) {
int sign = 1;
if ( date.year<0 || date.month<0) {
sign = -1;
}
return datatypeFactory.newDuration(sign == 1,
date.year != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.year):null,
date.month != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.month):null,
null,
null,
null,
null);
}
示例12: checkDurationAdd
import javax.xml.datatype.Duration; //导入依赖的package包/类
@Test(dataProvider = "duration-for-add")
public void checkDurationAdd(String initVal, String addVal, String result) {
Duration durationInit = datatypeFactory.newDuration(initVal);
Duration durationAdd = datatypeFactory.newDuration(addVal);
Duration durationResult = datatypeFactory.newDuration(result);
assertEquals(durationInit.add(durationAdd), durationResult);
}
示例13: checkDurationToString
import javax.xml.datatype.Duration; //导入依赖的package包/类
@Test(dataProvider = "number-string")
public void checkDurationToString(boolean isPositive, int years, int months, int days, int hours, int minutes, int seconds, String lexical) {
Duration duration = datatypeFactory.newDuration(isPositive, years, months, days, hours, minutes, seconds);
assertEquals(duration.toString(), lexical);
assertEquals(datatypeFactory.newDuration(duration.toString()), duration);
}
示例14: normalizeWith
import javax.xml.datatype.Duration; //导入依赖的package包/类
/**
* <p>Converts the years and months fields into the days field
* by using a specific time instant as the reference point.</p>
*
* <p>For example, duration of one month normalizes to 31 days
* given the start time instance "July 8th 2003, 17:40:32".</p>
*
* <p>Formally, the computation is done as follows:</p>
* <ol>
* <li>The given Calendar object is cloned.
* <li>The years, months and days fields will be added to
* the {@link Calendar} object
* by using the {@link Calendar#add(int,int)} method.
* <li>The difference between two Calendars are computed in terms of days.
* <li>The computed days, along with the hours, minutes and seconds
* fields of this duration object is used to construct a new
* Duration object.
* </ol>
*
* <p>Note that since the Calendar class uses <code>int</code> to
* hold the value of year and month, this method may produce
* an unexpected result if this duration object holds
* a very large value in the years or months fields.</p>
*
* @param startTimeInstant <code>Calendar</code> reference point.
*
* @return <code>Duration</code> of years and months of this <code>Duration</code> as days.
*
* @throws NullPointerException If the startTimeInstant parameter is null.
*/
public Duration normalizeWith(Calendar startTimeInstant) {
Calendar c = (Calendar) startTimeInstant.clone();
// using int may cause overflow, but
// Calendar internally treats value as int anyways.
c.add(Calendar.YEAR, getYears() * signum);
c.add(Calendar.MONTH, getMonths() * signum);
c.add(Calendar.DAY_OF_MONTH, getDays() * signum);
// obtain the difference in terms of days
long diff = getCalendarTimeInMillis(c) - getCalendarTimeInMillis(startTimeInstant);
int days = (int) (diff / (1000L * 60L * 60L * 24L));
return new DurationImpl(
days >= 0,
null,
null,
wrap(Math.abs(days)),
(BigInteger) getField(DatatypeConstants.HOURS),
(BigInteger) getField(DatatypeConstants.MINUTES),
(BigDecimal) getField(DatatypeConstants.SECONDS));
}
示例15: testNewDurationYearMonthBigInteger
import javax.xml.datatype.Duration; //导入依赖的package包/类
@Test
public void testNewDurationYearMonthBigInteger() throws DatatypeConfigurationException {
DatatypeFactory dtf = DatatypeFactory.newInstance();
BigInteger year = new BigInteger("20");
BigInteger mon = new BigInteger("15");
Duration d = dtf.newDurationYearMonth(true, year, mon);
int years = d.getYears();
Assert.assertTrue(years == 21, "Return value should be normalized");
}