Reputation:
In JSP i am getting date in following formatWed Jun 03 2015 13:30:40 GMT+0530 (India Standard Time)"
How to create a simple date format so that i can convert a string into a date object
SimpleDateFormat sdf=new SimpleDateFormat("what should be type here");
Date requestStartDate= request.getParameter("startTime");
i tried new SimpleDateFormat("EEE, d MMM yyyy")
But it is not working.
Here are some of ways for formatting but not this case.
Upvotes: 1
Views: 206
Reputation: 1886
You can use a simple Date object with toString() method to print current date and time as follows:
<%
Date date = new Date();
out.print( "<h2 align=\"center\">" +date.toString()+"</h2>");
%>
SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
<%
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>
The tag is used to format dates in a variety of ways
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL fmt:dateNumber Tag</title>
</head>
<body>
<h3>Number Format:</h3>
<c:set var="now" value="<%=new java.util.Date()%>" />
<p>Formatted Date (1): <fmt:formatDate type="time" value="${now}" /></p>
<p>Formatted Date (2): <fmt:formatDate type="date" value="${now}" /></p>
<p>Formatted Date (3): <fmt:formatDate type="both" value="${now}" /></p>
<p>Formatted Date (4): <fmt:formatDate type="both"
dateStyle="short" timeStyle="short" value="${now}" /></p>
<p>Formatted Date (5): <fmt:formatDate type="both"
dateStyle="medium" timeStyle="medium" value="${now}" /></p>
<p>Formatted Date (6): <fmt:formatDate type="both"
dateStyle="long" timeStyle="long"
value="${now}" /></p>
<p>Formatted Date (7): <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" /></p>
</body>
</html>
Upvotes: 1