java - SimpleDateFormat Comparison Issue -


i'd compare 2 dates in string format , return result in hh:mm:ss:sss string. when try running following startdate of 15 jul 2013 17:08:34.903 , enddate of 15 jul 2013 17:08:51.247 i'd expect see result of 00:00:16.344. instead i'm getting diff of 01:00:16.344. ideas why happening??

private static final simpledateformat date_format = new simpledateformat("d mmm yyyy hh:mm:ss.sss");     private static final simpledateformat date_format_hh_mm_ss = new simpledateformat("hh:mm:ss.sss");      public string calculateduration(string startdatestr, string enddatestr){          string methodname = "calculateduration";         try {                        date startdate = date_format.parse(startdatestr);             date enddate = date_format.parse(enddatestr);                       long diff = enddate.gettime() - startdate.gettime();                     return date_format_hh_mm_ss.format(diff);   

the jdk date api horrible. recommend using joda time library if must use jdk date api try code:

private static final simpledateformat date_format = new simpledateformat(         "d mmm yyyy hh:mm:ss.sss");  public string calculateduration(string startdatestr, string enddatestr)         throws parseexception {      string methodname = "calculateduration";     date startdate = date_format.parse(startdatestr);     date enddate = date_format.parse(enddatestr);      // in milliseconds     long diff = enddate.gettime() - startdate.gettime();      long diffmiliseconds = diff % 1000;     long diffseconds = diff / 1000 % 60;     long diffminutes = diff / (60 * 1000) % 60;     long diffhours = diff / (60 * 60 * 1000) % 24;      return string.format("%02d:%02d:%02d.%02d", diffhours, diffminutes,             diffseconds, diffmiliseconds); } 

Comments