简体   繁体   English

Oracle约会到Java日期

[英]Oracle date to Java date

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"); 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" java.text.ParseException:Unparseable date:“2011-08-19 06:11:03.0”

Kindly please tell me the SimpleDateFormat to use. 请告诉我要使用的SimpleDateFormat。 Thanks. 谢谢。

You should use this Pattern "yyyy-MM-dd HH:mm:ss.S" instead of "yyyy/mm/dd hh:mm:ss.sss" . 您应该使用此模式"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)" h为和“AM / PM(1-12)小时” H “中,日,小时(0-23)”为
see here: SimpleDateFormat 看到这里: SimpleDateFormat

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

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 java.time

Furthermore, you are using terrible old date-time classes that are now legacy, supplanted by the java.time classes. 此外,您正在使用现在遗留下来的可怕的旧日期时间类,取而代之的是java.time类。

Your input string nearly complies with the ISO 8601 standard for date-time formats. 您的输入字符串几乎符合日期时间格式的ISO 8601标准。 Replace the SPACE in the middle with a T . T替换中间的SPACE。

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

Your input lacks any indicator of time zone or offset-from-UTC. 您的输入缺少任何时区指示符或与UTC的偏移量。 So we parse as a LocalDateTime , for an object lacking any concept of zone/offset. 所以我们解析为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.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 ,您可以直接与数据库交换java.time对象。 No need for strings or java.sql.* classes. 不需要字符串或java.sql。*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM