-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Good day,
I am using MVC. I have a date column which I would like to format in dd/MM/yyyy. Currently the format is like "2013-06-13 21:27:17.0". This is in a grid in a zul page.
<row> <label id="startDate"/>
Hi jalolejalo
You have the same requirement of mine. Here is how i achieved the result in my project.
Zul Code
<template name="model" var="p1">
<listitem
sclass="@load(p1.active eq 0 ?'inactiveRecord':'')">
<listcell label="@load(p1.code)"
onClick="@command('onlinkOpen')" sclass="highlightcell" />
<listcell label="@load(p1.firstName)" />
<listcell label="@load(p1.lastName)" />
<listcell
label="@load(p1.DOB) @converter('com.product.webapp.component.MyDateFormatConverter')" />
<listcell label="@load(p1.gender)" />
<listcell label="@load(p1.mobile)" />
<listcell>
Converter Java Code
package com.product.webapp.component;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;
import org.zkoss.bind.BindContext; import org.zkoss.bind.Converter; import org.zkoss.zk.ui.Component;
public class MyDateFormatConverter implements Converter { /** * Convert Date to String. * * @param val * date to be converted * @param comp * associated component * @param ctx * bind context for associate Binding and extra parameter (e.g. * format) * @return the converted String */
/**
* The method coerceToUi() is invoked when loading ViewModel's property to
* component and its return type should correspond to bound component
* attribute's value[1]. The coerceToBean() is invoked when saving. If you
* only need to one way conversion, you can leave unused method empty.
*/
private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
public Object coerceToUi(Object val, Component comp, BindContext ctx) {
final Date date = (Date) val;
return date == null ? null : sdf.format(date);
}
/**
* Convert String to Date.
*
* @param val
* date in string form
* @param comp
* associated component
* @param ctx
* bind context for associate Binding and extra parameter (e.g.
* format)
* @return the converted Date
*/
public Object coerceToBean(Object val, Component comp, BindContext ctx) {
final String date = (String) val;
sdf.setLenient(false);
try {
return date == null ? null : sdf.parse(date);
} catch (ParseException e) {
comp.invalidate();
return null;
}
}
}
You need to write converter. Look at this example. Even though it is in MVVM, but you can apply the same in MVC also.
I think you can do this:
Date date = new Date(System.currentTimeMillis());
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(df.format(date));
Note that: System.currentTimeMillis()
is a TimeStamp (like that of yours 2013-06-18 00:00:00.0)
But you must create you own DateUtil that manages date issues so when you face problems like this and you get a good solution (from others or by yourself) you can put the code their and improve it.
Hope it helps. Good like.
Asked: 2013-06-15 14:29:19 +0800
Seen: 184 times
Last updated: Jun 19 '13
composite component help [closed]
EL in a forEach @command not working?
Different template for each grid row
Cardlayout animation is broken
Grid RowRender slow using 6.5.1 and sizable=true
setVisible(false), component will load or not
Decimalbox and doublebox value rounded on iPad
history management with page status
thanks for the reply. I have the converter and I can wire components and get a list of items from the database and display them in a list. I just need to format the output of my data column. The default format is e.g "2013-06-18 00:00:00.0". I want to change it to dd/MM/yyyy
jalolejalo ( 2013-06-18 02:09:37 +0800 )edit