ChatGPT解决这个技术问题 Extra ChatGPT

Convert and format a Date in JSP

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?

Have you tried taking a look at the javadocs? Google 'java date format' and see what comes up.
Don't you actually mean "In my JSP page" instead of "From my JSP page"?
Any chance you want to switch your answer to the overwhelming community favorite?
Never have I seen so many negatively voted answers to a single question. At least it's obvious which is the correct answer.

C
Community

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" />

i like you to add how to store the ${bean.date} to return java.util.date
@shareef: Just go through a basic JSP/Servlet tutorial. Our servlets wiki page is a good starting point to find references stackoverflow.com/tags/servlets/info
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: Your JSTL installation is corrupted. Follow 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.
p
phani_yelugula
<%@ 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


The op want yyyy-MM-dd HH:mm:ss and not dd/MM/yyyy
p
pb2q
Date td = new Date();
String b = new String("");
SimpleDateFormat format = new SimpleDateFormat("YYYY/MM/dd");
b = format.format(td);
out.println(b);           

D
Dave Hill

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>

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.
H
Hiren Odedra
<%@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>

Please put your answer always in context instead of just pasting code. See here for more details.
P
Paul D. Waite

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

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.
In JSP is better to use tags instead. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <fmt:formatDate value="${bean.date}" pattern="yyyy-MM-dd HH:mm:ss" />
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: 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).