如何在scriptlet中将java.util.date转换为所需格式 [英] How to convert java.util.date to required format in scriptlet

查看:101
本文介绍了如何在scriptlet中将java.util.date转换为所需格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索后,我有一个传输对象返回给JSP.它有一个java.util.Date字段(例如private Date issueDate;)

I have a transfer object being returned to the JSP after a search. It is having a java.util.Date field (e.g. private Date issueDate;)

我正在使用usebean标签访问TO中的数据,并将日期显示为:

I am accessing the data in TO using usebean tag and displaying the date as:

<INPUT TYPE="text" readonly="readonly" NAME="issueDt" ID="issueDt" 
       SIZE="45" value="<%=mySearchTO.getIssueDt()%>">

但是,这是以日期格式打印日期,例如 MON JAN 31 00:08:00 IST 2011

However, this is printing the date in the format say for e.g. MON JAN 31 00:08:00 IST 2011

我希望日期简单地以MM/DD/YYYY格式打印,并且在时间也很重要的情况下,以MM/DD/YYYY HH:MM格式打印.

I want the date to be printed simply as MM/DD/YYYY and in the cases where time is also important, in the MM/DD/YYYY HH:MM format.

如何在JSP中实现这一目标?我不知道我是否需要Javascript函数或某些静态Java方法.

How to achieve this inside JSP? I don't know if I need to go for Javascript function or some static Java method.

请原谅scriptlet的用法.这是一个遗留应用程序,因此我不能使用EL. 请仅通过scriptlet提供解决方案.像这样的解决方案:

Please excuse the usage of scriptlet. It's a legacy application and so I can not use EL. Please provide solution through scriptlet only. So solutions like:

<fmt:formatDate value="${new Date(c.dateInIntegerValue)}" 
                pattern="dd.MM.yyyy hh:mm"/> 

在其他问题中可用,对我不起作用.

available in other questions, will not work for me.

以下代码是否有效?

<fmt:formatDate value="<%=mySearchTO.getIssueDt()%>" 
                pattern="dd.MM.yyyy hh:mm"/> 

如果是,如何在JSP中使用它?我的意思是标签和所有!

If yes, how to use it in the JSP? I mean label and all!

我也尽可能避免使用jquery和此类库.

Also as far as possible, I want to avoid usage of jquery and such libraries.

推荐答案

<%@ page import="java.text.SimpleDateFormat" %>    
<% SimpleDateFormat dateFormatWithTime = new SimpleDateFormat("MM/dd/yyyy hh:mm");%>
<INPUT TYPE="text" readonly="readonly" NAME="issueDt" ID="issueDt" SIZE="45" value="<%=dateFormatWithTime.format(mySearchTO.getIssueDt())%>">

理想情况下,您应该只使用 formatDate JSTL或将此代码分解为一个 自定义taglib.

Ideally you should just use formatDate from JSTL or factor out this code into a custom taglib.

这篇关于如何在scriptlet中将java.util.date转换为所需格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆