70

From my JSP Page, I am getting Date in this format.

Fri May 13 2011 19:59:09 GMT 0530 (India Standard Time)

How can I convert this to the pattern yyyy-MM-dd HH:mm:ss?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 3
    Have you tried taking a look at the javadocs? Google 'java date format' and see what comes up. – mdrg May 28 '11 at 15:04
  • 1
    Don't you actually mean "In my JSP page" instead of "From my JSP page"? – BalusC May 28 '11 at 15:20
  • 3
    Any chance you want to switch your answer to the overwhelming community favorite? – Gray Sep 04 '14 at 13:59
  • Never have I seen so many negatively voted answers to a single question. At least it's obvious which is the correct answer. – Stewart Mar 19 '16 at 23:39

6 Answers6

273

In JSP, you'd normally like to use JSTL <fmt:formatDate> for this. You can of course also throw in a scriptlet with SimpleDateFormat, but scriptlets are strongly discouraged since 2003.

Assuming that ${bean.date} returns java.util.Date, here's how you can use it:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<fmt:formatDate value="${bean.date}" pattern="yyyy-MM-dd HH:mm:ss" />

If you're actually using a java.util.Calendar, then you can invoke its getTime() method to get a java.util.Date out of it that <fmt:formatDate> accepts:

<fmt:formatDate value="${bean.calendar.time}" pattern="yyyy-MM-dd HH:mm:ss" />

Or, if you're actually holding the date in a java.lang.String (this indicates a serious design mistake in the model; you should really fix your model to store dates as java.util.Date instead of as java.lang.String!), here's how you can convert from one date string format e.g. MM/dd/yyyy to another date string format e.g. yyyy-MM-dd with help of JSTL <fmt:parseDate>.

<fmt:parseDate pattern="MM/dd/yyyy" value="${bean.dateString}" var="parsedDate" />
<fmt:formatDate value="${parsedDate}" pattern="yyyy-MM-dd" />
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i like you to add how to store the ${bean.date} to return java.util.date – shareef May 14 '13 at 11:10
  • @shareef: Just go through a basic JSP/Servlet tutorial. Our servlets wiki page is a good starting point to find references http://stackoverflow.com/tags/servlets/info – BalusC May 14 '13 at 11:11
  • I thought this example was well written. Unfortunately it did not work for me even though in my case bean.date does return a java.util.Date! >> According to TLD or attribute directive in tag file, attribute [value] does not accept any expressions – user1445967 Mar 09 '20 at 20:00
  • 1
    @user1445967: Your JSTL installation is corrupted. Follow https://stackoverflow.com/a/4928309 for instructions. Most likely you have a `standard.jar` of JSTL 1.0 lingering around in runtime classpath. Get rid of it. – BalusC Mar 09 '20 at 22:08
11
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html dir="ltr" lang="en-US">
 <head>
 <meta charset="UTF-8" />
  <title>JSP with the current date</title>
  </head>
 <body>
 <%java.text.DateFormat df = new java.text.SimpleDateFormat("dd/MM/yyyy"); %>
<h1>Current Date: <%= df.format(new java.util.Date()) %> </h1>
</body>
</html>

Output: Current Date: 10/03/2010

phani_yelugula
  • 331
  • 1
  • 9
  • 14
0
Date td = new Date();
String b = new String("");
SimpleDateFormat format = new SimpleDateFormat("YYYY/MM/dd");
b = format.format(td);
out.println(b);           
pb2q
  • 58,613
  • 19
  • 146
  • 147
-3
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Locale"%>

<html>
<head>
<title>Date Format</title>
</head>
<body>
<%
String stringDate = "Fri May 13 2011 19:59:09 GMT 0530";
Date stringDate1 = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z", Locale.ENGLISH).parse(stringDate);
String stringDate2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(stringDate1);

out.println(stringDate2);
%>
</body>
</html>
Hiren Odedra
  • 193
  • 1
  • 3
  • Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Jun 05 '20 at 07:47
-3

The example above showing the import with ...sun.com/jsp/jstl/format is incorrect (meaning it didn't work for me).

Instead try the below -this import statement is valid

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>

  <body>
    <c-rt:set var="now" value="<%=new java.util.Date()%>" />

    <table border="1" cellpadding="0" cellspacing="0"
    style="border-collapse: collapse" bordercolor="#111111"
    width="63%" id="AutoNumber2">
      <tr>
        <td width="100%" colspan="2" bgcolor="#0000FF">
          <p align="center">
            <b>
              <font color="#FFFFFF" size="4">Formatting: 
              <fmt:formatDate value="${now}" type="both"
              timeStyle="long" dateStyle="long" />
              </font>
            </b>
          </p>
        </td>
      </tr>
Dave Hill
  • 81
  • 1
  • 2
  • 6
  • 3
    My answer is correct. Your problem is just caused by that you're using the 10 year old and deprecated JSTL prototype library instead of the more recent ones. The newer taglib URI with `/jsp` in the path was introduced with JSTL 1.1 around november 2003. We're in 2011 already. Figure how out of date you are... Keep yourself and your libraries up to date. – BalusC Jul 08 '11 at 15:19
-12

You can do that using the SimpleDateFormat class.

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dates=formatter.format(mydate);
//mydate is your date object
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Niroshan Abayakoon
  • 913
  • 1
  • 10
  • 22
  • 8
    I assume that the downvoting is for a) using scriptlets and b) for not even doing the carets around it / returning the formatted date and c) it is the accepted answer altough the fmt:formatDate answer is the better choice. At least thats why I am tempted to downvote it. – Manuel Manhart Mar 06 '14 at 16:25
  • 3
    In JSP is better to use tags instead. `<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>` `` – Pablo Gallego Falcón Jan 14 '15 at 19:27
  • 1
    Most voted answer is for convert Java Date object to display in the JSP. But the question asked when a Date object is returned to the back-end (probably a Java class) from a JSP how to convert it of format it to a more readable pattern. So I think this answer does give an answer to the question (It's obvious because this is the accepted one). – prime Jan 15 '15 at 16:52
  • @Prime: the format `Fri May 13 2011 19:59:09 GMT 0530 (India Standard Time)` is typical to `Date#toString()`, which means that he already has a `java.util.Date` object at hands which could impossibly come from a JSP file as HTTP request parameters are always `java.lang.String` (even then, converting `String` to `Date` would require knowlegde of `SimpleDateFormat`, which the OP apparently didn't have, otherwise he didn't ask this question at all). – BalusC Feb 02 '15 at 19:08