Java 将 oracle 时间戳解析为 date.util.date

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

Java parse oracle timestamp to date.util.date

javaoracledatetimestamp

提问by danarj

i want to parse oracle timestamp (01-MAY-12 01.00.47.000000000 PM) to java.util.Datei used this:

我想解析 oracle 时间戳 ( 01-MAY-12 01.00.47.000000000 PM)java.util.Date我用这个:

Date dateStart=new SimpleDateFormat("yy-MM-dd HH:mm:ss.S").parse("01-MAY-12 01.00.47.000000000 PM");

but i get this error

但我收到这个错误

java.text.ParseException: Unparseable date: "2012-5-1.13.0. 47. 0"

java.text.ParseException:无法解析的日期:“2012-5-1.13.0.47.0”

回答by JB Nizet

You shouldn't have to parse anything. Use one of the ResultSet.getTimestamp()methods, and you'll have a java.sql.Timestampobject directly, which extends java.util.Date.

你不应该解析任何东西。使用ResultSet.getTimestamp()方法之一,您将java.sql.Timestamp直接拥有一个对象,它扩展java.util.Date.

java.sql.Timestamp ts = myResultSet.getTimestamp( … );

And this will have the additional advantage of being portale across databases and locales.

这将具有跨数据库和区域设置的门户的额外优势。

回答by David Aldridge

"yy-MM-dd"?

“YY-MM-DD”?

"01-MAY-12"

“12 年 5 月 1 日”

Is your day number really "12" and your year "01"?

你的天数真的是“12”,你的年份是“01”吗?

And how come your error shows "2012-5-1.13.0. 47. 0", which is presumably a date in yet another format?

为什么你的错误显示“2012-5-1.13.0.47.0”,这大概是另一种格式的日期?

回答by Jigar Joshi

If you are trying to access it using JDBC then as @JB Nizet suggested use getTimestamp()or if you just have String and need to parse to Date then do it by following

如果您尝试使用 JDBC 访问它,那么作为@JB Nizet 建议的使用,getTimestamp()或者如果您只有 String 并且需要解析为 Date,请按照以下步骤操作

Try with following format

尝试使用以下格式

01-MAY-12 01.00.47.000000000 PM

yy-MMM-dd hh.mm.ss.SSSSSSSSSS a