Oracle 中的时区日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40841489/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:20:25  来源:igfitidea点击:

Timezone date format in Oracle

sqloracledatetime

提问by milheiros

I have to convert a SYSDATEdate to a specific date format. That format must be something like this: '2016-11-23T15:12:48Z'. I think this is a weird date format but is the requirements that I have.

我必须将SYSDATE日期转换为特定的日期格式。该格式必须是这样的:'2016-11-23T15:12:48Z'. 我认为这是一种奇怪的日期格式,但这是我的要求。

This must be a date to send in a Web Service message.

这必须是在 Web 服务消息中发送的日期。

In Oracle (12c or 11g) I have some function to transform a date in this specific format? Thanks.

在 Oracle(12c 或 11g)中,我有一些函数可以以这种特定格式转换日期吗?谢谢。

回答by Kacper

That will give you ISO-8601 mentioned in comment:

这将为您提供评论中提到的 ISO-8601:

select to_char(systimestamp,'YYYY-MM-DD"T"hh24:mi:sstzh:tzm') isodt from dual;

If you really want Zinstead of timezone you can use:

如果你真的想要Z而不是时区,你可以使用:

    select to_char(cast(systimestamp as timestamp) at time zone 'UTC',
               'yyyy-mm-dd"T"hh24:mi:ss"Z"')
    from dual;

回答by Boneist

Since the "Z" in the timestamp format you're after means "This is in UTC", you should first make sure your sysdate is returned in UTC. You can do this by using systimestamp and sys_extract_utc()like so:

由于您使用的时间戳格式中"Z" 表示 "This is in UTC",因此您应该首先确保您的 sysdate 以 UTC格式返回。您可以像这样使用 systimestamp 和sys_extract_utc()来做到这一点:

select to_char(sys_extract_utc(systimestamp), 'yyyy-mm-dd"T"hh24:mi:ss"Z"') dt_as_utc
from dual;

DT_AS_UTC
--------------------
2016-11-28T10:33:49Z