Java 中的对象到日期格式

问题描述

我有一个大问题,我必须以正确的方式格式化日期。我的解决方案如下:

...
//Read: 1935-05-12 00:00:00.0
Date dateOfBirth= (Date) arrayOfObject[3];

//Pattern: dd-MM-yyyy hh:mm:ss
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

//12-05-1935 12:00:00 (for me it's perfect but I need Date and no String)  
String strDate = dateFormat.format(dateOfBirth);  

//Read: Sun May 12 00:00:00 CET 1935
dateOfBirth= dateFormat.parse(strDate);

//Read: Sun May 12 00:00:00 CET 1935
rep.setDateOfBirth(dateOfBirth);
...

我需要像 strDate 这样的格式,因为我需要数据库的这种格式,但是我需要日期而不是字符串,我不知道怎么做我以正确的方式格式化我的日期字符串。

解决方法

您可以将日期按原样存储在数据库中,然后在检索它后,您可以将其转换为 DTO 级别所需的任何格式。