0

I've got Timestamp format in DB and my setEventDate method needs Date format. So in my DAO class there is something like this :

                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                Timestamp dbDate = rows.getTimestamp("event_date");
                String dbDateToString = new SimpleDateFormat("HH:mm").format(dbDate);
                Date dbDateToDate = sdf.parse(dbDateToString);
                e.setEventDate(dbDateToDate);
                System.out.println(dbDateToString);
                System.out.println(dbDateToDate);

I'm getting Timestamp from DB, format it to String and in next step I'm parsing it to Date. I know that it sounds weird. The result is: String - 17:08 Date - Thu Jan 01 17:08:00 CET 1970

I don't get it :/ I need that "HH:mm" format.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
majkoz0rd
  • 31
  • 8
  • What is the date String you are using? If you don't specify a month and year, it defaults to Jan 1, 1970. – Yserbius Mar 27 '18 at 15:35
  • Timestamps have no format in the database, nor has a `java.util.Date` a format. Your assumptions make no sense. Your problem comes from converting a timestamp to an hour:minute string, and then parsing that back to a date (which in absence of date info will default to January 1st 1970), and then printing using its default `toString`... If you want HH:mm, then you need to store it in a string and you would have been ready after producing `dbDateToString`. – Mark Rotteveel Mar 27 '18 at 16:31
  • @Mark Rotteveel So there is String dbDateToString with HH:mm format. In my POJO class there is private Date eventDate; I cannot use that String dbDateToString like this: smth.setEventDate(dbDateToString); because my method needs Date format. – majkoz0rd Mar 27 '18 at 16:53
  • You might consider using [java.time.LocalTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html) instead. – Gord Thompson Mar 27 '18 at 17:22
  • 1
    The only thing you need to do (as shown in the [answer by Arthur](https://stackoverflow.com/a/49517456/466862) is to do `new Date(dbDate.getTime)`. The presentation (formatting as `HH:mm`) is a later concern. And if you really only need the time, then 1) an SQL `Timestamp` is the wrong type (you should use a SQL `Time`), and 2) you might be better of using a `LocalTime` as suggested by Gord. – Mark Rotteveel Mar 28 '18 at 09:12

2 Answers2

2

You are taking a Timestamp from your DB, you need to change it a Date first

Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime();  

now you can do your SimpleDateFormat formatting stuff

Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime());  
String dbDateOnlyHourAndMinutes = new SimpleDateFormat("HH:mm").format(dbDate);

there you have your Hour and minutes only "Date".

arthur
  • 3,245
  • 4
  • 25
  • 34
  • Your usage of `new Date(dbDate.getTime() + (dbDate.getNanos() / 1000000))` is incorrect. A `Timestamp.getTime()` already adds the milliseconds that are part of `getNanos()`. Specifically it does `long time = super.getTime(); return (time + (nanos / 1000000));` – Mark Rotteveel Mar 27 '18 at 16:24
  • @MarkRotteveel Thanks man for the correction, I've updated my answer. – arthur Mar 28 '18 at 07:55
0

You say you want to parse date but in your date format you are passing only hours (HH) and minutes (mm) format. Check how to create your desired date format from :

What are the date formats available in SimpleDateFormat class?

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

example of a date format is: dd/MM/yyyy.

In case i did not understood correctly please explain more. Here is an example code to convert the timestamp to your format. ASSUMPTION: desired date format is MM:yyyy:

    Timestamp stamp = new Timestamp(System.currentTimeMillis());//your timestamp goes here
    Date date = new Date(stamp.getTime());
    System.out.println(date);//result = Tue Mar 27 18:50:09 EEST 2018
    SimpleDateFormat sdf = new SimpleDateFormat("MM:yyyy");
    System.out.println(sdf.format(date)); //result = 03:2018
LenosV
  • 172
  • 1
  • 6