Altova XMLSpy 2023 Enterprise Edition

此类可用于处理具有日期和时间类型(例如xs:dateTime)的XML特性或元素。

 

构造函数


Name

描述

ic_java_constructor

public DateTime()

DateTime类的新实例初始化为空值。

ic_java_constructor

public DateTime(DateTime newvalue)

DateTime类的新实例初始化为作为参数提供的DateTime值。

ic_java_constructor

public DateTime(int newyear, int newmonth, int newday, int newhour, int newminute, int newsecond, double newpartsecond, int newoffsetTZ)

DateTime类的新实例初始化为作为参数提供的年、月、日、小时、分钟、秒、秒的小数部分和时区。秒的小数部分newpartsecond必须在0和1之间。时区偏移量newoffsetTZ可以是整数或负数,以分钟为单位。

ic_java_constructor

public DateTime(int newyear, int newmonth, int newday, int newhour, int newminute, int newsecond, double newpartsecond)

DateTime类的新实例初始化为作为参数提供的年、月、日、小时、分钟、秒和秒的小数部分。

ic_java_constructor

public DateTime(int newyear, int newmonth, int newday)

DateTime类的新实例初始化为作为参数提供的年、月和日。

ic_java_constructor

public DateTime(Calendar newvalue)

DateTime类的新实例初始化为作为参数提供的java.util.Calendar值。

 

方法


Name

描述

ic_java_public_static

static DateTime now()

返回当前时间作为DateTime对象。

ic_java_public_static

static DateTime parse(String s)

返回一个从作为参数提供的字符串值解析的DateTime对象。例如,以下示例字符串值将被成功转换为一个DateTime对象:

 

2015-11-24T12:54:47.969+01:00

2015-11-24T12:54:47

2015/11/24

ic_java_public_member

int getDay()

返回当前DateTime实例所对应的星期几。

ic_java_public_member

int getHour()

返回当前DateTime实例的小时数。

ic_java_public_member

int getMillisecond()

以整数值形式返回当前DateTime实例的毫秒数。

ic_java_public_member

int getMinute()

返回当前DateTime实例的分钟数。

ic_java_public_member

int getMonth()

返回当前DateTime实例的月份。

ic_java_public_member

double getPartSecond()

返回当前DateTime实例的秒的小数部分,作为double值。返回值大于0且小于1,例如:

 

0.313

ic_java_public_member

int getSecond()

返回当前DateTime实例的秒数。

ic_java_public_member

int getTimezoneOffset()

返回当前DateTime实例的时区偏移量(以分钟为单位)。例如,“UTC-01:00”时区将被返回为:

 

-60

ic_java_public_member

Calendar getValue()

java.util.Calendar值的形式返回当前DateTime实例。

ic_java_public_member

int getWeekday()

返回当前DateTime实例所对应的星期几。返回值为0到6,其中0表示周一(ISO-8601)。

ic_java_public_member

int getYear()

返回当前DateTime实例的年份。

ic_java_public_member

int hasTimezone()

返回当前DateTime实例的时区信息。可能的返回值有:

CalendarBase.TZ_MISSING

未定义时区偏移量。

CalendarBase.TZ_UTC

时区为UTC。

CalendarBase.TZ_OFFSET

已定义时区偏移量。

ic_java_public_member

void setDay( int nDay )

将当前DateTime实例的日期设为作为参数提供的值。

ic_java_public_member

void setHasTimezone( int nHasTZ )

将当前DateTime实例的时区信息设为作为参数提供的值。此方法可用于除去时区信息,或将时区设为UTC(协调世界时)。nHasTZ参数的有效值:

CalendarBase.TZ_MISSING

将时区偏移量设为未定义。

CalendarBase.TZ_UTC

将时区设为UTC。

CalendarBase.TZ_OFFSET

如果当前对象有一个时区偏移,则保持不变。

ic_java_public_member

void setHour( int nHour )

将当前DateTime实例的小时数设为作为参数提供的值。

ic_java_public_member

void setMinute( int nMinute )

将当前DateTime实例的分钟数设为作为参数提供的值。

ic_java_public_member

void setMonth( int nMonth )

将当前DateTime实例的月份设为作为参数提供的值。

ic_java_public_member

void setPartSecond( double nPartSecond )

将当前DateTime实例的秒的小数部分设为作为参数提供的值。

ic_java_public_member

void setSecond( int nSecond )

将当前DateTime实例的秒数设为作为参数提供的值。

ic_java_public_member

void setTimezoneOffset( int nOffsetTZ )

将当前DateTime实例的时区偏移量设为作为参数提供的值。nOffsetTZ值必须是一个整数(正数或负数)并且以分钟为单位。

ic_java_public_member

void setYear( int nYear )

将当前DateTime实例的年份设为作为参数提供的值。

ic_java_public_member

String toString()

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

 

2015-11-24T15:50:56.968+01:00

 

示例

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

 

import com.altova.types.*;

 

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

 

protected static void DateTimeExample1()
{
  // Initialize a new instance of the DateTime class to the current time
  DateTime dt = new DateTime(DateTime.now());
  System.out.println("DateTime created from current date and time: " + dt.toString());
     
  // Initialize a new instance of the DateTime class by supplying the parts
  DateTime dt1 = new DateTime(2015, 11, 23, 14, 30, 24, .459);
  System.out.println("DateTime from parts (no timezone): " + dt1.toString());
 
  // Initialize a new instance of the DateTime class by supplying the parts
  DateTime dt2 = new DateTime(2015, 11, 24, 14, 30, 24, .459, -60);
  System.out.println("DateTime from parts (with negative timezone): " + dt2.toString());
     
  // Initialize a new instance of the DateTime class by parsing a string value        

  DateTime dt3 = DateTime.parse("2015-11-24T12:54:47.969+01:00");          
  System.out.println("DateTime parsed from string: " + dt3.toString());        
}      

 

以下代码片段展示了如何从DateTime对象获取值:

 

protected static void DateTimeExample2()
  {
    // Initialize a new instance of the DateTime class to the current time
    DateTime dt = new DateTime(DateTime.now());
       
    // Output the formatted year, month, and day of this DateTime instance
    String str1 = String.format("Year: %d; Month: %d; Day: %d;", dt.getYear(), dt.getMonth(), dt.getDay());
    System.out.println(str1);
       
    // Output the formatted hour, minute, and second of this DateTime instance
    String str2 = String.format("Hour: %d; Minute: %d; Second: %d;", dt.getHour(), dt.getMinute(), dt.getSecond());
    System.out.println(str2);
       
    // Return the timezone (in minutes) of this DateTime instance
    System.out.println("Timezone: " + dt.getTimezoneOffset());
       
    // Get the DateTime as a java.util.Calendar value
    java.util.Calendar dt_java = dt.getValue();
    System.out.println("" + dt_java.toString());
       
    // Return the day of week of this DateTime instance
    System.out.println("Weekday: " + dt.getWeekday());        
     
    // Check whether the DateTime instance has a timezone defined
    switch(dt.hasTimezone())
    {
        case CalendarBase.TZ_MISSING:
          System.out.println("No timezone.");
          break;
        case CalendarBase.TZ_UTC:            
          System.out.println("The timezone is UTC.");
          break;
        case CalendarBase.TZ_OFFSET:
          System.out.println("This object has a timezone.");
          break;
        default:
          System.out.println("Unable to determine whether a timezone is defined.");
          break;
    }      
  }

 

以下代码片段展示了如何更改DateTime对象的时区偏移:

 

protected static void DateTimeExample3()
{      
  // Create a new DateTime object with timezone -0100 UTC
  DateTime dt = new DateTime(2015, 11, 24, 14, 30, 24, .459, -60);
  // Output the value before the change
  System.out.println("Before: " + dt.toString());
  // Change the offset to +0100 UTC
  dt.setTimezoneOffset(60);            
  // Output the value after the change
  System.out.println("After:  " + dt.toString());
}

© 2017-2023 Altova GmbH