0

I would have thought this would be a common problem, but I haven't been able to find anything about it...

Basically, I have a jquery datepicker linked up to an input field which paths to a java Date object. Everything works just fine, but the datepicker initially displays a String like this:

Thu Sep 01 00:00:00 MDT 2011

As soon as I pick a date it formats it to the default, is there a way to make it format before the initial display?

genesis
  • 50,477
  • 20
  • 96
  • 125
Mark
  • 53
  • 9

1 Answers1

1

I handle this a couple ways depending on the framework if any you are using.

  1. Use a static method call to format the date to how you need it displayed using a simple date format method to return a string value

  2. Add an additional get method to your object (ie getDateFormatted) that simply does the same function as number 1 from the "host" object.

Again it depends on the framework, if any, that you are using on how to implement the tag structure on the page.


UPDATE Based on JSTL Solution

JSP

<input type="text" class="datepicker" name="date" value="${bean.getDateFormatted()}" />

JAVA BEAN This will have an additional getter method for returning a formatted date string instead of the java date object. You are essentially getting the toString() version on your JSP anyway.

public String getDateFormatted() {
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    return df.format(this.date);
}

Here is some additional information on how to set up date format options using Java:

Class SimpleDateFormat

Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29
  • Thanks for the response! I'm not sure I understand your answer though, it looks to me like that's how one would display a static date, whereas my question is about a datepicker input field... Could you explain a little more? I feel like there should be a way to have the datepicker format the value without going back into my Java program, since I know it can format selected dates however one likes We're using Spring with jsp, if it makes any difference! – Mark Sep 21 '11 at 15:56
  • Are you using Struts or JSTL? How are your values being retrieved from your Java class? – Russell Shingleton Sep 21 '11 at 15:58
  • Well, you can always parse it out using jQuery, http://docs.jquery.com/UI/Datepicker/parseDate – Russell Shingleton Sep 21 '11 at 16:07
  • Oops, forgot about this! We're using JSTL, and (forgive me if my answers are a little vague, I'm relatively new to web applications) the values are being retrieved from bean objects with getter/setter methods using a "path" value in the input field linked to the timepicker. And I'll take a look at this link you gave me. I think a big part of my problem is not really understanding how to use jQuery commands very well... – Mark Sep 21 '11 at 16:52
  • It also looks like I was making an incorrect assumption. For some reason I was under the impression that the datepicker understood the Date it was receiving, but was simple not formatting it right. Your suggestion to parse it makes makes me realize that it totally doesn't understand the given date, which makes more sense. – Mark Sep 21 '11 at 17:08
  • If I were you I would just use the JSTL to call a simple date format method from your bean. Create a method in your bean class to format the date how you want and return it. I'll update my answer shortly. – Russell Shingleton Sep 21 '11 at 17:18