0

In my sql query I have date formating as :

to_char(crtd_ts,'YYYY-MM-DD HH24:MI:SS.FF') crtd

and my database column stores the value as 2018-4-24.8.1. 30. 404577000

What is the way of doing the same thing in Java? I tried this way, but I am getting an error.

private String formatDate(String refCrtdTs) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH24:mm:ss.ff");
    String dateInString = refCrtdTs;
    try {
        Date date = sdf.parse(dateInString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

I am trying to replace the above sql query with hibernate criteria. My hibernate entity for the column is defined as

@Column(name="REF_CRTD_TS",columnDefinition="timestamp")
private String refCrtdTs;

and the column type in Oracle is Timestamp. So hibernate criteria returns me this value as String which I want to format now.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Aadil
  • 45
  • 3
  • 11
  • 2
    `DATE` columns do **not** have "a format". Use `ResultSet.getTimestamp()` to read the value from the database then you don't need to parse or format anything, do not read and write date or timestamp values as Strings –  Apr 24 '18 at 08:53
  • Possible duplicate of [How do I format a java.sql.date into this format: "MM-dd-yyyy"?](https://stackoverflow.com/questions/24320378/how-do-i-format-a-java-sql-date-into-this-format-mm-dd-yyyy) – Elarbi Mohamed Aymen Apr 24 '18 at 08:53
  • And what is the error? – CannedMoose Apr 24 '18 at 08:54
  • As @a_horse_with_no_name mentioned and from your question it seems, the columns is of type datetime already, you are converting it to a string while querying database and then the output string is passed through a similar function in java to convert it into a Date object. Why 2 transformations? – Himanshu Bhardwaj Apr 24 '18 at 09:00
  • I am replacing the above sql query with hibernate criteria. Updated my question – Aadil Apr 24 '18 at 09:33
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 24 '18 at 15:28
  • Can’t you persuade Hibernate to give you a `LocalDateTime` object (or an `Instant`) rather than a string? You should prefer to base your modelling on date-time classes rather than strings. I am asking with very little experience with Hibernate a long time ago. The classes I mention are part of [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 24 '18 at 15:57

2 Answers2

1

Try this,

yy-M-d H:m:s.F

And your database column stores the value should be "2018-4-24 8:1:30.114"

F is Day in year (example: Feb 1st => F = 32, 31 day in Jan + 1 day in Feb)

1

You have to match same patter as per your sql, just try the same pattern to get in Java I think this will work, please comment if you required more info

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS aa");
Kanthishere
  • 173
  • 3
  • 7