如何将字符序列转换为日期格式以存储在数据库中? [英] How to convert a sequence of characters into a date format to store in the database?
问题描述
基于文件名,我必须将字符串分解为不同的元素数组,并且能够从文件命名约定中提取时间戳,现在我需要将此字符串传递为日期格式以存储到数据库表:
Based on the filename i have to break down the string into a different array of elements and i was able to extract the timestamp from the file naming convention and now i need to pass this string to a date format to be stored into the db tables:
示例:需要将 20200913204839
字符串转换为日期格式为 13-sep-2020 20:48:39
(列名),并且pl/sql块具有此格式的参数值列名-数据类型(varchar2)-插入语句中的值是:
Example:
20200913204839
string needs to be converted into date format as 13-sep-2020 20:48:39
(column_name) and the pl/sql block has the parameter value in this format column name-datatype (varchar2) -and value inside the insert statement as :
to_date(column_name,'dd-mon-yyyy hh24:mi:ss')
这是等效的matlab代码:
This is the equivalent matlab code:
fn.fntestdate = datestr(datenum(column_name,'yymmddHHMM'),'dd-mmm-yyyy HH:MM:SS')
如何在 Java 中加入相同的逻辑?
How do i incorporate the same logic in Java?
我尝试了几个函数 SimpleDateFormat 试图对其进行解析,但是却收到了无法解析的函数".错误.
I tried several functions SimpleDateFormat trying to parse it,but was receiving "unparseable funcction" ERROR.
我认为column_name必须先转换为数字,然后再转换为日期才能解析?加载之前需要多次隐式转换.为此建议最佳的方法
I think column_name has to be converted into number and then converted to date before parsing it? Requires several multiple implicit conversions before loading. Suggest the best possible approach for this
推荐答案
以下是适用于您的测试用例的示例:
Here's an example that works with your test cases:
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="20200913204839";
Date date1=new SimpleDateFormat("yyyyMMddhhmmss").parse(sDate1);
System.out.println(date1);
}
}
对于测试用例 20200913204839
,它返回:
With test case 20200913204839
it returns:
Sun Sep 13 20:48:39 UTC 2020
这是SimpleDateFormat中的字符的含义:
Here's what the characters in the SimpleDateFormat mean:
yyyy
表示年份(例如2020年)
yyyy
signifies a year (Example: 2020)
MM
表示一个月(例如:七月,八月)
MM
signifies a month (Example: July, August)
dd
表示一个月中的某一天(例如15)
dd
signifies a day in a month (Example: 15)
HH
表示一天中的一个小时(示例:20)
HH
signifies an hour in a day (Example: 20)
mm
和 ss
分别表示分钟和秒(示例:52:34)
mm
and ss
signify minutes and seconds respectively (Example: 52:34)
它不必是日期-只需一个字符串就可以了.
It doesn't have to be a Date - just a String works fine.
是的-使用SimpleDateFormat可以正常工作.
And yes - using SimpleDateFormat works just fine.
特定格式
我已经针对您的情况进行了专门的格式化:
I've formatted it specifically for your case:
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="20200913204839";
Date date1=new SimpleDateFormat("yyyyMMddhhmmss").parse(sDate1);
String formatted = new SimpleDateFormat("dd-EE-yyyy hh:mm:ss").format(date1);
System.out.println(formatted);
}
}
我们首先将String解析为一个日期,然后可以对其进行格式化.
We first parse the String into a date, from which we can format.
一个测试用例 20200913204839
返回:
13-Sun-2020 08:48:39
这篇关于如何将字符序列转换为日期格式以存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!