Oracle 日期到 Java 日期

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

Oracle date to Java date

javaoracledate

提问by HashimR

What SimpleDateFormat to use for parsing Oracle date ?

什么 SimpleDateFormat 用于解析 Oracle 日期?

I'm using this SimpleDateFormat.

我正在使用这个 SimpleDateFormat。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss.sss");

its giving this exception.

java.text.ParseException: Unparseable date: "2011-08-19 06:11:03.0"

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss.sss");

它给出了这个例外。

java.text.ParseException:无法解析的日期:“2011-08-19 06:11:03.0”

Kindly please tell me the SimpleDateFormat to use. Thanks.

请告诉我要使用的 SimpleDateFormat。谢谢。

回答by oliholz

You should use this Pattern "yyyy-MM-dd HH:mm:ss.S"instead of "yyyy/mm/dd hh:mm:ss.sss".

您应该使用此 Pattern"yyyy-MM-dd HH:mm:ss.S"而不是"yyyy/mm/dd hh:mm:ss.sss".

little hfor "Hour in am/pm (1-12)" and Hfor "Hour in day (0-23)"
see here: SimpleDateFormat

h了“一小时的AM / PM(1-12)”,并 H为“在(0-23)一小时”活动
在这里看到:SimpleDateFormat的

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = dateFormat.parse("2011-08-19 06:11:03.0");

回答by Basil Bourque

tl;dr

tl;博士

LocalDateTime.parse( 
    "2011-08-19 06:11:03.0".replace( " " , "T" ) 
) 

Details

细节

Your input string does not match your formatting pattern. Your pattern has slash characters where your data has hyphens.

您的输入字符串与您的格式模式不匹配。您的模式有斜线字符,其中您的数据有连字符。

java.time

时间

Furthermore, you are using terrible old date-time classes that are now legacy, supplanted by the java.timeclasses.

此外,您正在使用可怕的旧日期时间类,这些类现在是遗留的,被java.time类取代。

Your input string nearly complies with the ISO 8601 standard for date-time formats. Replace the SPACE in the middle with a T.

您的输入字符串几乎符合日期时间格式的 ISO 8601 标准。将中间的 SPACE 替换为T.

String input = "2011-08-19 06:11:03.0".replace( " " , "T" ) ;

Your input lacks any indicator of time zone or offset-from-UTC. So we parse as a LocalDateTime, for an object lacking any concept of zone/offset.

您的输入缺少任何时区或UTC 偏移量指标。因此LocalDateTime,对于缺少任何区域/偏移量概念的对象,我们将其解析为, 。

LocalDateTime ldt = LocalDateTime.parse( input ) ;

To generate a string in standard format, call toString.

要生成标准格式的字符串,请调用toString.

String output = ldt.toString() ;

If this input was intended for a specific time zone, assign it.

如果此输入用于特定时区,请分配它。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

With a JDBC drivercomplying with JDBC 4.2or later, you may exchange java.timeobjects directly with your database. No need for strings or java.sql.* classes.

使用符合JDBC 4.2或更高版本的JDBC 驱动程序,您可以直接与您的数据库交换java.time对象。不需要字符串或 java.sql.* 类。

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多