I use JodaTime library for time operations.
I have two dates:
Date One:
DateTime time_server = new DateTime(server_time_milisecs).
withZone(DateTimeZone.forID("Europe/Zurich")); //+0100
Shows: 2013-01-27 13:44:42
Date two:
DateTime time_local = new DateTime(DateTime.now()).
withZone(DateTimeZone.getDefault()); //Have "Europe/Moscow" timezone +0400
Shows: 2013-01-27 16:41:47
I need to find real interval including timezone
Interval interval = new Interval(time_local, time_server);
Long.toString(interval.toDurationMillis()));
Result: 174040 miliseconds – NOT CORRECT
int secs = Seconds.secondsBetween(time_local,time_server).getSeconds();
Result: 174 secs NOT CORRECT
Period period = new Period(time_local, time_server);
Integer.toString(period.getSeconds()) //**Wrong too**
Result must be: 10974 secs
You should spend some time (no pun intended) to grasp the Jodatime concepts (eg) if you need to do time calculations involving different timezones.
For example, consider the following code
This outputs (for me, 4 hours offset from Zurich) :
Pause and try to guess the output of the following lines:
The above prints
0(zero). As it should.Because
nowHereandnowZurrepresent the same instant of time (in the physical line time), as represented in two different countries. They differ only in how they are represented, but physically they are the same time point (like2.54 cmand1 inare the same length, represented in two different forms).Likewise,
2013-01-27T10:19:24.460-03:00and2013-01-27T14:19:24.460+01:00are the same instant, the moment in which I run that code, only represented according the conventions of different countries; a martian could represent that same instant with his own martian calendar, and it would still be the same instant.As those DateTimes represent the same time point, the physical interval (duration) between them must be zero. Now, if you want to get the “civil time difference” between the two, that’s a wholly different thing. The
LocalDateTimeand thePeriod(totally different fromDateTimeandDuration) would then be the right concepts. For example:This prints 14400 for me (4 “standard” -not physical- hours)