spring mvc 在jsp页面如何使时间以格式yyyy-MM-dd HH:mm:ss显示,24小时制

来源:互联网 发布:淘宝实体店是什么意思 编辑:程序博客网 时间:2024/06/10 18:46

首先,javaBean为:

import java.util.Date;

public class OutcomeVo {private int oid;private int uid;private String ocName;private String ocType;private Date gotOCDate;private Date commitTime;private String fileUrl;public String getFileUrl() {return fileUrl;}public void setFileUrl(String fileUrl) {this.fileUrl = fileUrl;}public String getOcName() {return ocName;}public void setOcName(String ocName) {this.ocName = ocName;}public int getOid() {return oid;}public void setOid(int oid) {this.oid = oid;}public int getUid() {return uid;}public void setUid(int uid) {this.uid = uid;}public String getOcType() {return ocType;}public void setOcType(String ocType) {this.ocType = ocType;}public Date getGotOCDate() {return gotOCDate;}public void setGotOCDate(Date gotOCDate) {this.gotOCDate = gotOCDate;}public Date getCommitTime() {return commitTime;}public void setCommitTime(Date commitTime) {this.commitTime = commitTime;}}


 

其次,在后台从数据库中获取的时候是:

import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.jdbc.core.RowMapper;import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;import org.springframework.jdbc.core.namedparam.SqlParameterSource;import org.springframework.jdbc.support.GeneratedKeyHolder;import org.springframework.jdbc.support.KeyHolder;import org.springframework.stereotype.Repository;import cn.cnic.outcome.front.vo.OutcomeVo;import cn.cnic.outcome.util.BaseDAO;@Repository@SuppressWarnings("unchecked")public class OutcomeDao extends BaseDAO{private static final String TABLE = " outcome ";private static final String FIELDS = " uid,ocType,ocName,fileUrl,gotOCDate,commitTime ";private static final String SAVED_VALUES = " :uid,:ocType,:ocName,:fileUrl,:gotOCDate,:commitTime ";private static final String UPDATED_VALUES = " uid=:uid,ocType=:ocType,ocName=:ocName,fileUrl=:fileUrl,gotOCDate=:gotOCDate ";@SuppressWarnings("unchecked")public List<OutcomeVo> findByProperty(String propertyName, Object value) {String sql = "select distinct * from " + TABLE + " where "+ propertyName + "=:" + propertyName;Map paramMap = new HashMap();paramMap.put(propertyName, value);return (List<OutcomeVo>) getNamedParameterJdbcTemplate().query(sql,paramMap, rowMapper);}@SuppressWarnings("unchecked")public List<OutcomeVo> findAll() {String sql = "select * from " + TABLE + " order by oid";Map paramMap = new HashMap();return (List<OutcomeVo>) getNamedParameterJdbcTemplate().query(sql,paramMap, rowMapper);}public int save(OutcomeVo outcomeVo) {String sql = "insert into " + TABLE + " ( " + FIELDS + " ) values ( "+ SAVED_VALUES + " )";KeyHolder keyHolder = new GeneratedKeyHolder();SqlParameterSource ps = new BeanPropertySqlParameterSource(outcomeVo);getNamedParameterJdbcTemplate().update(sql, ps, keyHolder);return keyHolder.getKey().intValue();}public int update(OutcomeVo outcomeVo) {String sql = "update " + TABLE + " set " + UPDATED_VALUES+ " where oid=:oid";SqlParameterSource ps = new BeanPropertySqlParameterSource(outcomeVo);return getNamedParameterJdbcTemplate().update(sql, ps);}@SuppressWarnings("unchecked")private RowMapper rowMapper = new RowMapper() {public Object mapRow(ResultSet rs, int arg1) throws SQLException {OutcomeVo outcomeVo = new OutcomeVo();outcomeVo.setCommitTime(rs.getTimestamp("commitTime"));outcomeVo.setGotOCDate(rs.getDate("gotOCDate"));outcomeVo.setOcType(rs.getString("ocType"));outcomeVo.setOcName(rs.getString("ocName"));outcomeVo.setFileUrl(rs.getString("fileUrl"));outcomeVo.setOid(rs.getInt("oid"));outcomeVo.setUid(rs.getInt("uid"));return outcomeVo;}};}



最后,在页面上显示的时候是这样的:

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


以上仅为关键代码,只需看红色字体部分即可知晓,如何操作

原创粉丝点击