android - Parsing String to Calendar in Java? -
i having trouble parsing string calendar object in java pulling xml element (i'm writing android app) can calculate length of time between time in string , current time. brand new java , come .net world, additional background here can helpful.
the code trying use is:
// using class define format date parsing. simpledateformat sdf = new simpledateformat("yyyymmdd hh:mm:ss", locale.english); // readarrivalvalue reads element xml, seems working. string prdt = readarrivalvalue(parser, "prdt"); // @ point prdt evaluates correct value: "20150331 20:14:40" date dateprdt = sdf.parse(prdt); // dateprdt doesn't seem set properly. evaluates to: // dateprdt = {java.util.date@830031077456}"sat jan 31 20:14:40 cst 2015" // milliseconds = 1422761216000 // element.prdt of type calendar. nothing done prior statement. element.prdt.settime(dateprdt); // throws null pointer exception
two issues code:
fix
simpledateformat
pattern you're parsing minutesmm
in place of monthsmm
too!new simpledateformat("yyyymmdd hh:mm:ss", locale.english); ^
you forgot initialize
calendar
instance , hence npe.element.prdt = calendar.getinstance(); element.prdt.settime(dateprdt);
Comments
Post a Comment