vineri, 13 noiembrie 2015

[OmniFaces utilities 2.0] Format the given date in the given pattern with system default/provided timezone


[OmniFaces utilities] The formatDate() function formats the given date in the given pattern with system default/given timezone. This is useful when you want to format dates in for example the title attribute of an UI component, or the itemLabel attribute of select item, or wherever you can't use the <f:convertDateTime> tag. The format locale will be set to the one as obtained by Faces#getLocale().

Function:
Usage:

The of:formatDate() uses the default timezone (TimeZone.getDefault()). Below, you can see several examples:

@Named
@RequestScoped
public class DateBean {
   
 private Date date = new Date();
   
 // getters & setters
}

dd-MM-yyyy / #{of:formatDate(dateBean.date, "dd-MM-yyyy")}
dd/MM/yy / #{of:formatDate(dateBean.date, "dd/MM/yy")}
dd-MM-yy:HH:mm:SS / #{of:formatDate(dateBean.date, "dd-MM-yy:HH:mm:SS")}
dd-MM-yy:HH:mm:SS Z / #{of:formatDate(dateBean.date, "dd-MM-yy:HH:mm:SS Z")}

Output:

dd-MM-yyyy / 14-11-2015
dd/MM/yy / 14/11/15
dd-MM-yy:HH:mm:SS / 14-11-15:08:12:604
dd-MM-yy:HH:mm:SS Z / 14-11-15:08:12:604 +0200

If you need to indicate the timezone also, then use of:formatDateWithTimezone(). Here it is two examples:

@Named
@RequestScoped
public class DateBean {
   
 private Date date = new Date();
 private TimeZone tm = TimeZone.getTimeZone("America/Los_Angeles");

 // getters & setters
}

yyyy-MM-dd HH:mm:ss, GMT / #{of:formatDateWithTimezone(dateBean.date, 'yyyy-MM-dd HH:mm:ss', 'GMT')} <br/>      
yyyy-MM-dd HH:mm:ss, America/Los_Angeles / #{of:formatDateWithTimezone(dateBean.date, 'yyyy-MM-dd HH:mm:ss', dateBean.tm)}

Output:

yyyy-MM-dd HH:mm:ss, GMT / 2015-11-14 06:14:29
yyyy-MM-dd HH:mm:ss, America/Los_Angeles / 2015-11-13 22:14:29

Niciun comentariu:

Trimiteți un comentariu