java - mystery on timstamp getTime diff -
here code:
system.out.println(" serverstarttime:" + serverstarttime + " serverrunningtime:" + serverrunningtime); timestamp stime = converttimeformat(serverstarttime, "mmm d, yyyy h:mm:ss z"); timestamp etime = converttimeformat(serverrunningtime, "mmm d, yyyy h:mm:ss z"); long diff = (etime.gettime() - stime.gettime()); system.out.println("etime.gettime():" + etime.gettime() + " stime.gettime():" + stime.gettime() + " time diff:" + diff);
the converttime looks like:
private timestamp converttimeformat(string msgdate, string srcdateformat) { simpledateformat sdf = new simpledateformat(srcdateformat); simpledateformat output = new simpledateformat("yyyy-mm-dd hh:mm:ss"); try { date d = sdf.parse(msgdate); string formattedtime = output.format(d); return timestamp.valueof(formattedtime); } catch (parseexception e) { e.printstacktrace(); } return null; }
the output is:
serverstarttime:sep 22, 2016 12:42:56 pdt serverrunningtime:sep 22, 2016 1:33:42 pdt etime.gettime():1474533222000 stime.gettime():1474573376000 time diff:-40154000
notice running time value smaller start time. why?
you used h
in time format - that's 24-hour time, "start" time after midday, more 10 hours later end time. yes, you've got "am" in format string well, believe that's being ignored due use of h
. use h
instead. always, check format strings carefully.
timestamp stime = converttimeformat(serverstarttime, "mmm d, yyyy h:mm:ss z"); timestamp etime = converttimeformat(serverrunningtime, "mmm d, yyyy h:mm:ss z");
note way of working out problem have been print intermediate values in yyyy-mm-dd hh:mm:ss
format. suspect you'd have seen like:
2016-09-22 12:42:56 2016-09-22 01:33:42
it's not clear whether that's you'd have , haven't set time zone in output format, problem in itself. avoid string round-trip if possible.
Comments
Post a Comment