Rahul.V
Rahul.V

Reputation: 1

LocalDateTime Conversion

I am trying to convert current date and time with the help of LocalDateTime java class and then trying to store it in Database table column having Date datatype.

Below is the code , i am trying to perform

SewaApiReqResEntity sewaApiHealthReqRes = null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
String currentDateString = LocalDateTime.now().format(formatter);
LocalDateTime sentTime = LocalDateTime.parse(currentDateString, formatter);
sewaApiHealthReqRes.setSentTime(sentTime);
System.out.println(sewaApiHealthReqRes.getSentTime());

Below is my entity class - i have shown only the desired column :

public class SewaApiReqResEntity {

@Column(name = "sent_time")
private LocalDateTime sentTime;
}

I am using Oracle SQL Developer. And in DB, datatype of that column is DATE:

SENT_TIME DATE

With the above code, i am getting this format:

2024-04-29T18:24:41

and we have to store values in below format

03/28/2024 17:42:40

I have tried to change the datatype of Entity class from LocalDateTime to date and same in service class i used Date class for conversion.

Upvotes: -3

Views: 106

Answers (1)

rzwitserloot
rzwitserloot

Reputation: 102785

String currentDateString = LocalDateTime.now().format(formatter);
LocalDateTime sentTime = LocalDateTime.parse(currentDateString, formatter);

Utterly useless. I am flabbergasted that this simple mental model error comes up every 15th 'java' tagged question.

LocalDateTime has no format whatsoever.

They simply do not have one. They store year, month, day, hour, minute, and second. Period. They don't store that 'as a string'. It's simply this, nothing more, nothing less:

class LocalDateTime {
  int year, month, day, hour, minute, second;
}

Hence why you cannot 'print' an LDT object just with the LDT object. Instead you ask a DateTimeFormatter object to print an LDT by handing it an LDT. Yes, LDTs have a toString() method. Like all toString() methods, they are for debugging only and should never be used for producing a string you show to end users or you send to other code. You cannot change what it prints.

Hence, the snippet you wrote is simply an inefficient, slow way to do this equivalent:

LocalDateTime sentTime = LocalDateTime.now();

formatting and then unformatting it is just converting it to some state and right back again for no purpose whatsoever.

System.out.println(sewaApiHealthReqRes.getSentTime());

This calls the toString(). You cannot modify what the toString prints. If somehow that is relevant, whatever code you have that uses the toString output of an LDT is broken and you must fix it there, you can't fix it here.

and we have to store values in below format

LDTs don't store anything in any format. It is not possible to store it in some specific format. "It must be in this format" is a sentence you say about an API or storage mechanism where you store data in string form. LocalDateTime does not do that, therefore, the sentence cannot possibly apply to them. Whatever SewaApiRequestEntity is, is either [A] a buggy mess you cannot use or must first fix, or [B] will take care of putting the LDT in the appropriate format when sending it to 'sewa', whatever that might be. In other words, don't worry about this, everything is working fine. Alternatively, if it isn't, you can't fix it here, you have to fix whatever SewaApiRequestEntity is.

Upvotes: 3

Related Questions