1

Ok, first to say that I've been searching few days on how to resolve this problem and I've tried million ways but I think that neither of that working for me, or I'm missing something.

I have a db table with a column type date. I have model class with a field Date.

public class Pacijent {
    //..
    private Date datum;
    //getters and setters 
}

And a Data access object for retrieving and storing into a model class like this:

ResultSet rs = ps.executeQuery();
while(rs.next()) {
    Pacijent pacijent = new Pacijent();
    //..
    pacijent.setDatum(rs.getDate("datum"));
    //..
    pacijents.add(pacijent);
}

Next I set set attribute in controller and retrieve it in jsp page like ${param.paramName}

The problem is that it outputs in yyyy-MM-dd and I want it to show in dd-MM-yyyy. Can you please guide me how do I format that in a right way?

2 Answers 2

2

The JSTL fmt library has a formatDate tag for just this purpose. To use it, first put this directive in the <head> element of your JSP:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

Then in the body of the page you can write something like

<fmt:formatDate value="${date}" pattern="dd-MM-yyyy"/>

The doc, such as it is, for fmt:formatDate is here. You may also need the info here in order to figure out how to construct an appropriate format pattern.

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe it was so easy. Thank you!
1

This may not be optimal, but one option would be to expose a string getter in your Pacijent class which uses a SimpleDateFormat to generate the date string in the format you want for your presentation:

class Pacijent {
    // other content

    public String getDateFormatted() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
        String date = sdf.format(datum);
        return date;
    }
}

Then, access this getter from your JSP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.