String to Timestamp conversion difference error in Java [duplicate]
String to Timestamp conversion difference error in Java [duplicate]
This question already has an answer here:
I have the following value:
2018-01-16-18.56.57.300000
It is passed to the method parameter: "value".
private Timestamp getPossibleTimestampI(String value)
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss.SSS");
Date parsedDate;
Timestamp timestamp=null;
try
parsedDate = dateFormat.parse(value);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
catch (ParseException e1)
e1.printStackTrace();
return timestamp;
I am getting a Timestamp object with the value of 2018-01-16 19:01:57.0, about 5 minutes more compared to the original string value.
Why is this happening, and how can I correct my conversion?
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
300000
You are using terrible old classes that were supplanted years ago by the java.time classes.
– Basil Bourque
Sep 17 '18 at 20:00
Tip: learn about standard ISO 8601 formats.
– Basil Bourque
Sep 17 '18 at 20:01
You shouldn’t be wanting a
Timestamp
at all. That class is outdated and filled with design problems. For saving into your database use an Instant
or if circumstances dictate, a LocalDateTime
.– Ole V.V.
Sep 18 '18 at 11:35
Timestamp
Instant
LocalDateTime
Since an older JDK (like version 6 or 7) cannot parse your string, I suggest you use the backport of java.time, the ThreeTen Backport library, it can. Only you will have to use its
DateTimeUtils.toTimestamp
method for converting to a Timestamp
for your database.– Ole V.V.
Sep 19 '18 at 6:43
DateTimeUtils.toTimestamp
Timestamp
1 Answer
1
In Time 2018-01-16-18.56.57.300000, Your 300000 milliseconds are being converted to minutes
which is 300000/60000 = 5 minutes
and how can I correct my conversion?
– Sotirios Delimanolis
Sep 17 '18 at 17:32
Remove miliseconds before conversion.
– Aagam Jain
Sep 17 '18 at 17:42
How many minutes is
300000
milliseconds?– Sotirios Delimanolis
Sep 17 '18 at 17:18