java - Get year of datetime where date is not fully specified with JDBC -


i have java program connected mysql database jdbc in introduce datetimes. many times year introduced assigning 00 days , months not specified, instance 1700-00-00 year 1700.

when try date resultset using resultset.getdate().tostring() proper date specified dates in case of partial dates get: previous_year-11-30, 1699-11-30 year 1700. when try resultset.getdate().gettime() equivalent result: -8520339600000. resultset.gettimestamp() gives same results.

conversely, when datetime mysql proper value 1700-00-00.

i have tried different years obtaining same results. appreciated.

here how fetch data (this 1 method, many other similar , give same results):

public naixement(int fill) throws dbexception{         super();         try {             string str = "select * naixement id_fill=?";             preparedstatement pst = con.preparestatement(str);             pst.setint(1, fill);             resultset rs = pst.executequery();             if (rs.next()){                 this.idfill = fill;                 this.idlloc = nullify(rs.getint("id_lloc"));                 this.idunio = nullify(rs.getint("id_unio"));                 try {                     try{                         system.out.println("data de naixement: "+rs.getstring("data_naixement")+                                 " id: "+fill);                     }catch (nullpointerexception e){}                     this.datanaixement = new date(rs.getstring("data_naixement"));                 } catch (dateexception ex) {                     this.datanaixement = new date();                 }             }else{                 throw new dbexception("s'ha intentat aconseguir un naixement "                     + "inexistent (id de la persona: "+fill+")");             }         } catch (sqlexception ex) {             system.err.println("excepció amb id_fill: "+fill);             logger.getlogger(municipi.class.getname()).log(level.severe, null, ex);         }     } } 

getdate method using implemented jdbc jar provided vendor(in case mysql). believe these methods internally uses datefromat convert dates database java.util.date causes problem. in mysql jan represented 1 , december 12 month of year datefromat used converts date considering year range 1-12.java.util.date doesn't work same way, consider 0 jan , 11 december , dateformat , date implemented in way can go next month or date if given date or month greater given range.

for example. if given month 13 date go jan of next year. 01-13-1700 go 01-jan-1701.

 dateformat dateformat=new simpledateformat("yyyy-mm-dd");     dateformat.setcalendar(new gregoriancalendar());     date d=dateformat.parse("1700-00-00");     system.out.println(d);     d=dateformat.parse("1700-01-01");             system.out.println(d);     d=dateformat.parse("1700-13-01");             system.out.println(d); 

execute code , better understanding output:-

mon nov 30 00:00:00 ist 1699 fri jan 01 00:00:00 ist 1700 sat jan 01 00:00:00 ist 1701 

problem have old values in db causing problem. need modify them or add custom logic fetch dates string , convert date.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -