从Oracle数据库转换具有AM/PM日期时间的字符串 [英] Conversion of string with AM/PM date-time, from Oracle database
问题描述
我有03-AUG-12 08.15.00.000000000 PM -05:00
形式的时间戳
我无法以yyyy-MM-dd HH:mm:ss
的形式获取String
表示形式.
I have the timestamp in form of 03-AUG-12 08.15.00.000000000 PM -05:00
I am unable to get a String
representation in form on yyyy-MM-dd HH:mm:ss
.
这是我的代码:
public static void convert() {
String oldstring = "03-AUG-12 08.15.00.000000000 PM -05:00";
Date date = null;
try {
date = new SimpleDateFormat("dd-MMM-yy HH.mm.ss.S aa").parse(oldstring);
}
catch (ParseException e) {
e.printStackTrace();
}
String newstring = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(newstring);
}
基本上,这是Oracle数据库中具有时区格式的时间戳.
Basically it is a timestamp with timezone format from the Oracle database.
推荐答案
您不能使用SimpleDateFormat解析这样的字符串,至少并非没有一些限制:
You can't use SimpleDateFormat to parse such a string, at least not without some limitations:
-
直到Java 7才支持像-05:00(根据ISO 8601)这样的时区指示符.使用Java 7,您可以使用XXX模式来解析它.
A time zone designator like -05:00 (according to ISO 8601) is not supported until Java 7. With Java 7 you can use the XXX pattern to parse it.
要正确解析月份名称,应指定您需要英语语言环境.
To parse the month name correctly, you should specify that you require an English locale.
以毫秒为单位的模式(S)解析无限数量的数字.如果您的字符串包含"08.15.00.100000000",则SimpleDateFormat会将其解析为8:15:00和100000000ms,从而使预期值增加了将近28小时.如果您确定该值始终为0,则可以忽略此问题.
The pattern for milliseconds (S) parses an unlimited number of digits. If your string contains "08.15.00.100000000", SimpleDateFormat would parse this as 8:15:00 and 100000000ms, adding almost 28 hours to the expected value. If you are sure that the value is always 0, you can ignore this problem.
如果您可以接受最后一个问题并使用Java 7,则应该使用类似以下的内容:
If you can accept the last issue and use Java 7, you should be using something like this:
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa XXX", Locale.ENGLISH)
这篇关于从Oracle数据库转换具有AM/PM日期时间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!