public class DateTime extends Object implements Comparable<DateTime>
This primitive DataType is used to define Coordinated Universal Time (UTC) values. All time values conveyed between servers and clients in OPC UA are UTC values. Clients must provide any conversions between UTC and local time. This DataType is represented as a 64-bit signed integer which represents the number of 100 nanosecond intervals since January 1, 1601. [UA Part 6] defines details about this DataType.
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_STR_FORMAT
The default String format used in toString.
|
static DateTime[] |
EMPTY_ARRAY |
static NodeId |
ID
Deprecated.
use
Identifiers.DateTime instead. |
static String |
ISO_8601_STR_FORMAT
ISO 8601 date format.
|
static Instant |
MAX
Maximum possible time to represent in OPC UA.
|
static DateTime |
MAX_VALUE
Maximum possible time to represent in OPC UA.
|
static Instant |
MIN
Minimum possible time to represent in OPC UA.
|
static DateTime |
MIN_VALUE
Minimum possible time to represent in OPC UA.
|
static long |
OffsetToGregorianCalendarZero
Constant used to convert Java Epoch (1970/1/1) and OPC UA Epoch (1601/1/1) timestamps.
|
| Constructor and Description |
|---|
DateTime()
Deprecated.
use
currentTime() instead |
DateTime(Calendar value)
Deprecated.
use
fromInstant(Instant) instead and Java8 time APIs |
DateTime(int year,
int month,
int day,
int hour,
int minute,
int second)
Deprecated.
use
fromInstant(Instant) instead and Java8 time APIs |
DateTime(int year,
int month,
int day,
int hour,
int minute,
int second,
int nanosecond)
Deprecated.
use
fromInstant(Instant) instead and Java8 time APIs |
DateTime(int year,
int month,
int day,
int hour,
int minute,
int second,
int nanosecond,
TimeZone timeZone)
Deprecated.
use
fromInstant(Instant) instead and Java8 time APIs |
DateTime(long value)
Deprecated.
use
valueOf(long) instead. |
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(DateTime other) |
static DateTime |
currentTime()
Returns an instance of
DateTime that represents the current time. |
boolean |
equals(Object obj) |
static DateTime |
fromInstant(Instant value)
|
static DateTime |
fromMillis(long value)
Create a new DateTime instance from standard Java milliseconds since the beginning of epoch,
January 1, 1970 UTC.
|
GregorianCalendar |
getCalendar(TimeZone timezone)
Deprecated.
use
toInstant() and Java 8 Time APIs instead. |
GregorianCalendar |
getLocalCalendar()
Deprecated.
use
toInstant() and Java 8 Time APIs instead. |
static String |
getStrFormat()
Get the format used for toString().
|
long |
getTimeInMillis()
Get standard Java milliseconds between the time and midnight, January 1, 1970 UTC.
|
GregorianCalendar |
getUtcCalendar()
Deprecated.
use
toInstant() and Java 8 Time APIs instead. |
long |
getValue()
Get 1/10 microseconds (1 unit = 100 nanoseconds) between the time and midnight, January 1, 1601
UTC.
|
int |
hashCode() |
boolean |
isAfter(DateTime other)
Returns true if this DateTime is after the given DateTime.
|
boolean |
isAfterOrAt(DateTime other)
Returns true if this DateTime is after or at the same time as the given DateTime.
|
boolean |
isBefore(DateTime other)
Returns true if this DateTime is before the given DateTime.
|
boolean |
isBeforeOrAt(DateTime other)
Returns true if this DateTime is before or at the same time as the given DateTime.
|
static boolean |
isUseLocalTimeInToString()
If this is true, then DateTime.toString should print local timestamps and Utc if this is false.
|
DateTime |
minus(long timeToSubtract,
TemporalUnit temporalUnit)
Create a new DateTime instance that corresponds to a moment in time before this instance.
|
DateTime |
minus(long timeToSubtract,
TimeUnit timeUnits)
Create a new DateTime instance that corresponds to a moment in time a number of timeUnits
before this instance.
|
DateTime |
minus(TemporalAmount amountToSubtract)
Create a new DateTime instance that corresponds to a moment in time before this instance.
|
DateTime |
minusMillis(double millisToSubtract)
Create a new DateTime instance that corresponds to a moment in time a number of milliseconds
before this instance.
|
DateTime |
minusMillis(long millisToSubtract)
Create a new DateTime instance that corresponds to a moment in time a number of milliseconds
before this instance.
|
DateTime |
minusNanos(long nanosToSubtract)
Create a new DateTime instance that corresponds to a moment in time a number of nanoseconds
before this instance.
|
static DateTime |
parseDateTime(String string)
Parse a DateTime value from a String.
|
DateTime |
plus(long timeToAdd,
TemporalUnit temporalUnit)
Create a new DateTime instance that corresponds to a moment in time after this instance.
|
DateTime |
plus(long timeToAdd,
TimeUnit timeUnits)
Create a new DateTime instance that corresponds to a moment in time a number of timeUnits after
this instance.
|
DateTime |
plus(TemporalAmount amountToAdd)
Create a new DateTime instance that corresponds to a moment in time before this instance.
|
DateTime |
plusMillis(double millisToAdd)
Create a new DateTime instance that corresponds to a moment in time a number of milliseconds
after this instance.
|
DateTime |
plusMillis(long millisToAdd)
Create a new DateTime instance that corresponds to a moment in time a number of milliseconds
after this instance.
|
DateTime |
plusNanos(long nanosToAdd)
Create a new DateTime instance that corresponds to a moment in time a number of nanoseconds
after this instance.
|
static void |
setStrFormat(String strFormat)
Set the format used for toString().
|
static void |
setUseLocalTimeInToString(boolean useLocalTimeInToString)
If this is true, then DateTime.toString should print local timestamps and Utc if this is false.
|
Instant |
toInstant()
Returns the time as
Instant. |
String |
toString()
toString.
|
static DateTime |
valueOf(long uaEpoch100Nanoseconds)
Creates DateTime from the given 100-nanoseconds from 'OPC UA Epoch' (January 1, 1601).
|
public static final String DEFAULT_STR_FORMAT
DEFAULT_STR_FORMAT="%TD %TT.%07d %TZ"public static final String ISO_8601_STR_FORMAT
ISO_8601_STR_FORMAT="%TF %TT.%07d %TZ"public static final DateTime[] EMPTY_ARRAY
@Deprecated public static final NodeId ID
Identifiers.DateTime instead.public static final long OffsetToGregorianCalendarZero
Note that value is in OPC UA resolution of 100 nanoseconds. Constant
OffsetToGregorianCalendarZero=116444736000000000L
public static final DateTime MIN_VALUE
public static final DateTime MAX_VALUE
public static final Instant MIN
Corresponds to MIN_VALUE
@Deprecated public DateTime()
currentTime() instead@Deprecated public DateTime(Calendar value)
fromInstant(Instant) instead and Java8 time APIsvalue - a Calendar object.@Deprecated public DateTime(int year, int month, int day, int hour, int minute, int second)
fromInstant(Instant) instead and Java8 time APIs
Creates a GregorianCalendar using the parameters year..second.
Note: that month is 0-based, and the values correspond to the constants, Calendar.JANUARY, etc.
year - a int.month - a int.day - a int.hour - a int.minute - a int.second - a int.@Deprecated public DateTime(int year, int month, int day, int hour, int minute, int second, int nanosecond)
fromInstant(Instant) instead and Java8 time APIs
Creates a GregorianCalendar using the parameters year..second and adds nanoSeconds.
Note: that month is 0-based, and the values correspond to the constants, Calendar.JANUARY, etc.
year - a int.month - a int.day - a int.hour - a int.minute - a int.second - a int.nanosecond - a int.@Deprecated public DateTime(int year, int month, int day, int hour, int minute, int second, int nanosecond, TimeZone timeZone)
fromInstant(Instant) instead and Java8 time APIs
Creates a GregorianCalendar using the parameters year..second; sets the TimeZone and
adds nanoSeconds.
Note: that month is 0-based, and the values correspond to the constants, Calendar.JANUARY, etc.
year - a int.month - a int.day - a int.hour - a int.minute - a int.second - a int.nanosecond - a int.timeZone - a TimeZone object.@Deprecated public DateTime(long value)
valueOf(long) instead.public static DateTime currentTime()
DateTime that represents the current time.public static DateTime fromInstant(Instant value)
DateTime representing the same time as the given Instant.
Note that the resolution of DateTime is in 100 nanoseconds (from the OPC UA Epoch) and
thus cannot fully represent all Instants. The value is truncated to 100 nanosecond
resolution. Additionally values outside of MIN-MAX are clamped to the
boundary values.
Returns null if given null.
public static DateTime fromMillis(long value)
value - a long.DateTime object.public static String getStrFormat()
public static boolean isUseLocalTimeInToString()
public static DateTime parseDateTime(String string) throws ParseException
string - the String to parseMIN_VALUE and MAX_VALUE are truncated to the limits.ParseException - if the DateTime cannot be parsed from stringpublic static void setStrFormat(String strFormat)
strFormat - new formatpublic static void setUseLocalTimeInToString(boolean useLocalTimeInToString)
useLocalTimeInToString - a boolean.public static DateTime valueOf(long uaEpoch100Nanoseconds)
MIN_VALUE and are clamped to the boundary
values.public int compareTo(DateTime other)
compareTo in interface Comparable<DateTime>@Deprecated public GregorianCalendar getCalendar(TimeZone timezone)
toInstant() and Java 8 Time APIs instead.timezone - The desired TimeZone for the calendar@Deprecated public GregorianCalendar getLocalCalendar()
toInstant() and Java 8 Time APIs instead.getLocalCalendar.
public long getTimeInMillis()
toInstant() and then Instant.toEpochMilli().@Deprecated public GregorianCalendar getUtcCalendar()
toInstant() and Java 8 Time APIs instead.getUtcCalendar.
public long getValue()
DateTime using OPC UA Binary encoding rules.public boolean isAfter(DateTime other)
other - the other DateTimepublic boolean isAfterOrAt(DateTime other)
other - the other DateTimepublic boolean isBefore(DateTime other)
other - the other DateTimepublic boolean isBeforeOrAt(DateTime other)
other - the other DateTimepublic DateTime minus(long timeToSubtract, TemporalUnit temporalUnit)
timeToSubtract - time in the specified units to subtract from this time.temporalUnit - time units to use.DateTime instance.public DateTime minus(long timeToSubtract, TimeUnit timeUnits)
timeToSubtract - time in timeUnits to subtract from this time.timeUnits - time units.DateTime instance.public DateTime minus(TemporalAmount amountToSubtract)
amountToSubtract - the amount to subtract from this time.DateTime instance.public DateTime minusMillis(double millisToSubtract)
The method is provided, since OPC UA handles duration as double and this method enables direct use of those in context of DateTime that enables 100 nanosecond accuracy in practice.
millisToSubtract - milliseconds to subtract from this time. Fractions are used to provide
100 ns accuracy that is native to DateTime.DateTime instance.public DateTime minusMillis(long millisToSubtract)
millisToSubtract - milliseconds to subtract from this time.DateTime instance.public DateTime minusNanos(long nanosToSubtract)
Note that the actual resolution of DateTime is 100 nanoseconds, so the given value is truncated to 100 nanosecond accuracy in practice.
nanosToSubtract - nanoseconds to subtract from this time.DateTime instance.public DateTime plus(long timeToAdd, TemporalUnit temporalUnit)
This is a convenience method that is implemented using Instant as follows
return fromInstant(toInstant().plus(timeToAdd, temporalUnit));
See the documentation for Instant for other options that you can use to work with the
Instant timestamps.
timeToAdd - time in the specified units to add to this time.temporalUnit - time units to use.DateTime instance.public DateTime plus(long timeToAdd, TimeUnit timeUnits)
timeToAdd - time in the specified units to add to this time.timeUnits - time units to use.DateTime instance.public DateTime plus(TemporalAmount amountToAdd)
amountToAdd - the temporal amount to add to this time.DateTime instance.public DateTime plusMillis(double millisToAdd)
The method is provided, since OPC UA handles duration as double and this method enables direct use of those in context of DateTime that enables 100 nanosecond accuracy in practice.
millisToAdd - milliseconds to add to this time. Fractions are used to provide 100 ns
accuracy that is native to DateTime.DateTime instance.public DateTime plusMillis(long millisToAdd)
millisToAdd - milliseconds to add to this time.DateTime instance.public DateTime plusNanos(long nanosToAdd)
Note that the actual resolution of DateTime is 100 nanoseconds, so the given value is truncated to 100 nanosecond accuracy in practice.
nanosToAdd - nanoseconds to add to this time.DateTime instance.Copyright © 2026. All rights reserved.