How to print a date in the default JTable.print() header and setup the page

After a struggle, managed to figure out that the MessageFormat is not that complicated at all. All I wanted was to print a heading on each page of the JTable with the current date in it. The heading was to read “Report as on ” + today’s date.

Here is how I accomplished the task. Setup of the Page Settings is show with blue text. The heading is indicated in green text.

public void actionPerformed(ActionEvent ev) {
	try {
		PrintRequestAttributeSet attribSet = new HashPrintRequestAttributeSet();
		attribSet.add(MediaSizeName.ISO_A4);
		attribSet.add(new Copies(1));
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String headerField = "Report as on " + sdf.format(new Date());		//Just use a simple String to add whatever dynamic content is required
		MessageFormat header = null;
		header = new MessageFormat(headerField);	//No need to play with message format fields
		MessageFormat footer = new MessageFormat("Page {0}");
                boolean complete = table.print(JTable.PrintMode.FIT_WIDTH, header, footer, true, attribSet, true, null);
	} catch (PrinterException ex) {
		ex.printStackTrace();
	}
}