當前位置: 首頁>>代碼示例>>Java>>正文


Java Duration類代碼示例

本文整理匯總了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 }, };
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:DurationTest.java

示例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();
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:DurationTest.java

示例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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:29,代碼來源:XMLGregorianCalendarImpl.java

示例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");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:Bug6937964Test.java

示例5: isDateTimeOrDuration

import javax.xml.datatype.Duration; //導入依賴的package包/類
private boolean isDateTimeOrDuration(Object value)
{
   return value instanceof Duration || value instanceof XMLGregorianCalendar;
}
 
開發者ID:dmn-tck,項目名稱:tck,代碼行數:5,代碼來源:DroolsTCKTest.java

示例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;
    }
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:28,代碼來源:ObjectFactoryTest.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:DayTimeDurationDV.java

示例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);
}
 
開發者ID:zhaojunfei,項目名稱:lemon,代碼行數:12,代碼來源:DurationUtil.java

示例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()));
}
 
開發者ID:BuaBook,項目名稱:java-kdb-communication,代碼行數:8,代碼來源:Converters.java

示例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");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:Bug6937964Test.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:YearMonthDurationDV.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:DurationTest.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:DurationTest.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:54,代碼來源:DurationImpl.java

示例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");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:Bug6937964Test.java


注:本文中的javax.xml.datatype.Duration類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。