Date Format Field Codes vs SimpleDateFormat

I’m getting the following exception during a mailmerge operation.

Caused by: java.lang.IllegalArgumentException: Illegal pattern character ‘Y’
at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:752)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:559)
at java.text.SimpleDateFormat.(SimpleDateFormat.java:484)
at java.text.SimpleDateFormat.(SimpleDateFormat.java:459)
at asposewobfuscated.cp.B(Unknown Source)
at com.aspose.words.g.a(Unknown Source)
at com.aspose.words.kj.a(Unknown Source)
at com.aspose.words.kj.a(Unknown Source)
at com.aspose.words.kj.mu(Unknown Source)
at com.aspose.words.kj.a(Unknown Source)
at com.aspose.words.MailMerge.execute(Unknown Source)
at com.aspose.words.MailMerge.execute(Unknown Source)

This is due to not having a proper conversion between Office Date field formats for Year, and the java class SimpleDateFormat. According to the Office Online documentation, the year field can be either uppercase or lowercase, but SimpleDateFormat does not accept a capital Y as a valid pattern, only lowercase.

Can a check be added during the mailmerge for this conversion?

Thanks!
Rich

Hi
Thanks for your inquiry. I think that you can replace uppercase “YY” with lowercase “yy” before mail merge. For example see the following code:

Document doc = new Document("in.doc");
// Get collection of FieldStrat nodes
NodeCollection fieldStarts = doc.getChildNodes(NodeType.FIELD_START, true);
// Loop through all field starts
FieldStart start;
for (int i = 0; i < fieldStarts.getCount(); i++)
{
    start = (FieldStart)fieldStarts.get(i);
    // Check whether current fieldStrat is start of Mergefield
    if (start.getFieldType() == FieldType.FIELD_MERGE_FIELD)
    {
        // Get field code
        String fieldCode = "";
        Node currentNode = start.getNextSibling();
        while (currentNode.getNodeType() == NodeType.RUN)
        {
            fieldCode += ((Run)currentNode).getText();
            ((Run)currentNode).setText("");
            currentNode = currentNode.getNextSibling();
        }
        // Replace upper case "YY" with lower case "yy"
        fieldCode = fieldCode.replace("YY", "yy");
        // Set field code
        ((Run)start.getNextSibling()).setText(fieldCode);
    }
}
// execute mail merge
String[] names = { "test" };
GregorianCalendar day = new GregorianCalendar();
Object[] values = { day.getTime() };
doc.getMailMerge().execute(names, values);
// Save document
doc.save("out.doc");

I hope this could help you.
Best regards.

Thanks for the quick reply Alexey.

I agree, I can iterate through the document and replace the YY tags on the date formatted merge fields. However, that seems like the most inefficient way to do it. I would need to visit every merge field, then detemine if it’s a date field, and finally checking and modifying the switch as needed.

On the Aspose side, the mailmerge has already made the determination that this is a date mergefield before it ever calls SimpleDateFormat, would it not be trivial to add a upper to lowercase check to modify the switch if required just before constructing the SimpleDateFormat? Doing this during the mailmerge seems like the most effecient (and compatible to word) way.

Thanks!
Rich

Hi
Thank you for your suggestion. I will consult with our developers and provide you more information.
Best regards.

Hi
I created new issue #5843 in our defect database. We will notify you as soon as it is fixed.
Best regards

Hi
We are happy to tell you that we released new version of Aspose.Words for java. This version includes fix of issue you found earlier (issue # 5843). You can download new version from here:
https://releases.aspose.com/words/net
Best regards.