java - How to find date values which are more than 10 days old -
this question has answer here:
- how compare dates in java? 11 answers
i using java , iterating on d.b. column in return gives me date , time string shown below:
string datetime = resultset.getseries().get(0).getvalues().get(0).get(0);
if iterate on resultset getting datetime values in format shown below.
2017-07-20t19:21:37.987792408z 2017-04-24t22:04:26.808753375z 2017-08-14t22:22:40.340772396z 2017-06-24t22:24:32.422544491z 2017-07-31t22:27:05.893368615z
out of these records, how can compare date string "current" date object , discard values more 10 days old?
i tried
date date = new simpledateformat("yyyy-mm-dd't'hh:mm:ssz").parse(s);
that didn't work. other idea?
edit: using java7 , using influxdb not provide sysdate column while querying. have come solution using java.
java.time
retrieve date-time values date-time objects, not strings.
the instant
class represents moment on timeline in utc resolution of nanoseconds.
instant instant = myresultset.getobject( … , instant.class ) ;
compare ten days before current moment.
instant = instant.now() ; instant tendaysago = now.minus( 10 , chronounit.days ) ; boolean prior = instant.isbefore( tendaysago ) ;
you may not want base "ten days ago" on utc. if not, apply time zone zoneddatetime
, localdate
. has been covered many times search stack overflow. think if "days" means (a) chunks of 24-hours or (b) calendar dates you.
fyi, strings happen in standard iso 8601 format. t
separates date portion time portion. z
short zulu , means utc. instant
class uses same format in tostring
method.
sql
generally, should such comparison work in database part of sql query rather in java app. database highly tuned work; app not.
string sql = "select * tbl_ when_ < ? " ; … make preparedstatement mypreparedstatement.setobject( 1 , tendaysago ) ; … execute
Comments
Post a Comment