2

Do you know a format for this kind of date without losing the last 3 numbers?

I used a select query in my project to extract a date from a table and I need to use this date for a second query and extract other information on another table. The Date I got from the first query has this structure and the value is only an example:

"2022-09-23-11.25.52.660135"

I need to use this Date as String in order to do the second query, but when I use the format, I lose the last 3 numbers ( 135 ) and obviously the select returns nothing because the date is wrong.

I used this DateFormat for the first try:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SS");

Formats like yyyy-MM-dd-HH.mm.ss.SSS and yyyy-MM-dd-HH.mm.ss.SSSSSS don't work because it will just add zeroes and I need the exact same Date.

To solve this issue i had to edit the format of the column in the model class of the desired table. In my case for example

@Column(name = "TIMESTAMP_B_B03") private Timestamp timestampB03;

I had to change Timestamp and use LocalDateTime to get microseconds

ps: the real format of the column on DB is Timestamp but you need to use LocalDateFormat in java.

TheCrow
  • 39
  • 2
  • 2
    `DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS").parse("2022-09-23-11.25.52.660135");` use the new classes for representing instances in time – XtremeBaumer Jan 11 '23 at 10:38
  • 2
    Stay far away from `Date`, `DateFormat` and `SimpleDateFormat`. They give you nothing but trouble and are fortunately long outdated. Use java.time, the modern Java date and time API. – Ole V.V. Jan 11 '23 at 11:42
  • 1
    What data type has the value got in your database? And why would you need a string for your query? Prefer a datetime type (from java.time, as I said). – Ole V.V. Jan 11 '23 at 11:44
  • 2
    Check [this answer](https://stackoverflow.com/a/67752047/10819573) and [this answer](https://stackoverflow.com/a/67505173/10819573) to learn how to use `java.time` API with JDBC. It your case, it will be `LocalDateTime`. – Arvind Kumar Avinash Jan 11 '23 at 11:52
  • 1
    I checked the info about this column and it is a Timestamp on DB not a Date. I tried to get the date as LocalDateTime and now i get the entire date correctly – TheCrow Jan 11 '23 at 13:36
  • 1
    Great! Wish you more success! – Arvind Kumar Avinash Jan 11 '23 at 14:01
  • @TheCrow Would you write an answer to your own question based on what you got to work with `LocalDateTime`? I am sure it will be very helpful to future readers, and if it is, I will be happy to upvote it. – Ole V.V. Jan 11 '23 at 17:33

2 Answers2

1

Not only I do not know of any such format, but there exists no such format, because:

  • the available formats are listed exhaustively in the documentation of the class, and no such format is included in that list. If someone knew of some format which magically works even though it is not listed, you should not use it because this would constitute documentation by divine revelation and programming by magic.

  • both SimpleDateFormat and DateFormat were built to work with Date, which is defined by its documentation as having millisecond precision. Thus, there is no way to format microseconds because Date does not have microseconds.

In light of the above, your statement that you 'lose the "135"' is suspicious: what makes you believe that you had a 135 in the first place? Come to think of it, you have completely failed to explain something very important: are you trying to print a date, or to parse a date? If you are trying to parse a date, then what makes you believe that you can have that 135 in a date? That's why showing your code is always very important on StackOverflow.

To answer the "how do you do it" part:

  • If you really like SimpleDateFormat, you can use it to output the time coordinate without microseconds, then append a '.' to it, and then convert the microseconds to string by yourself, left-padding with zeros.

  • However, if you are using Instant, then you should be using the DateTimeFormatter class, which offers a format specifier for describing fractions of a second with precisions greater than milliseconds.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • I updated my question and i tried to explain better – TheCrow Jan 11 '23 at 11:22
  • Likely the OP is using [`java.sql.Timestamp`](https://docs.oracle.com/en/java/javase/17/docs/api/java.sql/java/sql/Timestamp.html), which has nanosecond precision (though not all DBMSes will actually support nanosecond precision), but this is not available when formatting with a `SimpleDateFormat`, IIRC. – Mark Rotteveel Jan 11 '23 at 11:31
  • @TheCrow you updated your question but you still haven't shown any code, nor have you shed any light into the mystery of exactly what data type you are trying to use for storing that date. – Mike Nakis Jan 11 '23 at 13:26
0

try this DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ssssss");

jayaraj
  • 19
  • 5