This time corresponds to Unix time, except for the "epoch", 2000-01-01 instead of 1970-01-01. @author Jean-Paul Vetterli
DateTime
is the most widely used implementation of {@link ReadableInstant}. As with all instants, it represents an exact point on the time-line, but limited to the precision of milliseconds. A DateTime
calculates its fields with respect to a {@link DateTimeZone time zone}.
Internally, the class holds two pieces of data. Firstly, it holds the datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z. Secondly, it holds a {@link Chronology} which determines how themillisecond instant value is converted into the date time fields. The default Chronology is {@link ISOChronology} which is the agreedinternational standard and compatible with the modern Gregorian calendar.
Each individual field can be queried in two ways:
getHourOfDay()
hourOfDay().get()
DateTime is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable. @author Stephen Colebourne @author Kandarp Shah @author Brian S O'Neill @since 1.0 @see MutableDateTime
Implementation is immutable and therefore thread-safe.
@since 1.0 @author Yaniv InbarThere are 2 distinct mental models for date-times, and they don't play well together :
The problem is that java.util. {@link java.util.Date} uses only the timeline style, while most users, most of the time, think in terms of the other mental model - the 'everday' style. In particular, there are a large number of applications which experience problems with time zones, because the timeline model is used instead of the everday model. Such problems are often seen by end users as serious bugs, because telling people the wrong date or time is often a serious issue. These problems make you look stupid.
The final point requires elaboration. Some may doubt its veracity, since they have seen date-time information "change time zone" when retrieved from a database. But this sort of change is usually applied using logic which is external to the data stored in the particular column.
For example, the following items might be used in the calculation of a time zone difference :
(Note as well what's missing from the above list: your own application's logic, and the user's time zone preference.)
When an end user sees such changes to a date-time, all they will say to you is "Why did you change it? That's not what I entered" - and this is a completely valid question. Why did you change it? Because you're using the timeline model instead of the everyday model. Perhaps you're using a inappropriate abstraction for what the user really wants.
Even though the above list may appear restrictive, it's very likely true that DateTime can handle the dates and times you're currently storing in your database.
Basic operations model the date-time as a simple, dumb String, with absolutely no parsing or substructure. This will always allow your application to reflect exactly what is in a ResultSet, with absolutely no modification for time zone, locale, or for anything else.
This is meant as a back-up, to ensure that your application will always be able to, at the very least, display a date-time exactly as it appears in your ResultSet from the database. This style is particularly useful for handling invalid dates such as 2009-00-00, which can in fact be stored by some databases (MySQL, for example). It can also be used to handle unusual items, such as MySQL's TIME datatype.
The basic operations are represented by {@link #DateTime(String)}, {@link #toString()}, and {@link #getRawDateString()}.
Computational operations allow for calculations and formatting. If a computational operation is performed by this class (for example, if the caller asks for the month), then any underlying date-time String must be parseable by this class into its components - year, month, day, and so on. Computational operations require such parsing, while the basic operations do not. Almost all methods in this class are categorized as computational operations.
The {@link #isParseable(String)} method lets you explicitly test if a given String is in a form that can be parsed by this class.
The following table defines the symbols used by this mini-language, and the corresponding text they would generate given the date:
1958-04-09 Wednesday, 03:05:06.123456789 AMin an English Locale. (Items related to date are in upper case, and items related to time are in lower case.)
Format | Output | Description | Needs Locale? |
---|---|---|---|
YYYY | 1958 | Year | ... |
YY | 58 | Year without century | ... |
M | 4 | Month 1..12 | ... |
MM | 04 | Month 01..12 | ... |
MMM | Apr | Month Jan..Dec | Yes |
MMMM | April | Month January..December | Yes |
DD | 09 | Day 01..31 | ... |
D | 9 | Day 1..31 | ... |
WWWW | Wednesday | Weekday Sunday..Saturday | Yes |
WWW | Wed | Weekday Sun..Sat | Yes |
hh | 03 | Hour 01..23 | ... |
h | 3 | Hour 1..23 | ... |
hh12 | 03 | Hour 01..12 | ... |
h12 | 3 | Hour 1..12 | ... |
a | AM | AM/PM Indicator | Yes |
mm | 05 | Minutes 01..59 | ... |
m | 5 | Minutes 1..59 | ... |
ss | 06 | Seconds 01..59 | ... |
s | 6 | Seconds 1..59 | ... |
f | 1 | Fractional Seconds, 1 decimal | ... |
ff | 12 | Fractional Seconds, 2 decimals | ... |
fff | 123 | Fractional Seconds, 3 decimals | ... |
ffff | 1234 | Fractional Seconds, 4 decimals | ... |
fffff | 12345 | Fractional Seconds, 5 decimals | ... |
ffffff | 123456 | Fractional Seconds, 6 decimals | ... |
fffffff | 1234567 | Fractional Seconds, 7 decimals | ... |
ffffffff | 12345678 | Fractional Seconds, 8 decimals | ... |
fffffffff | 123456789 | Fractional Seconds, 9 decimals | ... |
| | (no example) | Escape characters | ... |
As indicated above, some of these symbols can only be used with an accompanying Locale. In general, if the output is text, not a number, then a Locale will be needed. For example, 'September' is localizable text, while '09' is a numeric representation, which doesn't require a Locale. Thus, the symbol 'MM' can be used without a Locale, while 'MMMM' and 'MMM' both require a Locale, since they generate text, not a number.
The fractional seconds 'f' doesn't perform any rounding.
The escape character '|' allows you to insert arbitrary text. The escape character always appears in pairs; these pairs define a range of characters over which the text will not be interpreted using the special format symbols defined above.
Here are some practical examples of using the above formatting symbols:
Format | Output |
---|---|
YYYY-MM-DD hh:mm:ss.fffffffff a | 1958-04-09 03:05:06.123456789 AM |
YYYY-MM-DD hh:mm:ss.fff a | 1958-04-09 03:05:06.123 AM |
YYYY-MM-DD | 1958-04-09 |
hh:mm:ss.fffffffff | 03:05:06.123456789 |
hh:mm:ss | 03:05:06 |
YYYY-M-D h:m:s | 1958-4-9 3:5:6 |
WWWW, MMMM D, YYYY | Wednesday, April 9, 1958 |
WWWW, MMMM D, YYYY |at| D a | Wednesday, April 9, 1958 at 3 AM |
In the last example, the escape characters are needed only because 'a', the formating symbol for am/pm, appears in the text.
There are 2 distinct mental models for date-times, and they don't play well together :
The problem is that java.util. {@link java.util.Date} uses only the timeline style, while most users, most of the time, think in terms of the other mental model - the 'everday' style. In particular, there are a large number of applications which experience problems with time zones, because the timeline model is used instead of the everday model. Such problems are often seen by end users as serious bugs, because telling people the wrong date or time is often a serious issue. These problems make you look stupid.
The final point requires elaboration. Some may doubt its veracity, since they have seen date-time information "change time zone" when retrieved from a database. But this sort of change is usually applied using logic which is external to the data stored in the particular column.
For example, the following items might be used in the calculation of a time zone difference :
(Note as well what's missing from the above list: your own application's logic, and the user's time zone preference.)
When an end user sees such changes to a date-time, all they will say to you is "Why did you change it? That's not what I entered" - and this is a completely valid question. Why did you change it? Because you're using the timeline model instead of the everyday model. Perhaps you're using a inappropriate abstraction for what the user really wants.
Even though the above list may appear restrictive, it's very likely true that DateTime can handle the dates and times you're currently storing in your database.
Basic operations model the date-time as a simple, dumb String, with absolutely no parsing or substructure. This will always allow your application to reflect exactly what is in a ResultSet, with absolutely no modification for time zone, locale, or for anything else.
This is meant as a back-up, to ensure that your application will always be able to, at the very least, display a date-time exactly as it appears in your ResultSet from the database. This style is particularly useful for handling invalid dates such as 2009-00-00, which can in fact be stored by some databases (MySQL, for example). It can also be used to handle unusual items, such as MySQL's TIME datatype.
The basic operations are represented by {@link #DateTime(String)}, {@link #toString()}, and {@link #getRawDateString()}.
Computational operations allow for calculations and formatting. If a computational operation is performed by this class (for example, if the caller asks for the month), then any underlying date-time String must be parseable by this class into its components - year, month, day, and so on. Computational operations require such parsing, while the basic operations do not. Almost all methods in this class are categorized as computational operations.
The following table defines the symbols used by this mini-language, and the corresponding text they would generate given the date:
1958-04-09 Wednesday, 03:05:06.123456789 AMin an English Locale. (Items related to date are in upper case, and items related to time are in lower case.)
Format | Output | Description | Needs Locale? |
---|---|---|---|
YYYY | 1958 | Year | ... |
YY | 58 | Year without century | ... |
M | 4 | Month 1..12 | ... |
MM | 04 | Month 01..12 | ... |
MMM | Apr | Month Jan..Dec | Yes |
MMMM | April | Month January..December | Yes |
DD | 09 | Day 01..31 | ... |
D | 9 | Day 1..31 | ... |
WWWW | Wednesday | Weekday Sunday..Saturday | Yes |
WWW | Wed | Weekday Sun..Sat | Yes |
hh | 03 | Hour 01..23 | ... |
h | 3 | Hour 1..23 | ... |
hh12 | 03 | Hour 01..12 | ... |
h12 | 3 | Hour 1..12 | ... |
a | AM | AM/PM Indicator | Yes |
mm | 05 | Minutes 01..59 | ... |
m | 5 | Minutes 1..59 | ... |
ss | 06 | Seconds 01..59 | ... |
s | 6 | Seconds 1..59 | ... |
f | 1 | Fractional Seconds, 1 decimal | ... |
ff | 12 | Fractional Seconds, 2 decimals | ... |
fff | 123 | Fractional Seconds, 3 decimals | ... |
ffff | 1234 | Fractional Seconds, 4 decimals | ... |
fffff | 12345 | Fractional Seconds, 5 decimals | ... |
ffffff | 123456 | Fractional Seconds, 6 decimals | ... |
fffffff | 1234567 | Fractional Seconds, 7 decimals | ... |
ffffffff | 12345678 | Fractional Seconds, 8 decimals | ... |
fffffffff | 123456789 | Fractional Seconds, 9 decimals | ... |
| | (no example) | Escape character | ... |
As indicated above, some of these symbols can only be used with an accompanying Locale. In general, if the output is text, not a number, then a Locale will be needed. For example, 'September' is localizable text, while '09' is a numeric representation, which doesn't require a Locale. Thus, the symbol 'MM' can be used without a Locale, while 'MMMM' and 'MMM' both require a Locale, since they generate text, not a number.
The fractional seconds 'f' does not perform any rounding. The escape character '|' allows you to insert arbitrary text.
Examples :
Format | Output |
---|---|
YYYY-MM-DD hh:mm:ss.fffffffff a | 1958-04-09 03:05:06.123456789 AM |
YYYY-MM-DD hh:mm:ss.fff a | 1958-04-09 03:05:06.123 AM |
YYYY-MM-DD | 1958-04-09 |
hh:mm:ss.fffffffff | 03:05:06.123456789 |
hh:mm:ss | 03:05:06 |
YYYY-M-D h:m:s | 1958-4-9 3:5:6 |
WWWW, MMMM D, YYYY | Wednesday, April 9, 1958 |
WWWW, MMMM D, YYYY |at| D a | Wednesday, April 9, 1958 at 3 AM |
An element conforming to the Atom Date Construct. The data type implementation for this element is provided by the AtomDate class.
CURRENT_DATE: This function returns the value of current date on the database server.
CURRENT_TIME: This function returns the value of current time on the database server.
CURRENT_TIMESTAMP: This function returns the value of current timestamp on the database server.
functions_returning_datetime ::= CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP
The JDBC escape syntax may be used for the specification of date, time, and timestamp literals.
expression ::= {d 'yyyy-mm-dd'} | {t 'hh:mm:ss'} | {ts 'yyyy-mm-dd hh:mm:ss.f...'}
@version 2.4 @since 2.3 @author Pascal Filion
The format is defined by W3C XML Schema Recommendation and ISO8601 i.e (-)CCYY-MM-DD'T'HH:MM:SS(.SSSSS)(Z|(+|-)hh:mm) @author Edward Kuns @version $Revision: 0000 $
DateTime
is the most widely used implementation of {@link ReadableInstant}. As with all instants, it represents an exact point on the time-line, but limited to the precision of milliseconds. A DateTime
calculates its fields with respect to a {@link DateTimeZone time zone}.
Internally, the class holds two pieces of data. Firstly, it holds the datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z. Secondly, it holds a {@link Chronology} which determines how themillisecond instant value is converted into the date time fields. The default Chronology is {@link ISOChronology} which is the agreedinternational standard and compatible with the modern Gregorian calendar.
Each individual field can be queried in two ways:
getHourOfDay()
hourOfDay().get()
DateTime is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable. @author Stephen Colebourne @author Kandarp Shah @author Brian S O'Neill @since 1.0 @see MutableDateTime
DateTime
is the most widely used implementation of {@link ReadableInstant}. As with all instants, it represents an exact point on the time-line, but limited to the precision of milliseconds. A DateTime
calculates its fields with respect to a {@link DateTimeZone time zone}.
Internally, the class holds two pieces of data. Firstly, it holds the datetime as milliseconds from the Java epoch of 1970-01-01T00:00:00Z. Secondly, it holds a {@link Chronology} which determines how themillisecond instant value is converted into the date time fields. The default Chronology is {@link ISOChronology} which is the agreedinternational standard and compatible with the modern Gregorian calendar.
Each individual field can be queried in two ways:
getHourOfDay()
hourOfDay().get()
DateTime is thread-safe and immutable, provided that the Chronology is as well. All standard Chronology classes supplied are thread-safe and immutable. @author Stephen Colebourne @author Kandarp Shah @author Brian S O'Neill @since 1.0 @see MutableDateTime
Se quisermos mostrar a hora em Lisboa com esta mesma inst�ncia do DateTime temos que usar um formatter com o default time zone:
// formatter para Lisboa (default) DateFormat fmtLx = new SimpleDateFormat(DateTime.DATE_FORMAT); // usa um formatter com timezone de Lisboa para apresentar a hora em Lisboa System.out.println(fmtLx.format(date));obt�m-se 2007-01-23 11:00:00
O comportamento do construtor � equivalente a ter um DateTime e fazer posteriormente o {@link #setTimeZone(TimeZone)}.
@see AtlanticAzoresTimeZone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|