从Oracle导出的数据是这样的格式的 2022-06-19 11:25:37.602000,想要转换成date或者timestamp插入数据库,需要进行格式转换
分析了2种格式转换,供大家参考
@Test
public void testdate2() {
String time = "2022-06-19 11:25:37.602000";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
try {
long t1 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Date date = df.parse(time);
System.out.println(date.getTime());
}
long t2 = System.currentTimeMillis();
//经测试下面方式效率更高
for (int i = 0; i < 1000; i++) {
Timestamp date1 = Timestamp.valueOf(time);
System.out.println(date1.getTime());
}
long t3 = System.currentTimeMillis();
System.out.println((t2 - t1) + " " + (t3 - t2));
} catch (ParseException e) {
e.printStackTrace();
}
}
经过 测试发现,Timestamp date1 = Timestamp.valueOf(time); 这种方式更快,而且更准确