Daniel Morgan
Daniel Morgan

Reputation: 772

How to change the format of datatimepicker result?

I need to enable users to select a date from a calendar and then show the selected date on the console.

Usign the following code I can show a datetimepicker and receive the selected date. When the type of variable is String it shows the following output

2013-03-27T00:00:00+11:00

When the type of variable is Date it shows the following output

null

How can I receive the result in yyyy-mm-dd or dd-mm-yyyy ? so it should be 2013-03-27 or 27-03-2013 I am not interested in using subStr is there any other method?

JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%> 
<%@taglib uri="/struts-dojo-tags" prefix="sx" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <sx:head/>
        <title>JSP Page</title>
    </head>

....
<sx:datetimepicker name="Sdate"
                              label="MyDate"
                              displayFormat="dd-MMM-yyyy"
                              value="todayDate"/>
.....

Class

public class Datepicker{
      private String sdate;
     ... getter and setter go here ...
}

Upvotes: 2

Views: 1008

Answers (1)

NPKR
NPKR

Reputation: 5506

Here is typo problem

You JSP contains sx:datetimepicker name="Sdate" this shold be sx:datetimepicker name="sdate"

but the property in private String sdate;

You can use datatype is Date only in controller like this private Date sdate;

EDIT:

 String str = "2013-03-27T00:00:00+11:00";
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        try {
            SimpleDateFormat parseFormatter = new SimpleDateFormat("yyyy-MM-dd");
            //SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss+hh:mm"); 
            Date date = parseFormatter.parse(str);

            String formattedDate = formatter.format(date);
            System.out.println(formattedDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Upvotes: 2

Related Questions