Link to home
Start Free TrialLog in
Avatar of javabeginr
javabeginr

asked on

date in this format mm/dd/yyyy

I'm trying to display the current date in this format mm/dd/yyyy in my JSP.  I'm using this <%= new java.util.Date() %> but this gives the date, time, and it's not in the right format.  Can somebody help?
Avatar of lhankins
lhankins
Flag of United States of America image

use SimpleDateFormat :

      DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
      Date someDate = new Date();

      System.out.println(formatter.format(someDate));
Avatar of javabeginr
javabeginr

ASKER

Do I need to import something because I get a JasperException?
For the example I gave above :

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
I'm still getting the same error.  I've imported this

<%@page import "java.text.DateFormat"%>
<%@page import "java.text.SimpleDateFormat"%>
<%@page import "java.util.Date"%>

and have this to display the date in my jsp.
<%
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date someDate = new Date();
System.out.println(formatter.format(Date));
%>

Seems to me to display today's date wouldn't need this much code but what do I know.


System.out.println(new java.text.SimpleDateFormat("MM/dd/yyyy").format(new Date()));
ASKER CERTIFIED SOLUTION
Avatar of koppcha
koppcha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bloodredsun
koppcha's answer is the correct one, and all in one line, very nice although you do need to import one package as shown in the following example.
-------
<%@ page language="java" import="java.util.Date" %>
<html>
<head><title>formatted date example</title></head>
<body>
<%= new java.text.SimpleDateFormat("MM/dd/yyyy").format(new Date())%>
</body>
</html>
---------

Another point is that all the previous posts were writing to to the System output rather than ServletOutput
e.g.
System.out.println(formatter.format(Date)); - which would normally write to the tomcat logs not page.
out.println(formatter.format(Date)); - writes to OutputStream
koppcha, sorry mate, my screen cutoff the fact that you'd already imported the utils package in your example.
Ignore the first part of my post please.
Hi bloodredsun,
  Do not be sorry for small things.I have no problem with that.Infact i was inspired by you guys only to work in java (bloodredsun,TimYates,objects,CHEJ,zzynx..many others) :)
thank you koppcha, you're a gentleman :-)
Can you use the JSTL taglibs? If so how about the formatDate tag:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<html>
<body>
    <jsp:useBean id="now" class="java.util.Date" />
    Date: <fmt:formatDate value="${now}" pattern="MM/dd/yyyy" />
</body>
</html>

Regards,
Jim Cakalic
hi javabeginer may this help u

<%@ page import ="java.util.Date" %>
<%@ page import ="java.util.Calendar" %>
<%@ page import ="java.sql.*" %>
Calendar cal= Calendar.getInstance();
 Date date=new  Date();
String months[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
 out.println(months[cal.get(Calendar.MONTH)] +"/"+cal.get(Calendar.DATE)+"/"+cal.get(Calendar.YEAR));

it will print jan/21/2005 for "21/01/2005"
Grade B: The answer i provided is not the exact one  you are looking for ?