本文介绍了具体DateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JSP中,我以以下格式获取日期 Wed Jun 03 2015 13:30:40 GMT + 0530(印度标准时间)

如何创建简单的日期格式,以便我可以将字符串转换为日期对象

SimpleDateFormat sdf =新的SimpleDateFormat(这里应该是什么类型);
Date requestStartDate = request.getParameter(startTime);


i尝试 new SimpleDateFormat(EEE,d MMM yyyy)但它不起作用
是格式化的一些方法,但不是这种情况。

解决方案

您可以使用带有toString()方法的简单Date对象来打印当前日期和时间如下:

 code><%
Date date = new Date();
out.print(< h2 align = \center\>+ date.toString() +< / h2>);
%>
/ pre>

SimpleDateFormat允许您从选择日期时间格式的任何用户定义的模式开始。

 <%
日期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>);
%>

该标签用于以各种方式格式化日期

 <%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%> 
<%@ taglib prefix =fmturi =http://java.sun.com/jsp/jstl/fmt%>

< html>
< head>
< title> JSTL fmt:dateNumber标签< / title>
< / head>
< body>
< h3>数字格式:< / h3>
< c:set var =nowvalue =<%= new java.util.Date()%> />

< p>格式化日期(1):< fmt:formatDate type =timevalue =$ {now}/>< / p>
< p>格式化日期(2):< fmt:formatDate type =datevalue =$ {now}/>< / p>
< p>格式化日期(3):< fmt:formatDate type =bothvalue =$ {now}/>< / p>
< p>格式化日期(4):< fmt:formatDate type =both
dateStyle =shorttimeStyle =shortvalue =$ {now}/>< ; / p>
< p>格式化日期(5):< fmt:formatDate type =both
dateStyle =mediumtimeStyle =mediumvalue =$ {now}/>< ; / p>
< p>格式化日期(6):< fmt:formatDate type =both
dateStyle =longtimeStyle =long
value =$ {now} />< / p>
< p>格式化日期(7):< fmt:formatDate pattern =yyyy-MM-ddvalue =$ {now}/>< / p>

< / body>
< / html>


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.

解决方案

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>

这篇关于具体DateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:49