Altova XMLSpy 2023 Enterprise Edition

此类可用于处理具有xs:duration类型的XML特性或元素。

 

构造函数


Name

描述

ic_java_constructor

Duration(Duration newvalue)

Duration类的新实例初始化为作为参数提供的Duration对象。

ic_java_constructor

Duration(int newyear, int newmonth, int newday, int newhour, int newminute, int newsecond, double newpartsecond, boolean newisnegative)

Duration类的新实例初始化为由作为参数提供的部分构建的持续时间。

 

方法


Name

描述

ic_java_public_static

static Duration getFromDayTime( int newday, int newhour, int newminute, int newsecond, double newpartsecond )

返回一个Duration对象,该对象从作为参数提供的天数、小时数、分钟数、秒数和小数秒数部分创建。

ic_java_public_static

static Duration getFromYearMonth( int newyear, int newmonth )

返回一个Duration对象,该对象从作为参数提供的年数和月数创建。

ic_java_public_static

static Duration parse( String s )

返回一个从作为参数提供的字符串创建的Duration对象。例如,-P1Y1M1DT1H1M1.333S字符串可用于创建一个表示一年、一个月、一天、一小时、一分钟、一秒以及0.333秒的小数部分的负持续时间。要创建负持续时间,请向字符串添加减号(- )。

ic_java_public_static

static Duration parse( String s, ParseType pt )

使用特定的解析格式返回一个从作为参数提供的字符串创建的Duration对象。解析格式可以是以下之一:

ParseType.DAYTIME

当字符串s由以下任何一项组成时可使用:日、小时、分钟、秒、小数秒部分,例如-P4DT4H4M4.774S。

ParseType.DURATION

当字符串s由以下任何一项组成时可使用:年、月、日、小时、分钟、秒、小数秒部分,例如P1Y1M1DT1H1M1.333S。

ParseType.YEARMONTH

当字符串s由以下任何一项组成时可使用:年,月。例如:P3Y2。

ic_java_public_member

int getDay()

返回当前Duration实例中的天数。

ic_java_public_member

long getDayTimeValue()

返回当前Duration实例的天数和时间值(以毫秒为单位)。年和月被忽略。

ic_java_public_member

int getHour()

返回当前Duration实例中的小时数。

ic_java_public_member

int getMillisecond()

返回当前Duration实例中的毫秒数。

ic_java_public_member

int getMinute()

返回当前Duration实例中的分钟数。

ic_java_public_member

int getMonth()

返回当前Duration实例中的月数。

ic_java_public_member

double getPartSecond()

返回当前Duration实例中的小数秒部分的数量。

ic_java_public_member

int getSecond()

返回当前Duration实例中的秒数。

ic_java_public_member

int getYear()

返回当前Duration实例中的年数。

ic_java_public_member

int getYearMonthValue()

返回当前Duration实例的年和月的值(以月为单位)。日、小时、秒和毫秒被忽略。

ic_java_public_member

boolean isNegative()

如果当前Duration实例为负数,则返回true

ic_java_public_member

void setDayTimeValue(long l)

将持续时间设为作为参数提供的毫秒数,仅影响持续时间的天数和时间部分。

ic_java_public_member

void setNegative( boolean isnegative )

将当前Duration实例转换为负持续时间。

ic_java_public_member

void setYearMonthValue(int l)

将持续时间设为作为参数提供的月数。仅持续时间的年和月部分会受到影响。

ic_java_public_member

String toString()

返回当前Duration实例的字符串表示形式,例如:

 

-P4DT4H4M4.774S

ic_java_public_member

String toYearMonthString()

返回当前Duration实例的YearMonth部分的字符串表示形式,例如:

 

P1Y2M

 

示例

在您的程序中使用以下代码片段之前,请确保已导入Altova类型:

 

import com.altova.types.*;
import com.altova.types.Duration.ParseType;

 

以下代码片段展示了创建Duration对象的各种方法:

 

protected static void ExampleDuration()
{
  // Create a negative duration of 1 year, 1 month, 1 day, 1 hour, 1 minute, 1 second,
  // and 0.333 fractional second parts
  Duration dr = new Duration(1, 1, 1, 1, 1, 1, .333, true);
     
  // Create a duration from an existing Duration object
  Duration dr1 = new Duration(dr);
     
  // Create a duration of 4 days, 4 hours, 4 minutes, 4 seconds, .774 fractional second parts
  Duration dr2 = Duration.getFromDayTime(4, 4, 4, 4, .774);
     
  // Create a duration of 3 years and 2 months
  Duration dr3 = Duration.getFromYearMonth(3, 2);
     
  // Create a duration from a string
  Duration dr4 = Duration.parse("-P4DT4H4M4.774S");      
     
  // Create a duration from a string, using specific parse formats
  Duration dr5 = Duration.parse("-P1Y1M1DT1H1M1.333S", ParseType.DURATION);
  Duration dr6 = Duration.parse("P3Y2M", ParseType.YEARMONTH);
  Duration dr7 = Duration.parse("-P4DT4H4M4.774S", ParseType.DAYTIME);
}

 

以下代码片段展示了如何获取和设置Duration对象的值:

 

protected static void DurationExample2()
{
  // Create a duration of 1 year, 2 month, 3 days, 4 hours, 5 minutes, 6 seconds,
      // and 333 milliseconds
  Duration dr = new Duration(1, 2, 3, 4, 5, 6, .333, false);
  // Output the number of days in this duration
  System.out.println(dr.getDay());
     
  // Create a positive duration of one year and 333 milliseconds
  Duration dr1 = new Duration(1, 0, 0, 0, 0, 0, .333, false);
  // Output the day and time value in milliseconds
  System.out.println(dr1.getDayTimeValue());
     
  // Create a positive duration of 1 year, 1 month, 1 day, 1 hour, 1 minute, 1 second,
      // and 333 milliseconds
  Duration dr2 = new Duration(1, 1, 1, 1, 1, 1, .333, false);
  // Output the year and month value in months
  System.out.println(dr2.getYearMonthValue());
           
  // Create a positive duration of 1 year and 1 month
  Duration dr3 = new Duration(1, 1, 0, 0, 0, 0, 0, false);
  // Output the value
  System.out.println("The duration is now: " + dr3.toString());
  // Set the DayTime part of duration to 1000 milliseconds
  dr3.setDayTimeValue(1000);
  // Output the value
  System.out.println("The duration is now: " + dr3.toString());
  // Set the YearMonth part of duration to 1 month
  dr3.setYearMonthValue(1);
  // Output the value
  System.out.println("The duration is now: " + dr3.toString());
  // Output the year and month part of the duration
  System.out.println("The YearMonth part of the duration is: " + dr3.toYearMonthString());
}

© 2017-2023 Altova GmbH