KAKAK
KAKAK

Reputation: 899

How can i get the name of day from a date in JSP

Hi i am trying to get the day name from a date, the date will come from another page in a format (DD,MM,YYYY) and then the code will get the name of the day from this date. I tried:

<%@ page import="java.io.*,java.util.*" %>
<%@ page import="javax.servlet.*,java.text.*" %>
<%
    this line >> Date date = new Date(request.getParameter("DATE"));
    SimpleDateFormat ft = new SimpleDateFormat ("E");
    out.print( "<h2 align=\"left\">" +ft.format(date) +"</h2>");
%>

Upvotes: 1

Views: 2246

Answers (3)

AllTooSir
AllTooSir

Reputation: 49402

I would definitely go for JSTL fmt here :

 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 .....
 <h2 align="left">
   <fmt:formatDate pattern="E" value="${param.DATE}" />
 </h2>

You are using scriptlets and also using out.println() in your JSP , it is a very bad practice.

Please read How to avoid Java Code in JSP-Files?

Upvotes: 3

Ankur Lathi
Ankur Lathi

Reputation: 7836

public Date(String s) is deprecated.

So you should do it like this:

SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy"); // Your Input Date Format
Date date = sdf.parse(request.getParameter("DATE"));

SimpleDateFormat ft =  new SimpleDateFormat ("EEEE");

out.print( "<h2 align=\"left\">" +ft.format(date) +"</h2>");

Upvotes: 1

mel3kings
mel3kings

Reputation: 9405

try this:

  String input_date="01/08/2012"; //replace with your value
  SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
  DateFormat dformat=new SimpleDateFormat("EEEE"); 
  String finalDay=dformat.format(format1.parse(input_date));

Upvotes: 0

Related Questions