Altova MapForce 2025 Enterprise Edition

This class enables you to process XML attributes or elements of type xs:duration.

 

Constructors


Name

Description

ic_java_constructor

Duration(Duration newvalue)

Initializes a new instance of the Duration class to the Duration object supplied as argument.

ic_java_constructor

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

Initializes a new instance of the Duration class to a duration built from parts supplied as arguments.

 

Methods


Name

Description

ic_java_public_static

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

Returns a Duration object created from the number of days, hours, minutes, seconds, and fractional second parts supplied as argument.

ic_java_public_static

static Duration getFromYearMonth( int newyear, int newmonth )

Returns a Duration object created from the number of years and months supplied as argument.

ic_java_public_static

static Duration parse( String s )

Returns a Duration object created from the string supplied as argument. For example, the string -P1Y1M1DT1H1M1.333S can be used to create a negative duration of one year, one month, one day, one hour, one minute, one second, and 0.333 fractional parts of a second. To create a negative duration, append the minus sign ( - ) to the string.

ic_java_public_static

static Duration parse( String s, ParseType pt )

Returns a Duration object created from the string supplied as argument, using a specific parse format. The parse format can be any of the following:

ParseType.DAYTIME

May be used when the string s consists of any of the following: days, hours, minutes, seconds, fractional second parts, for example -P4DT4H4M4.774S.

ParseType.DURATION

May be used when the string s consists of any of the following: years, months, days, hours, minutes, seconds, fractional second parts, for example P1Y1M1DT1H1M1.333S.

ParseType.YEARMONTH

May be used when the string s consists of any of the following: years, months. For example: P3Y2M.

ic_java_public_member

int getDay()

Returns the number of days in the current Duration instance.

ic_java_public_member

long getDayTimeValue()

Returns the day and time value (in milliseconds) of the current Duration instance. Years and months are ignored.

ic_java_public_member

int getHour()

Returns the number of hours in the current Duration instance.

ic_java_public_member

int getMillisecond()

Returns the number of milliseconds in the current Duration instance.

ic_java_public_member

int getMinute()

Returns the number of minutes in the current Duration instance.

ic_java_public_member

int getMonth()

Returns the number of months in the current Duration instance.

ic_java_public_member

double getPartSecond()

Returns the number of fractional second parts in the current Duration instance.

ic_java_public_member

int getSecond()

Returns the number of seconds in the current Duration instance.

ic_java_public_member

int getYear()

Returns the number of years in the current Duration instance.

ic_java_public_member

int getYearMonthValue()

Returns the year and month value (in months) of the current Duration instance. Days, hours, seconds, and milliseconds are ignored.

ic_java_public_member

boolean isNegative()

Returns Boolean true if the current Duration instance is negative.

ic_java_public_member

void setDayTimeValue(long l)

Sets the duration to the number of milliseconds supplied as argument, affecting only the day and time part of the duration.

ic_java_public_member

void setNegative( boolean isnegative )

Converts the current Duration instance to a negative duration.

ic_java_public_member

void setYearMonthValue(int l)

Sets the duration to the number of months supplied as argument. Only the years and months part of the duration is affected.

ic_java_public_member

String toString()

Returns the string representation of the current Duration instance, for example:

 

-P4DT4H4M4.774S

ic_java_public_member

String toYearMonthString()

Returns the string representation of the YearMonth part of the current Duration instance, for example:

 

P1Y2M

 

Examples

Before using the following code listings in your program, ensure the Altova types are imported:

 

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

 

The following code listing illustrates various ways to create Duration objects:

 

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);
}

 

The following code listing illustrates getting and setting the value of Duration objects:

 

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());
}

© 2018-2024 Altova GmbH