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
Oracle date to Java date
提问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 h
for "Hour in am/pm (1-12)" and H
for "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 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
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 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。