-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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
BigShow
  • 1
  • 3
  • 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. – Dave Newton Apr 16 '21 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](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 16 '21 at 18:20
  • Does this answer your question? [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format). Does [this](https://stackoverflow.com/questions/50719313/convert-java-date-from-one-format-to-another-without-converting-it-into-string)? – Ole V.V. Apr 16 '21 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. – Ole V.V. Apr 16 '21 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? – Ole V.V. Apr 16 '21 at 18:27

1 Answers1

-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.

Coding_Cat
  • 21
  • 2
  • Indeed, if you’ve got a database, then you can. Does it answer the question? How? (Not your downvoter) – Ole V.V. Apr 16 '21 at 21:38