0

format date label

asked 2013-06-15 14:29:19 +0800

jalolejalo gravatar image jalolejalo
2 1

updated 2013-06-19 13:44:55 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

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"/>
delete flag offensive retag edit

Comments

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

3 Answers

Sort by ยป oldest newest most voted
0

answered 2013-06-19 10:30:34 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

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;

    }
}

}

link publish delete flag offensive edit
0

answered 2013-06-15 15:13:42 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

You need to write converter. Look at this example. Even though it is in MVVM, but you can apply the same in MVC also.

link publish delete flag offensive edit
0

answered 2013-06-18 14:16:30 +0800

eudsonbambo gravatar image eudsonbambo flag of Mozambique
30 2
http://eudsonbambo.blogsp...

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.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-06-15 14:29:19 +0800

Seen: 184 times

Last updated: Jun 19 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More