java - How to calculate expiry date when manufacturing date and best before month is given in database -
i have database hold value of manufacturing date , best before month date given in format of 'yyyy-mm-dd'. example date of manufacturing 2014-01-09 , best before month given 12 month or 11 month given in database. whenever try retrieving dates database gives exception bad format date '12' in column 2.
what tried is
public void calculateexpirydate(list<item> items) { // todo auto-generated method stub list<item> itemlist1 = new arraylist<item>(); string query = "select mfg_date,usebeforeinmonths cheese_tbl"; preparedstatement prepstmt = null; try { prepstmt = con.preparestatement(query); } catch (sqlexception e2) { // todo auto-generated catch block e2.printstacktrace(); } resultset rs = null; try { rs = prepstmt.executequery(); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { while (rs.next()) { item i1=new item(); java.sql.date dbsqldate = rs.getdate(2); i1.setmanufacturingdate(rs.getdate("mfg_date")); i1.setmanufacturingdate(rs.getdate("usebeforeinmonths")); itemlist.add(i1); } system.out.println(itemlist1); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } but gives me bad format date
can 1 me calculating expiry date of product. thank you.
if understand correctly, want give manufacturing date in yyy-mm-dd format, , store time till "best before date" in number of months.
i'd store duration in integer , use jodatime api (http://www.joda.org/joda-time/ or if use java se8 http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) calculate best before date.
this works follows :
localdatetime manufacturingdate = new localdatetime(rs.getdate("mfg_date")); localdatetime bestbefore = manufacturingdate.plusmonths(rs.getint("usebeforeinmonths")); hope helps.
Comments
Post a Comment