xml - Java time manipulation - subtracting 2 strings -


i pulling 2 time values (as strings) xml file using xpath, these values (for example) follows:

00:07

08:00

00:07 equal 7 minutes

08:00 means 8am, no date associated or needed (that handled elsewhere)

each of these values subject change in each xml file read. attempting follows:

  1. i need subtract or add (depending on situation) 7mins 8am , give me hh:mm time (eg: 07:53 or 08:07) in string can output csv
  2. next need produce 2 additional strings, 1 min before , 1 min after (eg: 07:52 , 07:54 or 08:06 , 08:08) need output csv

i have tried , can think of in relation time interpretation , manipulation minutes subtracted/added time , +/- 1 min there, being complete novice totally stuck despite reading , testing as find. spent last 2 days working joda time first time must missing fundamental cannot desired result either.

the question - how can achieve this?

some sample code gets me reading xml , printing time

 fileinputstream file = null;     try {         file = new fileinputstream(new file("output/xmlconfig.xml"));     } catch (filenotfoundexception ex) {         logger.getlogger(katt.class.getname()).log(level.severe, null, ex);     }         documentbuilderfactory builderfactory = documentbuilderfactory.newinstance();         documentbuilder builder = null;     try {         builder = builderfactory.newdocumentbuilder();     } catch (parserconfigurationexception ex) {         logger.getlogger(katt.class.getname()).log(level.severe, null, ex);     }         document xmldocument = null;     try {         xmldocument = builder.parse(file);     } catch (saxexception ex) {         logger.getlogger(katt.class.getname()).log(level.severe, null, ex);     } catch (ioexception ex) {         logger.getlogger(katt.class.getname()).log(level.severe, null, ex);     }         xpath xpath = xpathfactory.newinstance().newxpath();          //get in rule xml         string exceptioninearlyxml = "root/response/wsaexceptionrule/@inearly";         nodelist nodelistinearly = null;     try {         nodelistinearly = (nodelist) xpath.compile(exceptioninearlyxml).evaluate(xmldocument, xpathconstants.nodeset);     } catch (xpathexpressionexception ex) {         logger.getlogger(katt.class.getname()).log(level.severe, null, ex);     }         string exceptioninearly = (nodelistinearly.item(1).getfirstchild().getnodevalue());        string inearly = exceptioninearly;         simpledateformat format = new simpledateformat("hh:mm");        date d2 = null;      try {         d2 = format.parse(inearly);     } catch (parseexception ex) {         logger.getlogger(katt.class.getname()).log(level.severe, null, ex);     }          datetime dt2 = new datetime(d2);          system.out.println(dt2); 

this give me output of 1970-01-01t00:07:00.000+10:00

i have tried many permutations of code @ point of deleting , starting again scratch un-compilable, , not experienced enough yet able solve issue.

once have date object parsed time, use gettime() time in milliseconds , save long variables. parse offset time format , use numberformat number of minutes offset. add or subtract needed. take result , create new date(millis) apply format it.

here working example:

    string stime = "08:00";     string soffset ="00:07";     simpledateformat dateformat = new simpledateformat("hh:mm");     date dttime = null;     try {         dttime = dateformat.parse(stime);     } catch (parseexception e) {         // handle exception         return;     }      string[] offsethrsmins = null;     numberformat numberformat = numberformat.getnumberinstance();     long offsetmillis = 0;     try {         offsethrsmins = soffset.split(":");         long offsethrs = (long) numberformat.parse(offsethrsmins[0]);         long offsetmins = (long) numberformat.parse(offsethrsmins[1]);         offsetmillis = 1000 * 60 * ((offsethrs * 60) + offsetmins);     } catch (parseexception e) {         // handle exception         return;     }      long ltime = dttime.gettime();      system.out.println("adding minutes: " + dateformat.format(new date(ltime + offsetmillis)));     system.out.println("subtracting minutes: " + dateformat.format(new date(ltime - offsetmillis))); 

output:

adding minutes: 08:07 subtracting minutes: 07:53 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -