Java Date.toString在Oracle的TO_DATE [英] Java Date.toString in Oracle's TO_DATE
问题描述
Date.toString()的默认格式似乎是在美国地区,如下所示:
Thu Nov 24 15:20:52 CET 2016
Oracle数据库具有允许从字符串转换的TO_DATE函数至今。第一个参数是日期格式。
哪个是映射Java Date.toString()字符串的正确日期格式?是:
DAY MONTH DD HH24:MI:SS TZD YYYY
pre>
?
谢谢。
解决方案Oracle
DATE
数据类型没有时区 - 您需要一个TIMESTAMP WITH TIMEZONE
数据类型:SELECT TO_TIMESTAMP_TZ(
'Thu Nov 24 15:20:52 CET 2016'
'DY MON DD HH24:MI:SS TZR YYYY'
)
从DUAL
如果要转换为
DATE
(而时区区域始终为CET
)那么你可以使用:SELECT TO_DATE(
'Thu Nov 24 15:20:52 CET 2016',
'DY MON DD HH24:MI:SSCETYYYY'
)
从DUAL
如果您希望将其作为
DATE
数据类型,并遵守原始字符串中的时区,则需要(1)将其转换为一个TIMEST AMP WITH TIMEZONE
数据类型; (2)将该值转换为标准化时区(UTC常用于此);然后(3)将其转换为日期:SELECT CAST(
TO_TIMESTAMP_TZ(
'Thu Nov 24 15:20:52 CET 2016',
'DY MON DD HH24:MI:SS TZR YYYY'
)
AT TIME ZONE'UTC'
AS DATE
)
FROM DUAL;
哪个将输出日期
2016-11-24 14:20:52
(输入日期的UTC表示)。The default format for Date.toString() seems to be, in an US locale, something like:
Thu Nov 24 15:20:52 CET 2016
Oracle database has a function "TO_DATE" that allows convert from string to date. First argument is the date format.
Which is the correct date format to map a Java Date.toString() string? Is it:
DAY MONTH DD HH24:MI:SS TZD YYYY
?
Thanks.
解决方案An Oracle
DATE
data type does not have a time zone - you need aTIMESTAMP WITH TIMEZONE
data type:SELECT TO_TIMESTAMP_TZ( 'Thu Nov 24 15:20:52 CET 2016', 'DY MON DD HH24:MI:SS TZR YYYY' ) FROM DUAL
If you want to convert to a
DATE
(and the time zone region is alwaysCET
) then you can use:SELECT TO_DATE( 'Thu Nov 24 15:20:52 CET 2016', 'DY MON DD HH24:MI:SS "CET" YYYY' ) FROM DUAL
If you want it as a
DATE
data type and to respect the time zone in the original string then you will need to (1) convert it to aTIMESTAMP WITH TIMEZONE
data type; (2) convert that value to a standardized time zone (UTC is often used for this); then (3) convert that to a date:SELECT CAST( TO_TIMESTAMP_TZ( 'Thu Nov 24 15:20:52 CET 2016', 'DY MON DD HH24:MI:SS TZR YYYY' ) AT TIME ZONE 'UTC' AS DATE ) FROM DUAL;
Which will output the date
2016-11-24 14:20:52
(the UTC representation of the input date).这篇关于Java Date.toString在Oracle的TO_DATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!