Question

Hello I have this excerpt of code:

end = new DateTime(mergeToDateTime(this.endDate, this.empEndTime));

Duration extraTime = new Duration(this.preTime.getTime()); //add the first 30 mins
extraTime = extraTime.plus(new Duration(this.postTime.getTime())); //add the second 30 mins
end = end.plus(extraTime); // extraTime = -3600?

When I look in the debugger my durations are always coming up negative. I have no idea why this is, even though according to the API, it is possible to create a duration out of the a long type, hence the getTime(). (preTime and postTime are java.sql.Time types)

Était-ce utile?

La solution

I guess your instances of java.sql.Time were created in such a way that their millisecond values include timezone offset.

For example, deprecated java.sql.Time(int hour, int minute, int second) constructor takes offset of the current timezone into account:

System.out.println(new Time(1, 0, 0).getTime()); // Prints -7200000 in UTC+3 timezone

It looks like timezone offset is introduced by JDBC driver, and it can be easily compensated by converting java.sql.Time to LocalTime (and vice versa):

LocalTime lt = new LocalTime(time);

Then you can convert LocalTime to duration:

Duration d = new Duration(lt.getMillisOfDay());

Autres conseils

Aren't you starting out wrong when you use an instant in time as duration? The constructor signature you are using is Duration(long duration), not Duration(long startInstant) -- there is no such constructor, in fact.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top