• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java SQL Date (360 days ago)  RSS feed

 
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi!  I want a date 360 days from todays date.  This code is giving the same "todays date"   ..... not a date 360 days before


 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This "past date" needed maybe set for lets say 30 days. I want to get this past date so I can delete any records in Derby database before this past date.
 
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getTime returns the timestamp in milliseconds since 1970, so your code is currently taken 'now' and subtracting 360 milliseconds.

I would suggest using the new time classes, probably Instant.
That has methods built in to handle this sort of thing.
It can then be converted back to  a sql.Date using toEpochMilli()...another method on Instant.
 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!   ..... I want to keep my data base from growing over one year of call records by itself.
My prepared statement is not right.  For every new record that gets stored into my Derby database, I then want to see if the oldest record is older than the "one year old" offset date .... if so, to DELETE that oldest record.  Also, I need to check for "null" ..... because I wount have any records over one year old for the first year!

 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..... after every new record saved to database, this prepared "Deleting" statement will be run next. ie: new record in , then check for oldest record in database.
 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


...... this is closer to what I want (I think) .... but still not right.

 
Dave Tolls
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in what way is it "not right"?
(Apart from the syntax, which I assume is deliberate?)
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What database are you using?  Comparing dates is a little different from DB to DB.

In MySql you can use WHERE myDate < '2016-01-01'.  To get the '2016-01-01' , the best way is a prepared statement.  See https://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html.

You will need a date formatter to format the date into a string, like '2016-01-01'.  See SimpleDateFormat (that is a link).
 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..... Derby Database.   
 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


..... don't understand why I am getting this error in my prepared statement:

now: 2017-05-20
OneYearOld: 2016-05-20

May 20, 2017 1:04:12 AM myNewSMDR.TwoWaySerialComm$SerialReader serialEvent

SEVERE: null
java.sql.SQLSyntaxErrorException: Column 'ONEYEAROLD' is either not in any table in the FROM list




 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..... stmtB statement
 
Rick Liberato
Ranch Hand
Posts: 104
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

......... looks like this got it!


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!