|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QDateTime
public class QDateTime
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).
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, 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 |
---|
public QDateTime(QDateTime other)
Constructs a copy of the other datetime.
public QDateTime(QDate arg__1)
Constructs a datetime with the given arg__1, and a valid time (00.00:00.000).
public QDateTime()
Constructs a null datetime (i.e. null date and null time). A null datetime is invalid, since the date is invalid.
public QDateTime(QDate arg__1, QTime arg__2)
Equivalent to QDateTime(arg__1, arg__2, Qt::LocalTime).
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 |
---|
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).
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).
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).
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).
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).
public final QDate date()
Returns the date part of the datetime.
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.
public final boolean isNull()
Returns true if both the date and the time are null; otherwise returns false. A null datetime is invalid.
public final boolean isValid()
Returns true if both the date and the time are valid; otherwise returns false.
public final void writeTo(QDataStream arg__1)
public final void readFrom(QDataStream arg__1)
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));
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.
public final void setTime(QTime time)
Sets the time part of this datetime to time.
public final void setTimeSpec(Qt.TimeSpec spec)
Sets the time specification used in this datetime to spec.
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.
public final QTime time()
Returns the time part of the datetime.
public final Qt.TimeSpec timeSpec()
Returns the time specification of the datetime.
public final QDateTime toLocalTime()
Returns a datetime containing the date and time information in this datetime, but specified using the Qt::LocalTime definition.
public final java.lang.String toString()
Equivalent to toString(Qt::TextDate).
toString
in class java.lang.Object
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.
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:
Expression | Output |
---|---|
d | the day as number without a leading zero (1 to 31) |
dd | the day as number with a leading zero (01 to 31) |
ddd | the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). |
dddd | the long localized day name (e.g. 'Qt::Monday' to 'Qt::Sunday'). Uses QDate::longDayName(). |
M | the month as number without a leading zero (1-12) |
MM | the month as number with a leading zero (01-12) |
MMM | the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). |
MMMM | the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). |
yy | the year as two digit number (00-99) |
yyyy | the year as four digit number |
These expressions may be used for the time:
Expression | Output |
---|---|
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
m | the minute without a leading zero (0 to 59) |
mm | the minute with a leading zero (00 to 59) |
s | the second without a leading zero (0 to 59) |
ss | the second with a leading zero (00 to 59) |
z | the milliseconds without leading zeroes (0 to 999) |
zzz | the milliseconds with leading zeroes (000 to 999) |
AP | use AM/PM display. AP will be replaced by either "AM" or "PM". |
ap | use 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):
Format | Result |
---|---|
dd.MM.yyyy | 21.05.2001 |
ddd MMMM d yy | Tue May 21 01 |
hh:mm:ss.zzz | 14:13:09.042 |
h:m:s ap | 2:13:9 pm |
If the datetime is invalid, an empty string will be returned.
public final QDateTime toTimeSpec(Qt.TimeSpec spec)
Returns a copy of this datetime configured to use the given time spec.
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.
public final QDateTime toUTC()
Returns a datetime containing the date and time information in this datetime, but specified using the Qt::UTC definition.
public static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
public static QDateTime fromString(java.lang.String s)
Equivalent to fromString(s, Qt::TextDate).
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.
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:
Expression | Output |
---|---|
d | the day as number without a leading zero (1 to 31) |
dd | the day as number with a leading zero (01 to 31) |
ddd | the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). |
dddd | the long localized day name (e.g. 'Qt::Monday' to 'Qt::Sunday'). Uses QDate::longDayName(). |
M | the month as number without a leading zero (1-12) |
MM | the month as number with a leading zero (01-12) |
MMM | the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). |
MMMM | the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). |
yy | the year as two digit number (00-99) |
yyyy | the year as four digit number |
These expressions may be used for the time part of the format string:
Expression | Output |
---|---|
h | the hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display) |
hh | the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display) |
H | the hour without a leading zero (0 to 23, even with AM/PM display) |
HH | the hour with a leading zero (00 to 23, even with AM/PM display) |
m | the minute without a leading zero (0 to 59) |
mm | the minute with a leading zero (00 to 59) |
s | the second without a leading zero (0 to 59) |
ss | the second with a leading zero (00 to 59) |
z | the milliseconds without leading zeroes (0 to 999) |
zzz | the milliseconds with leading zeroes (000 to 999) |
AP or A | interpret as an AM/PM time. AP must be either "AM" or "PM". |
ap or a | Interpret 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:
Field | Default value |
---|---|
Year | 1900 |
Month | 1 (January) |
Day | 1 |
Hour | 0 |
Minute | 0 |
Second | 0 |
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()
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.
public static QDateTime fromNativePointer(QNativePointer nativePointer)
nativePointer
- the QNativePointer of which object should be returned.public static QNativePointer nativePointerArray(QDateTime[] array)
array
- the array that the returned pointer will point to.
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int compareTo(java.lang.Object other)
compareTo
in interface java.lang.Comparable
|
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |