oracle 在 SQL 中将日期转换为简单的 DD-MM-YYYY HH24:MI:SS 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34763077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Date into simple DD-MM-YYYY HH24:MI:SS format in SQL
提问by superigno
I have a date passed as a string with this format:
我有一个日期作为具有以下格式的字符串传递:
Fri Dec 04 01:00:00 CST 2015
Anyone know how to convert it into DD-MM-YYYY HH24:MI:SS
format in Oracle? I'm trying to look for samples but can't seem to find any.
有谁知道如何DD-MM-YYYY HH24:MI:SS
在Oracle中将其转换为格式?我正在寻找样品,但似乎找不到任何样品。
Result should be:
结果应该是:
04-12-2015 01:00:00
(not sure if the time is right since it has CST)
(不确定时间是否正确,因为它有 CST)
I can have the string passed into Java, so if there are easier ways to transform it in Java it's fine with me.
我可以将字符串传递到 Java 中,所以如果有更简单的方法在 Java 中转换它,那对我来说没问题。
回答by LordAnomander
You could parse this String from the database to Date
in Java, by using SimpleDateFormat
.
您可以Date
使用SimpleDateFormat
.
The correct format would be EEE MMM dd HH:mm:ss z yyyy
.
正确的格式是EEE MMM dd HH:mm:ss z yyyy
.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(dateFromDatabase);
Once you have it as a date you can format the date in any format you'd like by using a new SimpleDateFormat
.
将其作为日期后,您可以使用新的SimpleDateFormat
.
SimpleDateFormat desiredFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String result = desiredFormat.format(date);
回答by Arun Palanisamy
You Can use the below Query to get ur required output
您可以使用以下查询来获取您所需的输出
SELECT TO_CHAR(TO_TIMESTAMP_TZ('Fri Dec 04 01:00:00 CST 2015',
'DY Mon DD HH24:MI:SS TZR YYYY'), 'DD-MM-YYYY HH24:MI:SS')
FROM DUAL;
OUTPUT
输出
04-12-2015 01:00:00
04-12-2015 01:00:00