简体   繁体   English

如何通过Oracle函数to_date()将java格式ddMMyyyyHmmss的字符串转换为日期?

[英]How to convert a string in java-format ddMMyyyyHmmss to a date by Oracle function to_date ()?

For example, there is a string line 0106201395810 (or 31052013155754 ), which fits the format of new SimpleDateFormat ("ddMMyyyyHmmss") , ie in the first case, 1 June 2013, 9 hours 58 minutes 10 seconds . 例如,字符串行0106201395810 (或31052013155754 )适合new SimpleDateFormat ("ddMMyyyyHmmss")的格式,即在第一种情况下, 2013年6月1日,9小时58分10秒 Variant with to_date ('0106201395810 ',' DDMMYYYYHH24MISS ') is not suitable because it involves two characters in HH24 . 带有to_date ('0106201395810 ',' DDMMYYYYHH24MISS ')变体不适合,因为它在HH24中涉及两个字符。

You could try reassembling the string along these lines to have a zero in the right place- and use the DDMMYYYYHH24MISS format 您可以尝试将这些字符串重新组装为在正确的位置具有零,并使用DDMMYYYYHH24MISS格式

to_date(
 case length(input)
  when 14 THEN -- the length is OK: this has 2 digits at the hour
     input
  when 13 THEN -- length is one short, have to add one '0'
     substr(input,0,8) || '0' || substr(input, 9)
  END,
'DDMMYYYYHH24MISS')

Though I must say, this is ugly , and probably has "less than optimal" performance... I'd much rather change the Java side format, as this format is clearly not a standard one. 尽管我必须说,这很丑陋 ,并且可能具有“低于最佳性能”的性能……我宁愿更改Java端格式,因为这种格式显然不是标准格式。 Individualism can be good at times - this is clearly not the case. 个人主义有时可能很好-事实并非如此。 This will just cause pain in the long term... 从长远来看,这只会引起痛苦...

I am not quite sure about your specific need to use ddMMyyyyHmmss ; 我不确定您是否需要使用ddMMyyyyHmmss; myself will be using HH for a 24hrs date format 我将使用HH格式的24小时格式

Java 爪哇

String s1 = "0106201395810";
Date d1 = (new SimpleDateFormat("ddMMyyyyHmmss")).parse(s1);
String s2 = (new SimpleDateFormat("ddMMyyyyHHmmss")).format(d1);
System.out.println("s2 "+s2);

SQL 的SQL

select to_date ('01062013095810', 'DDMMYYYYHH24MISS' ) from dual  

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

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