eclipse - How to convert two different strings to date format and finding their differences in JSP/JAVA -
i have 2 strings str1: 2016-7-25.15.38. 32. 0 (this im getting db) string2 : july 25, 2016 3:19 pm (this 1 wrote program read email timestamp)
how convert these 2 strings date format , find differnce in time.. pls help. ive gone through many pages in , google not getting specific
how convert these 2 strings date format
using java 8, you'd parse them this:
zoneid zoneid = zoneid.systemdefault(); zoneddatetime dt1 = zoneddatetime.parse("2016-7-25.15.38. 32. 0", datetimeformatter.ofpattern("uuuu-m-d.h.m. s. 0").withzone(zoneid)); zoneddatetime dt2 = zoneddatetime.parse("july 25, 2016 3:19 pm", datetimeformatter.ofpattern("mmmm d, uuuu h:mm a").withzone(zoneid));
using non-standard indentation align format string value parsed, easier comparison.
the code using zoneddatetime
daylight savings time handled correctly.
and find difference in time
getting difference in time easy enough, using until()
or between()
:
long seconds = dt1.until(dt2, chronounit.seconds); long seconds = chronounit.seconds.between(dt1, dt2);
they same. use whichever best.
if print values above, (i'm in usa eastern time zone):
2016-07-25t15:38:32-04:00[america/new_york] 2016-07-25t15:19-04:00[america/new_york] -1172
Comments
Post a Comment