Mail merge does not work for date switches using Java

Hi there

I met a problem with formatting mail merged Date field. In Java, I have formatted the java.Util.Date field as String:

private String formatDate(Date date){
DateFormat df = new SimpleDateFormat(“dd MMMM yyyy”);
return df.format(date);
}

Suppose the fieldName is My Field, in Word template I have inserted the following merge field:

{MERGEFIELD “My Field” @ “d MMMM yyyy”}

The problem is no matter which format I set with @ switch in the template, it doesn’t take any effect, I always got the format I set in the Java code i.e. "dd MMMM yyyy"

Could you please tell me why?

Thank you very much!!!

Hi

After read the following post

Mail merge generates incorrect output for date field using Java - Free Support Forum - aspose.com

I upgraded my Aspose.Words for Java to 10.5.0. I still can’t make @ switch work if the value of the Date field has been pre-converted to String.

I tried not to pre-convert the Date field, just pass a java.util.Date object as the field value, then the @ formatting works. However the value of Date object I returned is 2012-01-01 however it was generated as 31 December 2011. Do you know if there are any settings for timezone or something ?

Thanks a lot

Hi Gregory,

Thanks for your query. Please use the latest version of Aspose.Words for Java 11.1.0. I have used the following code snippet and have not found any issue. Please let us know, If you have any more queries.

Document doc = new Document("D:\\in.docx");

String[] names = { "datum" };

Object[] obj = { formatDate(new java.util.Date()) };

doc.getMailMerge().execute(names, obj);

doc.save("D:\\out.doc");