com.trolltech.qt.core
Class QDateTime

java.lang.Object
  extended by com.trolltech.qt.QSignalEmitter
      extended by com.trolltech.qt.QtJambiObject
          extended by com.trolltech.qt.core.QDateTime
All Implemented Interfaces:
QtJambiInterface, java.lang.Comparable

public class QDateTime
extends QtJambiObject
implements java.lang.Comparable

The QDateTime class provides date and time functions.

A QDateTime object contains a calendar date and a clock time (a "datetime"). It is a combination of the QDate and QTime classes. It can read the current datetime from the system clock. It provides functions for comparing datetimes and for manipulating a datetime by adding a number of seconds, days, months, or years.

A QDateTime object is typically created either by giving a date and time explicitly in the constructor, or by using the static function currentDateTime that returns a QDateTime object set to the system clock's time. The date and time can be changed with setDate and setTime. A datetime can also be set using the setTime_t function that takes a POSIX-standard "number of seconds since 00:00:00 on January 1, 1970" value. The fromString function returns a QDateTime, given a string and a date format used to interpret the date within the string.

The date and time functions provide access to the date and time parts of the datetime. The same information is provided in textual format by the toString function.

QDateTime provides a full set of operators to compare two QDateTime objects where smaller means earlier and larger means later.

You can increment (or decrement) a datetime by a given number of seconds using addSecs, or days using addDays. Similarly you can use addMonths and addYears. The daysTo function returns the number of days between two datetimes, and secsTo returns the number of seconds between two datetimes.

QDateTime can store datetimes as local time or as UTC. QDateTime::currentDateTime() returns a QDateTime expressed as local time; use toUTC to convert it to UTC. You can also use timeSpec to find out if a QDateTime object stores a UTC time or a local time. Operations such as addSecs and secsTo are aware of daylight saving time (DST).

See Also:
QDate, QTime, QDateTimeEdit

Nested Class Summary
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.Signal0, QSignalEmitter.Signal1<A>, QSignalEmitter.Signal2<A,B>, QSignalEmitter.Signal3<A,B,C>, QSignalEmitter.Signal4<A,B,C,D>, QSignalEmitter.Signal5<A,B,C,D,E>, QSignalEmitter.Signal6<A,B,C,D,E,F>, QSignalEmitter.Signal7<A,B,C,D,E,F,G>, QSignalEmitter.Signal8<A,B,C,D,E,F,G,H>, QSignalEmitter.Signal9<A,B,C,D,E,F,G,H,I>
 
Constructor Summary
QDateTime()
          Constructs a null datetime (i.e. null date and null time).
QDateTime(QDate arg__1)
          Constructs a datetime with the given arg__1, and a valid time (00.00:00.000).
QDateTime(QDate arg__1, QTime arg__2)
          Equivalent to QDateTime(arg__1, arg__2, Qt::LocalTime).
QDateTime(QDate arg__1, QTime arg__2, Qt.TimeSpec spec)
          Constructs a datetime with the given arg__1 and arg__2, using the time specification defined by spec.
QDateTime(QDateTime other)
          Constructs a copy of the other datetime.
 
Method Summary
 QDateTime addDays(int days)
          Returns a QDateTime object containing a datetime days days later than the datetime of this object (or earlier if days is negative).
 QDateTime addMonths(int months)
          Returns a QDateTime object containing a datetime months months later than the datetime of this object (or earlier if months is negative).
 QDateTime addMSecs(long msecs)
          Returns a QDateTime object containing a datetime msecs miliseconds later than the datetime of this object (or earlier if msecs is negative).
 QDateTime addSecs(int secs)
          Returns a QDateTime object containing a datetime secs seconds later than the datetime of this object (or earlier if secs is negative).
 QDateTime addYears(int years)
          Returns a QDateTime object containing a datetime years years later than the datetime of this object (or earlier if years is negative).
 int compareTo(java.lang.Object other)
           
static QDateTime currentDateTime()
          Returns the current datetime, as reported by the system clock, in the local time zone.
 QDate date()
          Returns the date part of the datetime.
 int daysTo(QDateTime arg__1)
          Returns the number of days from this datetime to the arg__1 datetime.
 boolean equals(java.lang.Object other)
          
static QDateTime fromNativePointer(QNativePointer nativePointer)
          This function returns the QDateTime instance pointed to by nativePointer
static QDateTime fromString(java.lang.String s)
          Equivalent to fromString(s, Qt::TextDate).
static QDateTime fromString(java.lang.String s, Qt.DateFormat f)
          Returns the QDateTime represented by the s, using the f given, or an invalid datetime if this is not possible.
static QDateTime fromString(java.lang.String s, java.lang.String format)
          Returns the QDateTime represented by the s, using the format given, or an invalid datetime if the string cannot be parsed.
static QDateTime fromTime_t(int secsSince1Jan1970UTC)
          Returns a datetime whose date and time are the number of secsSince1Jan1970UTC that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
 boolean isNull()
          Returns true if both the date and the time are null; otherwise returns false.
 boolean isValid()
          Returns true if both the date and the time are valid; otherwise returns false.
static QNativePointer nativePointerArray(QDateTime[] array)
          This function returns a QNativePointer that is pointing to the specified QDateTime array.
 void readFrom(QDataStream arg__1)
          Reads a QDateTime from arg__1.
 int secsTo(QDateTime arg__1)
          Returns the number of seconds from this datetime to the arg__1 datetime.
 void setDate(QDate date)
          Sets the date part of this datetime to date.
 void setTime_t(int secsSince1Jan1970UTC)
          Sets the date and time given the number of secsSince1Jan1970UTC that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
 void setTime(QTime time)
          Sets the time part of this datetime to time.
 void setTimeSpec(Qt.TimeSpec spec)
          Sets the time specification used in this datetime to spec.
 QTime time()
          Returns the time part of the datetime.
 Qt.TimeSpec timeSpec()
          Returns the time specification of the datetime.
 QDateTime toLocalTime()
          Returns a datetime containing the date and time information in this datetime, but specified using the Qt::LocalTime definition.
 java.lang.String toString()
          Equivalent to toString(Qt::TextDate).
 java.lang.String toString(Qt.DateFormat f)
          Returns the datetime as a string in the f given.
 java.lang.String toString(java.lang.String format)
          Returns the datetime as a string.
 int toTime_t()
          Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
 QDateTime toTimeSpec(Qt.TimeSpec spec)
          Returns a copy of this datetime configured to use the given time spec.
 QDateTime toUTC()
          Returns a datetime containing the date and time information in this datetime, but specified using the Qt::UTC definition.
 void writeTo(QDataStream arg__1)
          Writes thisQDateTime to arg__1.
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Constructor Detail

QDateTime

public QDateTime(QDateTime other)

Constructs a copy of the other datetime.


QDateTime

public QDateTime(QDate arg__1)

Constructs a datetime with the given arg__1, and a valid time (00.00:00.000).


QDateTime

public QDateTime()

Constructs a null datetime (i.e. null date and null time). A null datetime is invalid, since the date is invalid.

See Also:
isValid

QDateTime

public QDateTime(QDate arg__1,
                 QTime arg__2)

Equivalent to QDateTime(arg__1, arg__2, Qt::LocalTime).


QDateTime

public QDateTime(QDate arg__1,
                 QTime arg__2,
                 Qt.TimeSpec spec)

Constructs a datetime with the given arg__1 and arg__2, using the time specification defined by spec.

If arg__1 is valid and arg__2 is not, the time will be set to midnight.

Method Detail

addDays

public final QDateTime addDays(int days)

Returns a QDateTime object containing a datetime days days later than the datetime of this object (or earlier if days is negative).

See Also:
daysTo, addMonths, addYears, addSecs

addMSecs

public final QDateTime addMSecs(long msecs)

Returns a QDateTime object containing a datetime msecs miliseconds later than the datetime of this object (or earlier if msecs is negative).

See Also:
addSecs, secsTo, addDays, addMonths, addYears

addMonths

public final QDateTime addMonths(int months)

Returns a QDateTime object containing a datetime months months later than the datetime of this object (or earlier if months is negative).

See Also:
daysTo, addDays, addYears, addSecs

addSecs

public final QDateTime addSecs(int secs)

Returns a QDateTime object containing a datetime secs seconds later than the datetime of this object (or earlier if secs is negative).

See Also:
addMSecs, secsTo, addDays, addMonths, addYears

addYears

public final QDateTime addYears(int years)

Returns a QDateTime object containing a datetime years years later than the datetime of this object (or earlier if years is negative).

See Also:
daysTo, addDays, addMonths, addSecs

date

public final QDate date()

Returns the date part of the datetime.

See Also:
setDate, time, timeSpec

daysTo

public final int daysTo(QDateTime arg__1)

Returns the number of days from this datetime to the arg__1 datetime. If the arg__1 datetime is earlier than this datetime, the value returned is negative.

See Also:
addDays, secsTo

isNull

public final boolean isNull()

Returns true if both the date and the time are null; otherwise returns false. A null datetime is invalid.

See Also:
QDate::isNull, QTime::isNull, isValid

isValid

public final boolean isValid()

Returns true if both the date and the time are valid; otherwise returns false.

See Also:
QDate::isValid, QTime::isValid

writeTo

public final void writeTo(QDataStream arg__1)
Writes thisQDateTime to arg__1.


readFrom

public final void readFrom(QDataStream arg__1)
Reads a QDateTime from arg__1.


secsTo

public final int secsTo(QDateTime arg__1)

Returns the number of seconds from this datetime to the arg__1 datetime. If the arg__1 datetime is earlier than this datetime, the value returned is negative.

Before performing the comparison, the two datetimes are converted to Qt::UTC to ensure that the result is correct if one of the two datetimes has daylight saving time (DST) and the other doesn't.

Example:

    QDateTime now = QDateTime::currentDateTime();
    QDateTime xmas(QDate(now.date().year(), 12, 25), QTime(0, 0));
    qDebug("There are %d seconds to Christmas", now.secsTo(xmas));

See Also:
addSecs, daysTo, QTime::secsTo

setDate

public final void setDate(QDate date)

Sets the date part of this datetime to date. If no time is set, it is set to midnight.

See Also:
date, setTime, setTimeSpec

setTime

public final void setTime(QTime time)

Sets the time part of this datetime to time.

See Also:
time, setDate, setTimeSpec

setTimeSpec

public final void setTimeSpec(Qt.TimeSpec spec)

Sets the time specification used in this datetime to spec.

See Also:
timeSpec, setDate, setTime, Qt::TimeSpec

setTime_t

public final void setTime_t(int secsSince1Jan1970UTC)

Sets the date and time given the number of secsSince1Jan1970UTC that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). On systems that do not support timezones this function will behave as if local time were Qt::UTC.

See Also:
toTime_t

time

public final QTime time()

Returns the time part of the datetime.

See Also:
setTime, date, timeSpec

timeSpec

public final Qt.TimeSpec timeSpec()

Returns the time specification of the datetime.

See Also:
setTimeSpec, date, time, Qt::TimeSpec

toLocalTime

public final QDateTime toLocalTime()

Returns a datetime containing the date and time information in this datetime, but specified using the Qt::LocalTime definition.

See Also:
toTimeSpec

toString

public final java.lang.String toString()

Equivalent to toString(Qt::TextDate).

Overrides:
toString in class java.lang.Object

toString

public final java.lang.String toString(Qt.DateFormat f)

Returns the datetime as a string in the f given.

If the f is Qt::TextDate, the string is formatted in the default way. QDate::shortDayName(), QDate::shortMonthName(), and QTime::toString() are used to generate the string, so the day and month names will be localized names. An example of this formatting is "Wed May 20 03:40:13 1998".

If the f is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form YYYY-MM-DDTHH:MM:SS.

If the f is Qt::LocalDate or Qt::SystemLocaleDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(dateTime, QLocale::ShortFormat). Qt::LocalDate has been deprecated, please use Qt::SystemLocaleDate instead.

If the f is Qt::LocaleDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(dateTime, QLocale::ShortFormat);

If the datetime is invalid, an empty string will be returned.

Warning: The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to Qt::LocalDate as well, depending on the locale settings.

See Also:
QDate::toString, QTime::toString, Qt::DateFormat

toString

public final java.lang.String toString(java.lang.String format)

Returns the datetime as a string. The format parameter determines the format of the result string.

These expressions may be used for the date:

ExpressionOutput
dthe day as number without a leading zero (1 to 31)
ddthe day as number with a leading zero (01 to 31)
dddthe abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
ddddthe long localized day name (e.g. 'Qt::Monday' to 'Qt::Sunday'). Uses QDate::longDayName().
Mthe month as number without a leading zero (1-12)
MMthe month as number with a leading zero (01-12)
MMMthe abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().
MMMMthe long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().
yythe year as two digit number (00-99)
yyyythe year as four digit number

These expressions may be used for the time:

ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
APuse AM/PM display. AP will be replaced by either "AM" or "PM".
apuse am/pm display. ap will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.

Example format strings (assumed that the QDateTime is 21 May 2001 14:13:09):

FormatResult
dd.MM.yyyy21.05.2001
ddd MMMM d yyTue May 21 01
hh:mm:ss.zzz14:13:09.042
h:m:s ap2:13:9 pm

If the datetime is invalid, an empty string will be returned.

See Also:
QDate::toString, QTime::toString

toTimeSpec

public final QDateTime toTimeSpec(Qt.TimeSpec spec)

Returns a copy of this datetime configured to use the given time spec.

See Also:
timeSpec, toUTC, toLocalTime

toTime_t

public final int toTime_t()

Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).

On systems that do not support timezones, this function will behave as if local time were Qt::UTC.

See Also:
setTime_t

toUTC

public final QDateTime toUTC()

Returns a datetime containing the date and time information in this datetime, but specified using the Qt::UTC definition.

See Also:
toTimeSpec

currentDateTime

public static QDateTime currentDateTime()

Returns the current datetime, as reported by the system clock, in the local time zone.

See Also:
QDate::currentDate, QTime::currentTime, toTimeSpec

fromString

public static QDateTime fromString(java.lang.String s)

Equivalent to fromString(s, Qt::TextDate).


fromString

public static QDateTime fromString(java.lang.String s,
                                   Qt.DateFormat f)

Returns the QDateTime represented by the s, using the f given, or an invalid datetime if this is not possible.

Note for Qt::TextDate: It is recommended that you use the English short month names (e.g. "Jan"). Although localized month names can also be used, they depend on the user's locale settings.

Warning: Note that Qt::LocalDate cannot be used here.


fromString

public static QDateTime fromString(java.lang.String s,
                                   java.lang.String format)

Returns the QDateTime represented by the s, using the format given, or an invalid datetime if the string cannot be parsed.

These expressions may be used for the date part of the format string:

ExpressionOutput
dthe day as number without a leading zero (1 to 31)
ddthe day as number with a leading zero (01 to 31)
dddthe abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
ddddthe long localized day name (e.g. 'Qt::Monday' to 'Qt::Sunday'). Uses QDate::longDayName().
Mthe month as number without a leading zero (1-12)
MMthe month as number with a leading zero (01-12)
MMMthe abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().
MMMMthe long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().
yythe year as two digit number (00-99)
yyyythe year as four digit number

These expressions may be used for the time part of the format string:

ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
Hthe hour without a leading zero (0 to 23, even with AM/PM display)
HHthe hour with a leading zero (00 to 23, even with AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
AP or Ainterpret as an AM/PM time. AP must be either "AM" or "PM".
ap or aInterpret as an AM/PM time. ap must be either "am" or "pm".

All other input characters will be treated as text. Any sequence of characters that are enclosed in singlequotes will also be treated as text and not be used as an expression.

    QTime time1 = QTime::fromString("131", "HHh");
    // time1 is 13:00:00
    QTime time1 = QTime::fromString("1apA", "1amAM");
    // time1 is 01:00:00

    QDateTime dateTime2 = QDateTime::fromString("M1d1y9800:01:02",
                                                "'M'M'd'd'y'yyhh:mm:ss");
    // dateTime is 1 January 1998 00:01:02

If the format is not satisfied an invalid QDateTime is returned. The expressions that don't have leading zeroes (d, M, h, m, s, z) will be greedy. This means that they will use two digits even if this will put them outside the range and/or leave too few digits for other sections.

    QDateTime dateTime = QDateTime::fromString("130", "Mm"); // invalid

This could have meant 1 January 00:30.00 but the M will grab two digits.

For any field that is not represented in the format the following defaults are used:

FieldDefault value
Year1900
Month1 (January)
Day1
Hour0
Minute0
Second0

For example:

    QDateTime dateTime = QDateTime::fromString("1.30.1", "M.d.s");
    // dateTime is January 30 in the current year 00:00:01

QDateTime::toString() QTime::toString()

See Also:
QDate::fromString, QTime::fromString, QDate::toString

fromTime_t

public static QDateTime fromTime_t(int secsSince1Jan1970UTC)

Returns a datetime whose date and time are the number of secsSince1Jan1970UTC that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC). On systems that do not support timezones, the time will be set as if local time were Qt::UTC.

See Also:
toTime_t, setTime_t

fromNativePointer

public static QDateTime fromNativePointer(QNativePointer nativePointer)
This function returns the QDateTime instance pointed to by nativePointer

Parameters:
nativePointer - the QNativePointer of which object should be returned.

nativePointerArray

public static QNativePointer nativePointerArray(QDateTime[] array)
This function returns a QNativePointer that is pointing to the specified QDateTime array.

Parameters:
array - the array that the returned pointer will point to.
Returns:
a QNativePointer that is pointing to the specified array.

equals

public boolean equals(java.lang.Object other)

Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.Object other)
Specified by:
compareTo in interface java.lang.Comparable