CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2002
    Posts
    65

    How to display the current date in JSP (dd/MM/yy format)

    hi. On my JSP web page I want to display the current date in the SHORT format,i.e, dd/MM/yy. Presently, I am using the line of code below:

    <%= new java.util.Date() %>

    This displays the date in the foramt below:
    Mon Dec 30 14:00 GMT 2002

    How do I display the date in the format 31/12/02? Plz help me. I also need to use this format to insert the current date in my database. So this is very important.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    Use java.text.SimpleDateFormat.

    but the rope was gone, and she was lost in the whirling nightmare of the blizzard...
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Dec 2002
    Posts
    65
    Hi again. Thanks for the current date format. But now i have another problem. In the mysql database that I'm using the default format is yyyy-mm-dd and I have tried very hard to change it but has been unsuccessful. Do you of any way to change the default format to dd/mm/yy? I need this to insert dates in my database. Plz help.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    I don't understand. If the default database format is YYYY-MM-DD, then surely that is what you need to insert dates?

    But whatever format you need, SimpleDateFormat can provide it. If you need dd/mm/yy format, use SimpleDateFormat to obtain it. Exactly what is the problem?


    he could feel the saliva on his tongue boiling away in the hard vacuum...
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Dec 2002
    Posts
    65
    Could you plz tell how exactly to use the java.text.SimpleDateFormat? I wrote

    <%=new java.text.SimpleDateFormat() %>
    and I got no date dispalyed on the page. What appeared is somthing like that:
    java.text.SimpleDateFormat@bnb788

    Could you plz tell plz how to specify the dd/MM/yy format?

    As for mysql database, I want to change the date format to dd/MM/yy rather than using yyyy/MM/dd as is the default presently. I do not know how.
    If java.text.SimpleDateFormat could be used to insert dates in the format yyyy/MM/dd, plz could you tell me how?

    Thanks in advance.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    DateFormat contains the basics. The SimpleDateFormat link I posted gives the details on specifying a format pattern (you did read the JavaDocs, didn't you?).

    To get today's date in 'dd/MM/yy' format:
    Code:
    DateFormat df = new SimpleDateFormat("dd/MM/yy");
    String formattedDate = df.format(new Date());
    I don't know how to change your database defaults - check the docs that came with it. Your database default format seems to have changed - in your second post it was 'yyyy-mm-dd' but now it's 'yyyy/MM/dd'!

    Whatever, to get date formatted as 'yyyy/MM/dd':
    Code:
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    String formattedDate = df.format(theDate);
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Dec 2002
    Posts
    65
    Hi. Thanks for the lines of codes. Now my curent date is working? But I have another problem. In my database, I have a field of type float called Delay (Representing Number of days) and another field DatePosted of type DATE & in my jsp page I have a where clause
    WHERE DatePosted + Delay + 1 > CURRENT_DATE

    however, the DatePosted + Delay + 1 is not working properly since I suppose Delay is of type float, if DatePosted is 30/12/2002 and Delay is 3, I get 34/12/2002 instead of 03/01/2003. It is not being converted to Date type. can you help me. Do you know what should be the type of Delay? It should be able to take decimals( e.g, 0.5 day)

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    Sorry, I'm not well up with SQL date calculations. It does seem to have added the days to the date correctly, but it hasn't rolled the month over... The solution may depend on your database. Some databases have a 'DATEADD(...)' function (e.g. Oracle, which supports adding integer or decimal values for number of days, and returns a DATE).

    You might do better in a SQL or database-specific forum.

    they begged him not to open the door, but he was beyond reason...
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured