-1

I need to delete some records from the database. In the table, I have a column with Date (java.util.Date) records. I need to get from the table the rows that are older than x hours for example. My function has an int Y that deserves seconds. I need to make something like that. Date (currentDate) - Y. And after that to make a query where I compare that column with the calculation. Any suggestion?

Robert Vasile
  • 111
  • 1
  • 9
  • 1
    The SQL syntax will be slightly different depending on which relational database you're using. – Gilbert Le Blanc Jan 05 '21 at 11:57
  • is Hana. I try to make this query with this condition "< now() - interval 100 SECOND" but it doesn't work in Hana. I need to subtract the current date with that x parameter what is seconds and after in query to put that condition that column < calculatedTime. – Robert Vasile Jan 05 '21 at 12:00
  • 2
    Maybe something like this? https://stackoverflow.com/questions/18726873/where-datetime-older-than-some-time-eg-15-minutes/18726918 Also I think you shouldnt use java.util.Date and Calendar now, you should use new Time Api, for example LocalDateTime, LocalDate etc. These classes have their own implementations for time adding and subtraction. – MrFisherman Jan 05 '21 at 12:01
  • 1
    Please provide more info. What have you tried already? – HoRn Jan 05 '21 at 12:01
  • @MrFisherman I did in that way, but hana don't recognize that condition – Robert Vasile Jan 05 '21 at 12:03
  • @HoRn I need to delete some records. In the table, I have a column with Date (java.util.Date) records. I need to get from the table the rows that are older than x hours for example. My function has an int Y that deserves seconds. I need to make something like that. Date (currentDate) - Y. And after that to make a query where I compare that column with the calculation – Robert Vasile Jan 05 '21 at 12:06
  • Great. I would advise you to add all this clarifying info as an edit to your post, not as a comment – HoRn Jan 05 '21 at 12:09
  • 1
    This is very easy if you use [LocalDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html) from java.time. – Joakim Danielson Jan 05 '21 at 12:26
  • I recommend you don’t use `java.util.Date`. That class is poorly designed and long outdated. Instead use for example `OffsetDateTime` or `LocalDateTIme`; both are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also *I have a column with Date (java.util.Date) records* is nonsense. The database engine uses its own timestamp or datetime type which is different from `java.util.Date`. – Ole V.V. Jan 05 '21 at 16:38
  • A recommended solution is to adapt [this answer by Basil Bourque](https://stackoverflow.com/a/33404165/5772882). – Ole V.V. Jan 05 '21 at 16:59
  • This is your lucky day. :-) Live and Let Live just wrote [a new, modern. good and exhaustive answer for you here](https://stackoverflow.com/a/65586534/5772882). – Ole V.V. Jan 06 '21 at 03:53

2 Answers2

2

First, if you work with java 8 or higher I would strongly advise not to use outdated class java.util.Date and switch to package java.time. You can use class ZonedDateTime or any of its "brothers" such as LocalDateTime or others.

But, to answer your question here is how you can substruct seconds from Date class:

Date date = new Date(); //Current date
long seconds = 100L; // 100 seconds to substruct
date.setTime(date.getTime() - seconds * 1000); //convert seconds to milliseconds and substruct it from current value of date
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

I solved this with this function. I hope will be helpful for somebody.

private Date calculateTheDate(final int seconds){

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, - seconds);

    return calendar.getTime();
}
Robert Vasile
  • 111
  • 1
  • 9
  • You have found the old-fashioned and poor solution. I don’t recommend it. I suggest that no one should use it. Thanks for posting the solution that you found, though. – Ole V.V. Jan 05 '21 at 17:01