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?
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;
import java.text.DateFormat;
import java.text.SimpleDateFormat
import java.util.Date;
ASKER
I'm still getting the same error. I've imported this
<%@page import "java.text.DateFormat"%>
<%@page import "java.text.SimpleDateForma t"%>
<%@page import "java.util.Date"%>
and have this to display the date in my jsp.
<%
DateFormat formatter = new SimpleDateFormat("MM/dd/yy yy");
Date someDate = new Date();
System.out.println(formatt er.format( Date));
%>
Seems to me to display today's date wouldn't need this much code but what do I know.
<%@page import "java.text.DateFormat"%>
<%@page import "java.text.SimpleDateForma
<%@page import "java.util.Date"%>
and have this to display the date in my jsp.
<%
DateFormat formatter = new SimpleDateFormat("MM/dd/yy
Date someDate = new Date();
System.out.println(formatt
%>
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/yy yy").forma t(new Date()));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/yy yy").forma t(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(formatt er.format( Date)); - which would normally write to the tomcat logs not page.
out.println(formatter.form at(Date)); - writes to OutputStream
-------
<%@ page language="java" import="java.util.Date" %>
<html>
<head><title>formatted date example</title></head>
<body>
<%= new java.text.SimpleDateFormat
</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(formatt
out.println(formatter.form
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.
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,obje cts,CHEJ,z zynx..many others) :)
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,obje
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
<%@ 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","Au g","Sep"," Oct","Nov" ,"Dec"};
out.println(months[cal.get (Calendar. MONTH)] +"/"+cal.get(Calendar.DATE )+"/"+cal. get(Calend ar.YEAR));
it will print jan/21/2005 for "21/01/2005"
<%@ 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","
out.println(months[cal.get
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 ?
DateFormat formatter = new SimpleDateFormat("MM/dd/yy
Date someDate = new Date();
System.out.println(formatt