zkh152 / xdocreport

Automatically exported from code.google.com/p/xdocreport
0 stars 0 forks source link

number to date( docx velocity) #153

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
hello, 

I don't know if it is possible but i have some number in type long in java, is 
it possible to insert them in my docx template and in the same time changing 
them in a date format like 12/06/2012 ?

thanks

Gaspard GUERAND

Original issue reported on code.google.com by gaspardg...@gmail.com on 21 Sep 2012 at 1:59

GoogleCodeExporter commented 9 years ago
Hi Gaspard,

If I understand you have a long value that you put in the context like this:

------------------------------------------
long longDate = ...
context.put("myDate", longDate);
------------------------------------------

And you wish write a mergefield in your docx : 

------------------------------------------
My date : $myDate
------------------------------------------

If it's that, you can create a Java class DateHelper :

------------------------------------------
public class DateHelper {
   public static final DateHelper INSTANCE= new DateHelper ();
   public String format(long longDate) {
    // format long as date
   }
}
------------------------------------------

put an helper class in the context DateHelper

------------------------------------------
context.put("d", DateHelper.INSTANCE);
------------------------------------------

And after you can use it in your docx like this : 

------------------------------------------
My date : $d.format($myDate)
------------------------------------------

But I suggest you to read Velocity documentation to use static method (never 
used).

Hope it will help you.

Regards Angelo

Original comment by angelo.z...@gmail.com on 21 Sep 2012 at 9:17

GoogleCodeExporter commented 9 years ago
thanks a lot. 

it works fine. 

Regards, 

Gaspard

Original comment by gaspardg...@gmail.com on 24 Sep 2012 at 8:50