Jump to content
We've recently updated our Privacy Statement, available here ×

joda time (api) in ireport


rajuchacha007

Recommended Posts

Hello Experts

 

In order to display milliseconds, I tried to fire SQL query using

TIME_FORMAT(TIMEDIFF(process1.end_datetime, process1.start_datetime),'%H:%i:%S%f') as exec_time. I am sure it's not working. I came to know about joda time. It is possible to display milliseconds using it in java program. I am not sure whether it is applicable in ireport/jasperreport. I would like to know whether we can import joda api in ireport or not? has anyone worked on it in ireport? I would really appreciate if you please let me know about it. Thank you in advance.

 

Best regards,

Link to comment
Share on other sites

  • Replies 13
  • Created
  • Last Reply

Top Posters In This Topic

hi rajuchacha007 :)

I find some indications that you give to us very interesting... so I try to understand how they work reporting to you what I find (to be fair :) )

joda-time is an interesting library...

to use it simply:

--put the joda-time.jar in a directory.
--from Option-->classpath-->add jar: add this jar
--create a simple report
--create a variable (we call it "now") of java.util.Date type...
  set the expression as " new Date() "
--click on the report main node, the in the properties on the "import" field and add: org.joda.time.DateTime
--define a new variable (we cal it "ms") that represent millisecond, of type Long,
  set the expression as: new Long((new DateTime($V{now})).millisOfSecond().getMillis())

now add to the report two textfiled, one for the variable "now" (type Date), now for the variable "ms", type Long.

I use to set a connection to a Orable DB setting the query to "select 1 from dual".

you can try to use a empty data source with 2 or 10 records...
if you use more and more record you can see how the variables show different values on differend rows.

I used a simple test to show the use of joda-time, maybe it'snt the method you want... but, it works, so you can choose the method you prefer to show your data.

give me feedback...
and good work :)

_________________________________________

if you like it... give me KARMA points please!    : ) 
_________________________________________

listening: CSI - Io Sto Bene

Link to comment
Share on other sites

First of all thank you for the present. :D :). It certainly works for the milliseconds. I am looking for difference between two dates including difference in milliseconds. Following is my output at the moment.

 

ID StepName StartTime EndTime RunTime Status

3 Process1 2010-08-09 02:08:00 AM 2010-08-09 02:08:01 AM 00:00:01 Success

4 Process2 2010-08-09 02:08:01 AM 2010-08-09 02:15:00 AM 00:06:59 Success

 

My concern is to display milliseconds difference if any. That is why I tried to use Timedff(above query) etc. It didn't work. I read about joda time and asked about it if it is feasible. I tried to use your solution. I think it is not working. I really appreciate your help/solution. I think i should try while executing the report in java code. What do you recon? I mean there must be some mechanism to import these fields in java code with modifications? Am I thinking rather guessing correct? Thank you very much.

 

Best regards.

Link to comment
Share on other sites

Hi rajuchacha00,

I read only this morning your post (in the weekend I turned off my pc :) ) so excuse me for the delay.
I think you should sometimes look more far than a single example can suggest to you.
So try to read my suggestion:

A time difference is a subtraction between two dates, or similar: a subtraction between two instants.
As you can think, a time istant can be represented as a long value of millisenconds... so a time difference can be viewed as a long values subtraction.
The result of this operation is a number of milliseconds between two instants... and so it's a Period (in the Joda "language").

I made the report I have attached to suggest to you how It can works.

Time_diff(ms) is calculated as the subraction of the two values in the long format  $V{now}  representing the instant when the row is printed and $V{start_time} representing the instant when the report is launched.

Time_diff uses the joda Period to create a time difference and print only the details you prefer (hours, minutes, seconds, milliseconds...)
I used JodaTime as you asked, but I think the report can be done without it, simply using  java Date, long etc.

:)

Stop worrying about the potholes on the road and enjoy the ride. (Arabic proverb)

_________________________________________

if you like it... give me KARMA points please!    : ) 
_________________________________________

listening: Smashing Pumpkins - Cherub Rock

Link to comment
Share on other sites

I got your point. Thank you for the explanation. Just wanted to know, how did you bring start_time (ms)in the report? Did you define a new variable here? I can see the difference between start_time (ms) and now(ms) there. I think I should subtract my report's dates in this way so that I can get every element including milliseconds. i am experimenting for my result set so that there should be less coding in the actual java programming while executing the report. Thanks for the time to time suggestions.
Link to comment
Share on other sites

rajuchacha007
Wrote:

Time_diff uses the joda Period to create a time difference and print only the details you prefer (hours, minutes, seconds, milliseconds...)
I used JodaTime as you asked, but I think the report can be done without it, simply using  java Date, long etc.
teps partially now. thanks :)

will you please guide how does it show in the format like 0hh 0mm 0ss 15ms. Right now i am getting milliseconds difference. Not able to convert them in 0hh 0mm 0ss 15ms in this format. Thank you in advance. I have imported joda class.

 

BEst  regards



Post Edited by rajuchacha007 at 08/27/2010 12:17
Link to comment
Share on other sites

hi rajuchacha007,

as I wrote, "Time_diff uses the joda Period to create a time difference and print only the details you prefer (hours, minutes, seconds, milliseconds...)"

you can read here: http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html
and here: http://joda-time.sourceforge.net/key_period.html
and here: http://stackoverflow.com/questions/1440557/joda-time-period-to-string

about Joda Period...

so, in your report you must define a variable with Joda Period object, (suppose we call it $V{joda_period} and the a textfield with the expression:

$V{joda_period}.getHours()+"hh "+$V{joda_period}+"mm "+$V{joda_period}+"ss "+$V{joda_period}+"ms "

or in a simpler way, a TextField with this expression:

" "+(new Period($V{start_time}.getTime(),$V{now}.getTime())).getHours()+"hh"+
" "+(new Period($V{start_time}.getTime(),$V{now}.getTime())).getMinutes()+"mm"+
" "+(new Period($V{start_time}.getTime(),$V{now}.getTime())).getSeconds()+"ss"+
" "+(new Period($V{start_time}.getTime(),$V{now}.getTime())).getMillis()+"ms"

where $V{start_time} and $V{now} are java.util.Date Object and the getTime() method returns the millisecond representing this date.

(you can use your millisecond if you have it)

I hope this can help you...
Keep in mind that I never used Joda and did not know it before you cite it, and it is really easy to use...

 

_________________________________________

if you like it... give me KARMA points please!    : ) 
_________________________________________

listening: Tiromancino - E' necessario

"E’ necessario che io sia coerente con me stesso
per dare il peso giusto e un senso a tutto il resto
" (t.)

 

Link to comment
Share on other sites

hello. thanks for the reply. I did everything as per steps. I have defined 2 variables for startDate and endDate of the type java.util.Date(). For startDate in the variable expression i am writing $F{starttime} instead of new date() as $F{starttime} is my actual start time from the database.$F{starttime} is of type java.sql.timestamp() though. I have same thing for endDate(java.util.Date)variable expression $F{endtime}(from database of type java.sql.timestamp. Is this the thing where in i am going wrong? I mean while defining variable expression? Also I got here a variable called $V{jodaPeriod}(of type long) and tried both expression with the change as per my names. Instead of now and start_time I am using my corresponding variables. I went through these articles as well. I tried it by changing its expression class (from java.util.Date to java.sql.TimeStamp and vice versa). It is really easy to use but i am bungling some where. Thank you.

 

Best regards.

Link to comment
Share on other sites

you can use your variable names... but don't change the java object...

$V{jodaPeriod} is a Joda Period object... or, if you prefer, you can create a String variable or a String textfield that uses a Period to show data (as I show in the perviuos post)...

if you use long , i think it's impossible show hours, minutes etc etc if you don't create a function that translate it in you format ore use one that do it for you.

you can try to use the java SimpleDateFormat object if you prefer to use the long values but not the Joda Object...

in a TextField of type String you can use:
 

(new SimpleDateFormat("HH 'hh', mm 'mm,' ss 'ss',SS 'ms'")).format(new Date($V{ms}.longValue()-3600000))

 

where $V{ms} is a Long Object and 3600000 (=60min*60sec*1000ms=1hour)  is the amount of millisencond I subract to regain the extra time (1 hour) that the default SimpleDateFormat give to print the 0 GMT time in my Locale.

_________________________________________

if you like it... give me KARMA points please!    : ) 
_________________________________________

listening:  Prodigy- Narayan

 

 

 

Link to comment
Share on other sites

Slow,

This is what I have achieved so far.

Database: I am selecting 2 dates from database. starttime and endtime.

Variables: I have defined following variables.

name                     class                           expression

startdate                java.util.date              $F{starttime}   

endDate                java.util.date              $F{endime}

starttimems         java.lang.long             new Long((new DateTime($V{startDate})).millisOfSecond().getMillis())

endtimems         java.lang.long              new Long((new DateTime($V{endDate})).millisOfSecond().getMillis())

joda_period        java.lang.Object         

I took a textfield(java.util.String) and pasted following expression

Now taking a textfield and putting expression ($V{endtimems}-$V{starttimems}) i am getting milliseconds. I am attaching the report.

Apart from those I tried,

($V{endtimems}-$V{starttimems}).getHours()+"hh "+($V{endtimems}-$V{starttimems}).getMinutes()+"mm "+($V{endtimems}-$V{starttimems}).getSeconds()+"ss "+($V{endtimems}-$V{starttimems}).getMillis()+"ms "

 

I also tried,

" "+(new Period($V{starttimems}.getTime(),$V{endtimems}.getTime())).getHours()+"hh"+
" "+(new Period($V{
starttimems}.getTime(),$V{endtimems}.getTime())).getMinutes()+"mm"+
" "+(new Period($V{
starttimems}.getTime(),$V{endtimems}.getTime())).getSeconds()+"ss"+
" "+(new Period($V{
starttimems}.getTime(),$V{endtimems}.getTime())).getMillis()+"ms"

I also tried

$V{joda_period}.getHours()+"hh "+$V{joda_period}+"mm "+$V{joda_period}+"ss "+$V{joda_period}+"ms ".

Now I tried

(new SimpleDateFormat("HH 'hh', mm 'mm,' ss 'ss',SS 'ms'")).format(new Date(($V{endtimems}-$V{starttimems}).longValue()-3600000)) without using joda. I am getting following result. I am attaching 2 pdf files here.

Also, how to view milliseconds  in mysql. I have been trying to get milliseconds column but not successful yet. Not sure how you did print start_time: 23/08/2010 10:57:00:592. Please suggest. I know you have used oracle DB. I am afraid that i am going at wrong direction as i am using mysql. While searcing I found that MySQL doesn't support milliseconds in any of the time / date columns, or return it in NOW().

Thank you for your help.

Best regards.


 




Post Edited by rajuchacha007 at 08/31/2010 09:32
Link to comment
Share on other sites

rajuchacha007, I spended more and more words to suggest to you how use the Joda library , or Java Object to show or manipulate milliseconds...

but in the late post you wrote i read this sentence:
"Also, how to view milliseconds  in mysql. I have been trying to get milliseconds column but not successful yet."

when I show millisecond in my example, I obtained them from java object (Date) or Oracle columns...
I haven't a MySql installation on my work pc.

but... you can try more and more times... you'll never obtain milliseconds if you start from date coming from mysql DB.
why this?

MySql time object (timestamp or date) doesn't support millisecond... so each date(or time) has details only from year to seconds.
read here:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-types.html

http://bugs.mysql.com/bug.php?id=8523

http://dev.mysql.com/doc/refman/5.1/en/datetime.html

"However, microseconds cannot be stored into a column of any temporal data type. Any microseconds part is discarded."

As you can see... milliseconds aren't supported by MySql... so, your dates (I wonder how you have not noticed it before) have exactly 0 milliseconds each time you print them in the pdf files in the long format...

So... you can try to store in a different column in your mysql db the milliseconds you want...or save it in the long format or in the string format...but if you have date and time already stored in mysql DB, I think there ins'nt a way to obtain milliseconds from your mysql data object.

(I did a quick check ... and only the last post you mentioned using mysql!!!)

_________________________________________

if you like it... give me KARMA points please!    : ) 
_________________________________________

listening: MUSICA NUDA - Sacrifice



Post Edited by slow at 08/31/2010 09:52
Link to comment
Share on other sites

I noticed this. I kept on searching to find a way to resolve (I thought i am doing something wrong in sql syntax). I should have pointed out it earlier though. I was working on some other issues as well. Got your point. Sorry for this long post. It's been to long. Thank you very much for the help.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...