1

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?

Oliver
  • 1,218
  • 1
  • 17
  • 21
  • 3
    How many minutes is `300000` milliseconds? – Sotirios Delimanolis Sep 17 '18 at 17:18
  • Gosh, thanks...I guess I should go home today... – Oliver Sep 17 '18 at 17:20
  • 1
    You are using terrible old classes that were supplanted years ago by the *java.time* classes. – Basil Bourque Sep 17 '18 at 20:00
  • 1
    Tip: learn about standard ISO 8601 formats. – Basil Bourque Sep 17 '18 at 20:01
  • 1
    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
  • Thanks for the answers, the thing is, I am obliged to use an older JDK unfortunately... – Oliver Sep 18 '18 at 11:39
  • 1
    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](https://www.threeten.org/threetenbp/), 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 06:43
  • I am not allowed to use third party libraries as well, the policy is quite strict.. – Oliver Sep 19 '18 at 16:41

1 Answers1

0

In Time 2018-01-16-18.56.57.300000, Your 300000 milliseconds are being converted to minutes

which is 300000/60000 = 5 minutes
Aagam Jain
  • 1,546
  • 1
  • 10
  • 18