0

I'm trying to populate a TextField in JasperReport with the duration of some kind. This field's value is in miliseconds and to make it readable to user, I used Java's Date class ans SimpleDateFormat class to convert it to hh:mm:ss format like this:

new java.text.SimpleDateFormat("hh:mm:ss").format(new Date($F{milisec}.longValue()))

The $F{milisec} is Double so I had to convert it to long. Anyway, The problem is that the output of this expression has my local timezone added to it. So if the field's value is 10000.0, then the output would be 01:30:10 (assuming my machine's TimeZone is set to +1:30). I searched how to set the TimeZone, and I found this post:

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");

The thing is that I'm going to use my code as a JasperReport expression and as far as I know, I'm not allowed to use multiple lines in there. So how can I set the TimeZone of a date in java all in one line so it is considered one expression?

Community
  • 1
  • 1
Mehran
  • 15,593
  • 27
  • 122
  • 221
  • 2
    This isn't what a `Date` is designed for. What you have done is created a timestamp that is X mills after the epoch. This is **not** a duration. Surprisingly enough, until Java 8 comes out, Java has no way to represent a duration. Let alone to format one. You will either need to do this manually or use a library (like the excellent [Joda-Time](http://www.joda.org/joda-time/)). – Boris the Spider Mar 18 '14 at 08:00
  • Putting aside the TimeZone problem, the rest is working perfectly. Are you saying that what I want can not be done in one line? – Mehran Mar 18 '14 at 08:04
  • Yes. This will not work as you are printing out the time of a timestamp **not** a duration expressed in hours and minutes. You are actually calculating the a timestamp that is some millis after Thursday, 1 January 1970 at 00:00:00.000; this means that if your duration is more than 24 hours you will get 2 January 1970 at some point - your time will be "reset". This will not work. – Boris the Spider Mar 18 '14 at 08:12
  • I'm fully aware of that, and it's not a problem in my case. It would be great if you guide me how to get what I want using the named classes. Thanks. – Mehran Mar 18 '14 at 08:14
  • Or if you are to advise some new class, I need it to be included in Java by default. I'm limited to use no external libraries. – Mehran Mar 18 '14 at 08:16
  • I'm saying that Java has **no support** of durations of any kind (beside time, which is a duration since the epoch). You cannot do what you want using the JDK classes. If you cannot use external resources you will have to implement it yourself. – Boris the Spider Mar 18 '14 at 08:20
  • I know that Java does not support duration, but the solution given here solves my problem perfectly if I can set the TimeZone to GMT. My case is guaranteed never to exceed 24 hours so if I can set the TimeZone in the same line as the rest of code, I'm all done. – Mehran Mar 18 '14 at 08:23
  • In addition, even my own code is considered an external library. – Mehran Mar 18 '14 at 08:25

1 Answers1

3

You can make use of the REPORT_FORMAT_FACTORY builtin parameter, which would give you a default implementation of net.sf.jasperreports.engine.util.FormatFactory.

With that you can do something like $P{REPORT_FORMAT_FACTORY}.createDateFormat("hh:mm:ss", $P{REPORT_LOCALE}, TimeZone.getTimeZone("GMT")).format(new Date($F{milisec}.longValue()))

You could also store the $P{REPORT_FORMAT_FACTORY}.createDateFormat(..) result in a parameter or variable so that you don't create a new object on each record.

dada67
  • 4,723
  • 17
  • 19