[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1596
  • Last Modified:

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?
0
javabeginr
Asked:
javabeginr
  • 4
  • 3
  • 2
  • +3
1 Solution
 
lhankinsCommented:
use SimpleDateFormat :

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

      System.out.println(formatter.format(someDate));
0
 
javabeginrAuthor Commented:
Do I need to import something because I get a JasperException?
0
 
lhankinsCommented:
For the example I gave above :

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
javabeginrAuthor Commented:
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.


0
 
koppchaCommented:
System.out.println(new java.text.SimpleDateFormat("MM/dd/yyyy").format(new Date()));
0
 
koppchaCommented:
This is print in your JSP page
<%@ page language="java" import="java.util.*" %>



<body>
<%= new java.text.SimpleDateFormat("MM/dd/yyyy").format(new Date())%>
</body>
0
 
bloodredsunCommented:
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
0
 
bloodredsunCommented:
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.
0
 
koppchaCommented:
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) :)
0
 
bloodredsunCommented:
thank you koppcha, you're a gentleman :-)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
0
 
patbinCommented:
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"
0
 
koppchaCommented:
Grade B: The answer i provided is not the exact one  you are looking for ?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now