0

I need to convert a String timestamp into Date timestamp as the column in database is in Date format. The following is the code (method) I have written which successfully converts the str in the parameter to "yyyy.MM.dd HH:mm:ss" string format but I need it to be converted to Date. Can you please tell how I can do this?

public String convertTimestampToDate(String str) throws ParseException {
        Date timestamp = new SimpleDateFormat("yyyyMMddHHmmss").parse(str);
        String uploadTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(timestamp);
        return uploadTime;
}
  • If your question is how to create a java.util.Date object from a String: You already did. Right here: `Date timestamp = new SimpleDateFormat("yyyyMMddHHmmss").parse(str);` That is converting your string to a Date object. – OH GOD SPIDERS Apr 21 '21 at 13:27
  • @OHGODSPIDERS yes but eventually I am returning uploadTime which is String format so when I am using this method to upload date in db, I get an error. I need a method where I can accept a string and convert it to yyyy.MM.dd hh:mm:ss format which should be of type Date. – Gooner Coder Apr 21 '21 at 13:30
  • Well, then don't return the String and instead return the Date object you created. Oh and: java.util.Date objects do not have a "format". Saying you want to create a java.util.Date with a specific format makes therefor no sense. As I said that one line is already what you need. – OH GOD SPIDERS Apr 21 '21 at 13:33
  • @OHGODSPIDERS ok I have removed that line but now I get the date in the following format which again gives error Thu Mar 09 14:04:04 EET 2017 public Date convertTimestampToDate(String str) throws ParseException { Date timestamp = new SimpleDateFormat("yyyyMMddHHmmss").parse(str); return timestamp; } I only need "yyyy.MM.dd HH:mm:ss" to be uploaded to database. – Gooner Coder Apr 21 '21 at 13:45
  • 1
    Again: Java.util.Date as a data type does not have anything that you call format. A format only comes into play when you convert from or to a String. See: https://stackoverflow.com/questions/66664791/converting-the-format-of-the-date-in-java – OH GOD SPIDERS Apr 21 '21 at 13:48
  • If you have an error when trying to save to a Database the error is therefor not because your java.util.Date is in a wrong format, because that is impossible as they have no formats. The error is most likely something else you do when trying to save to your Database. But since you did not include any details on how you use that java.util.Date to save to your Database I cannot tell you more here – OH GOD SPIDERS Apr 21 '21 at 13:51
  • @OHGODSPIDERS the error I am getting while uploading this date to db is "ora-01858: a non-numeric character was found where a numeric was expected" and when I searched about it, I found that because I am trying to input a different format as to different from just Date, that is why I am getting that error. – Gooner Coder Apr 21 '21 at 13:54
  • The "Upload Time" column in db expects a Date to be inserted there of the following format, yyyy.MM.dd HH:mm:ss. After removing the middle line from my above code, I get "Thu" and I think thats why I am still getting the error. – Gooner Coder Apr 21 '21 at 13:57
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 21 '21 at 14:46
  • 1
    You may want to show us the code from which you get that error message. That will probably help us help you. – Ole V.V. Apr 21 '21 at 15:34
  • For passing a date and time to your SQL database (e.g., Oracle), pass a `LocalDateTime` to your `PreparedStatement` or other means from JDBC, and don’t worry about its format, it will work. See [my answer here](https://stackoverflow.com/a/54907501/5772882) and remember to read the last three lines. – Ole V.V. Apr 21 '21 at 16:35
  • @OleV.V. actually I need to input a date after reading a timestamp from a filename and then parsing it to yyyy.MM.dd HH:mm:ss format and then uploading it to a column in db. I think LocalDateTime will give me the current time which I don't need. – Gooner Coder Apr 22 '21 at 09:35
  • No, @GoonerCoder, parse the timesamp fomr the filename into a `LocalDateTime`, and you will get the time in that timestamp. You should not pass a date and time as string to your database. – Ole V.V. Apr 22 '21 at 10:11

0 Answers0