一.页面展示时间类型数据

1.引入头文件:<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

2.将从后台取出的时间类型数据按yyyy-MM-dd格式,格式化;

<td><fmt:formatDate value="${u.birthday }" pattern="yyyy-MM-dd"/> </td>

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">function deleteUser(userId,userName){if(window.confirm("您确定要删除用户["+userName+"]吗?")){location.href="${pageContext.request.contextPath }/user/"+userId+"/delete.action";}}
</script>
</head>
<body>
<h1>用户列表</h1>
<table border="1" cellpadding="0" cellspacing="0"><tr><th><input type="checkbox"/></th><th>编号</th><th>姓名</th><th>生日</th><th>性别</th><th>地址</th><th>操作</th></tr><c:forEach items="${users }" var="u" varStatus="status"><c:if test="${status.index%2==0 }"><tr style="background-color: #ccc"></c:if><c:if test="${status.index%2!=0 }"><tr></c:if><td><input type="checkbox" value="${u.id }" name="uid"/></td><td>${u.id }</td><td>${u.username }</td><td><fmt:formatDate value="${u.birthday }" pattern="yyyy-MM-dd"/> </td><td>${u.sex==1?'男':'女' }</td><td>${u.address }</td><td><a href="">查看</a>/<a href="${pageContext.request.contextPath }/editUser.action?id=${u.id }">修改</a>/<a onclick="deleteUser(${u.id},'${u.username} }')" href="javascript:void(0)">删除</a></td></tr></c:forEach>
</table>
</body>
</html>

二.后台时间类型字段接收前台传来的时间字符串

1.自定义时间字符串转换类DateConverter继承 Converter<String, Date>

注明:Converter这个类可以转换任何类型,用的最多的是时间转换;

package com.igeek.ssm.ex.converters;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;import org.springframework.core.convert.converter.Converter;/*** @author www.igeehome.com* * TODO** 2018年10月24日下午7:57:37*/
public class DateConverter implements Converter<String, Date> {@Overridepublic Date convert(String dateStr) {Date date = null;SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {date = dateFormat.parse(dateStr);} catch (ParseException e) {//e.printStackTrace();dateFormat = new SimpleDateFormat("yyyy-MM-dd");try {date = dateFormat.parse(dateStr);} catch (ParseException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}return date;}}

2.springMVC.xml中配置:

<!-- 配置注解驱动 --><!-- 如果配置此标签,可以不用配置... --><mvc:annotation-driven conversion-service="conversionService" /><!-- 配置类型转换器 --><bean id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><set><bean class="com.igeek.ssm.ex.converters.DateConverter" /></set></property></bean>

 3.页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户</title>
</head>
<body><form action="${pageContext.request.contextPath }/addUser.action" method="post"><p>用户名:<input type="text" name="username"/></p><p>生日:<input type="date" name="birthday"/></p><p>性别:<input type="radio" name="sex" value="1"/>男<input type="radio" name="sex" value="2"/>女</p><p>地址:<input type="text" name="address"/></p><p><input type="submit" value="添加"/></p></form>
</body>
</html>

4.后台接收:

 @RequestMapping("/addUser")public String addUser(User user) {userService.saveUser(user);// 无论是否成功,进入列表页面// 重定向进入url:userList.actionreturn "redirect:userList.action";}

5.实体类:

package com.igeek.ssm.ex.pojo;import java.util.Date;/*** @author www.igeehome.com* * TODO** 2018年10月29日下午7:48:30*/
public class User implements java.io.Serializable{private int id;private String username;private Date birthday;private int sex;private String address;/*** @return the id*/public int getId() {return id;}/*** @param id the id to set*/public void setId(int id) {this.id = id;}/*** @return the username*/public String getUsername() {return username;}/*** @param username the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the birthday*/public Date getBirthday() {return birthday;}/*** @param birthday the birthday to set*/public void setBirthday(Date birthday) {this.birthday = birthday;}/*** @return the sex*/public int getSex() {return sex;}/*** @param sex the sex to set*/public void setSex(int sex) {this.sex = sex;}/*** @return the address*/public String getAddress() {return address;}/*** @param address the address to set*/public void setAddress(String address) {this.address = address;}}

fmt标签实现时间日期格式化,与类型转换Converter相关推荐

  1. mysql java 日期格式化_(转)java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)...

    java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明);部分资料参考网络资源 1. java向MySQL插入当前时间的四种方式 第一种:将java.util.Date ...

  2. java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)...

    转载地址:http://www.devba.com/index.php/archives/4581.html java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明); ...

  3. java时间日期格式化和JDBC中的处理

    java时间日期格式化和JDBC中的处理 时间格式化类 SimpleDateFormat类 字符串格式化为时间对象 .parse()方法 时间对象格式化为字符串 .format()方法 DateTim ...

  4. 什么是千年虫?计算机如何开始处理日期?都有哪些时间日期格式化?

    目录 "千年虫"漏洞(Year 2000 Problem,简称"Y2K") 计算机是怎么开始处理日期的么? 举例1:时间格式化举例( 过滤器) 举例2:时间格式 ...

  5. uniapp将时间日期格式化的组件uni-dateformat的用法

    uniapp开发时,我们需要将数据库里取到的时间戳格式化为某个格式的日期时间形式,uniapp官方插件市场的uni-dateformat组件即可解决. uniapp官方插件地址及详细用法介绍:uni- ...

  6. 时间日期格式化的两种方式

    java中时间格式化的两种方式: 1.使用@JsonFormat注解进行时间日期的格式化 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",t ...

  7. vue 过滤器 格式时间秒数,js 时间日期格式化

    vue过滤器将总秒数转化为 00:00:00 Vue.filter('filterTime',function (value) {let t;if(value > -1){let hour = ...

  8. golang 时间原点 时间日期格式化

    问题: 问一个时间格式化问题: fmt.Println(time.Now().Format("2006year 01month 02day")) 2015year 12month ...

  9. mysql 日期格式化 yyyymmdd_mysql中时间日期格式化

    这里是一个使用日期函数的例子.下面的查询选择了所有记录,其date_col的值是在最后30天以内: mysql> SELECT something FROM table WHERE TO_DAY ...

最新文章

  1. R语言ggplot2可视化:在可视化图像中添加对角线(diagonal line)
  2. 《java编程思想》学习笔记——内部类五
  3. 定时器 线程池\进程池
  4. STM32学习笔记之__attribute__ ((at())绝对定位分析
  5. Blockchain区块链架构设计之四:Fabric多通道和下一代账本设计
  6. 考研学弟问的n个问题,梳理一下分享给大家
  7. C# Console.ReadLine()方法的使用 以及利用其返回值null终止输入
  8. java comparator_【面试题】Java必考面试题全集(15)
  9. c语言printf里的自增,笔试题记录:C语言——函数printf()的执行机制;先自增与后自增的区别;取值运算与自增运算的优先级...
  10. 如何bat清楚谷歌浏览器缓存_如何解决谷歌浏览器启动页面被篡改?
  11. 2021-2025年中国云企业管理软件行业市场供需与战略研究报告
  12. 【Silverlight】Bing Maps学习系列(二):通过Bing Maps Silverlight Control如何显示地图...
  13. git branch查看/删除分支
  14. qt web混合编程_Qt+VS混合编程教程
  15. C#个人邮箱发邮件给多个邮箱
  16. 搭建fastdfs服务,及单机redis服务,springboot实现h5与fastdfs之间的断点续传,大文件上传,秒传文件和批量上传
  17. 图解PKCS#1(合)
  18. 机械硬盘4k读写速度_极速鲨课堂43:机械硬盘秒变固态靠谱吗?
  19. 模4补码(也称为变形补码)详解
  20. C语言%lld、%llu无法正常输出比long long长整数小的数

热门文章

  1. Microsoft Virtual Lab Use Guide
  2. zabbix源码编译安装以及添加第一台host监控
  3. 现代软件工程 第3-6章 作业
  4. [译]git fetch
  5. mina 和 xsocket
  6. 算法nodehdu 2112 hdu today
  7. apache重写模块开启
  8. 25款.NET开发工具
  9. ASP.NET防止用户多次登录的方法
  10. 给图片添加水印效果图的函数(可以在图片上添加自己的版权和LOGO图片的水印) 【转载】...