1

I am struggling with a date calculation to define my own range for a bar chart series.

My dynamic dates are parameters the timestamp parameters "timestamp1", "timestamp2", "timestamp3", "timestamp4" wheras I want to create the intervals

interval1: days between "timestamp1" and "timestamp2"

interval2: days between "timestamp3" and "timestamp4".

What is the expression that I have to add to my variables in order to have two time intervals for my bar chart series? I am completely lost with this issue.

Alex K
  • 22,315
  • 19
  • 108
  • 236
  • How are the dates currently represented in your program? Also, do you care about time of day as well, or just the date without the time? – user1445967 Dec 27 '13 at 22:30
  • This looks like a duplicate of http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances to me. The best answer to that question is Michal's answer, near the bottom, because it takes account of daylight savings time correctly. – Dawood ibn Kareem Dec 27 '13 at 22:31
  • I am going to downvote any answer that doesn't deal with daylight savings correctly, by the way. Just subtracting the milliseconds and dividing by 86400000 would fit into that category. – Dawood ibn Kareem Dec 27 '13 at 22:31
  • In which format your timestamp? I mean you should have date in string like "dd-mm-yyyy" or as well? – user1791574 Dec 28 '13 at 11:26
  • This is a related [answer](http://stackoverflow.com/a/37579395/5292302) – Petter Friberg Jun 01 '16 at 22:07

2 Answers2

1

As commented by David Wallace, you should look at the question Calculating the Difference Between Two Java Date Instances. And search for other questions with "joda" and "interval".

Joda-Time 2.3 is an open-source library for handling date-time calculations. Joda-Time has inspired the creation of the java.time.* classes being built into Java 8.

As discussed in the doc, Joda-Time offers the Interval class for exactly your needs: Handling a pair of start-stop dates in an intelligent manner, savvy with time zones and Daylight Savings Time (DST) and other anomalies.

DateTime now = DateTime.now( DateTimeZone.forID( "Europe/Paris" ) );  // Current moment in Paris.
Interval interval = new Interval( now, now.plusHours( 8 ) );

To describe the time span (elapsed), see the Period and Duration classes. They can describe the years-months-days-hours-minutes-seconds in human language (French, English, etc.). They can describe the span in ISO 8601 format of PnYnMnDTnHnMnS.


Further tips…

For representing a single moment in time, use the DateTime class.

When necessary to interact with other classes that know only java.util.Date, call the toDate method.

Note that you should avoid using the java.util.Date/Calendar classes, as they are badly designed and implemented. Java 8 will supplant them with the java.time.* classes mentioned above.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I solved the problem with a complete different approach. So if anyone may want to add time intervals into an iReprt chart, consider the following:

I summed up the download numbers from each time interval in my SQL query. Like that:

SELECT  
SUM(CASE WHEN Downloads.ExtractDate BETWEEN "$P{myTimestamp1}" AND "$P{myTimestamp2}" THEN Downloads.Downloads ELSE 0 END) AS Sum1,
SUM(CASE WHEN Downloads.ExtractDate BETWEEN "$P{myTimestamp2}" AND "$P{myTimestamp3}" THEN Downloads.Downloads ELSE 0 END) As Sum
FROM aacdb.Downloads

In iReport, I created a bar chart using the following Chart Series Information (right click on chart -> Chart Data -> Details):

SERIES 1

Series Expression

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp1})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2}))

Category Expression

$F{Name}

Value Expression

$F{Sum1}

SERIES 2

Series Expression

(new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp2})) + " - " + (new SimpleDateFormat("dd.MM.yy").format($P{myTimestamp3}))

Category Expression

$F{Name}

Value Expression

$F{Sum2}

Thanks to everyone for the effort