-1

I have a big problem with a date that I have to formatting in the right way. My proposal of solution following:

...
//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);
...

I need a format like strDate, because I need this format for the database, but I need a Date and not a String and I don't know how can I formatting in the right way my Date string.

5
  • 1
    I'm not sure what you're asking. You have a Date already. A Date is a moment in time. The representation of a date can be a string. Commented Apr 16, 2021 at 17:19
  • 1
    I recommend you don’t use SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use LocalDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API. Commented Apr 16, 2021 at 18:20
  • Does this answer your question? display Java.util.Date in a specific format. Does this? Commented Apr 16, 2021 at 18:22
  • Since you get 1935-05-12 00:00:00.0, it looks like you are really getting a java.sql.Timestamp object out of your array. It’s another poorly deigned and long outdated class, a subclass of Date and a true hack on top of it. Commented Apr 16, 2021 at 18:24
  • Also pleas explain, when the time of day in the original object was 00:00:00.0, how can 12:00:00 be perfect? Commented Apr 16, 2021 at 18:27

1 Answer 1

-1

You can store the Date as is in the DB, and then after you retrieve it, you can convert it to whatever format you need for the DTO level.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, if you’ve got a database, then you can. Does it answer the question? How? (Not your downvoter)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.