0

I am trying to convert String to Timestamp to save in my postgresql database using Java Springboot. The problem I'm facing is a problem I read in many different topics here but it seems I might be missing something and I cannot see what exactly. My assumption is that something wrong is happening to SimpleDateFormat object and it's not formatting the date appropriately.

This is my function to convert from String to Timestamp. The string i pass here is taken from Android sending it with Volley requests.

private Timestamp convertDate(String date){
    try {
        System.out.println("Passed date to method: " + date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd HH:mm");
        Date parsedDate = simpleDateFormat.parse(date);
        System.out.println("Pre returned date: " + parsedDate);
        Timestamp timestamp = new Timestamp(parsedDate.getTime());
        System.out.println("Pre returned timestamp: " + timestamp);
        return timestamp;
    }catch (ParseException e){
        return null;
    }
}

And this is what is in my logs.

Passed date to method: 11/06 12:47
Pre returned date: Fri Nov 06 12:47:00 UTC 1970
Pre returned timestamp: 1970-11-06 12:47:00.0

Any help would be greatly appreciated.

Galaxiset
  • 69
  • 7
  • 2
    it seems like the code is working fine..what is the output that you expect? also since you are not giving a year, it takes 1970 by default.. – Jos Nov 06 '16 at 18:22
  • @redflar3 I would like to get the exact same date as I have in the String but now saved as a Timestamp. – Galaxiset Nov 06 '16 at 18:26
  • isn't thats what you are getting? 11/06 12:47 -> 1970-11-06 12:47...? if you expect something else, please give an example input and exactly what you expect in log for that input.. – Jos Nov 06 '16 at 18:28
  • @redflar3 I would like for Timestamp not to include the year and miliseconds since I didn't pass them. Is it possible? I would like Timestamp to look like 11-06 12:47:00 or 11-06 12:47 – Galaxiset Nov 06 '16 at 18:32
  • i don't think that is possible.. Timestamp has to store some day. You can either modify the code to provide your own date or keep 1970 and ignore/hide the year while displaying the Timestamp.. – Jos Nov 06 '16 at 18:42
  • 1
    @redflar3 Okey, thank you for your quick response. I appreciate it. – Galaxiset Nov 06 '16 at 18:44
  • Not a duplicate. This Question is about omitting the year. – Basil Bourque Nov 06 '16 at 19:28
  • For a month, day, and time-of-day but no year… No such data type in Java nor Postgres. Break it up into a pair of values. In Java, use the `MonthDay` and `LocalTime` classes. In Postgres store the string generated by `MonthDay::toString` which uses standard ISO 8601 format of --MM-DD, such as `--10-23` for October 23rd. And pass the `LocalTime` to Postgres via `PreparedStatement::setObject` to be stored in a column of type `TIME WITHOUT TIME ZONE`. – Basil Bourque Nov 06 '16 at 19:36

0 Answers0