1

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>");
%>
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
KAKAK
  • 879
  • 7
  • 16
  • 32

3 Answers3

3

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?

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

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>");
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
0

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));
mel3kings
  • 8,857
  • 3
  • 60
  • 68