ssm日期格式转换

1      需求

前端传入字符串类型日期转化成java中的Date类型,存入数据库中;将数据库中的日期类型通过jstl标签在前端页面转换成字符串类型。

2      步骤

2.1    创建一个日期转换类实现Converter接口,将String类型的日期转换成java中的Date类型,如果数据库中的字段设置为date,则SimpleDateFormat中设置格式为(yyyy-MM-dd),如果为datetime,则SimpleDateFormat中设置格式为(yyyy-MM-dd HH:mm:ss)。

public class CustomDateConverter implements Converter<String, Date>{
@Override
public Date convert(String source) {
// 将日期串转换成日期格式(格式是yyyy-MM-dd)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
//返回上述格式的Date类型对象
return simpleDateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

2.2    将自定义的转换类在springmvc中配置,并且在mvc:annotation-driven添加conversion-service属性,值为conversionService。

<!-- 自定义参数绑定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<!-- 日期类型的转换 -->
<bean class="cn.haohan.ssm.converter.CustomDateConverter"></bean>
</property>
</bean> <mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>

2.3    Jsp页面顶部添加fmt标签

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

2.4    Jsp页面中显示日期的位置,将从数据库读取到的日期类型的数据转换成字符串类型的数据

<td><input type="text" name="createtime" value="<fmt:formatDate value="${items.createtime }" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>

或者:

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

ssm日期格式转换的更多相关文章

  1. 日期格式转换 java 2016-09-03T00:00:00.000+08:00

    /**  * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  (yyyy-MM-dd'T'HH:mm:ss.SSSZ) TO  yyyy-MM-dd HH:mm:ss  * @ ...

  2. Oracle日期格式转换,tochar(),todate()

    Oracle日期格式转换 本文主要介绍Oracle中的日期转换. 1. 日期转化为字符串 (以2016年10月20日为例) select to_char(sysdate,'yyyy-mm-dd hh2 ...

  3. SpringMVC与MyBatis整合之日期格式转换

    在上一篇博客<SpringMVC与MyBatis整合(一)——查询人员列表>中遗留了日期格式转换的问题,在这篇记录解决过程. 对于controller形参中pojo对象,如果属性中有日期类 ...

  4. Python 日期格式转换

    经常需要爬取网站上的时间信息,不同的网站又有不同的日期显示方式.而我需要将日期格式转化为一种特定的格式,所以为了简便和学习,记录下各种不同的日期格式转换. 日期格式化符号: %y :两位数的年份表示( ...

  5. 在SQL Server中 获取日期、日期格式转换

    --常用日期转换参数: PRINT CONVERT(varchar, getdate(), 120 ) 2016-07-20 16:09:01 PRINT replace(replace(replac ...

  6. ABAP - 日期格式转换 &amp; ABAP经常使用日期处理函数

    ABAP - 日期格式转换 如今提供下面一些日期格式转换的函数: Below are several FMs which can be used to convert date format. 1. ...

  7. freemark声明变量,boolean,date,date日期格式转换成String类型的(五)

    <br/>assign用来定义变量<#assign name="刘德华"><br/> 获取assign定义变量的值:${name} <br ...

  8. Nullable<System.DateTime>日期格式转换 (转载)

    一.问题 1.html页面中时间显示出错,数据库中时间是正确的. 原因:没有把DateTime转成String类型. 2.  在C#中,发现不能直接使用ToString("yyyy-MM-d ...

  9. java 获取当前日期和特殊日期格式转换

     1.获取当前日期: package com.infomorrow.dao; import java.sql.Timestamp; import java.util.Calendar; import ...

随机推荐

  1. python importlib动态导入模块

    一般而言,当我们需要某些功能的模块时(无论是内置模块或自定义功能的模块),可以通过import module 或者 from * import module的方式导入,这属于静态导入,很容易理解. 而 ...

  2. maxSubArray

    Description: Find the contiguous subarray within an array (containing at least one number) which has ...

  3. Key-Value Store Indexer(Lily HBase Indexer) 小型采坑

    环境: Cloudera Express 5.12.1 JDK 1.8.0_92 CentOS 7 步骤1:数据导入到Hbase中(非正题,跳过) hbase中表为allDoc,两个Family:fu ...

  4. AUTOSAR-关于配置文件的思考

    基于Can: 1. Can_Cfg.h contains compile time configurations. It should be included by Can.h which is sp ...

  5. 浅谈面试中的OOD面向对象设计问题

    转载自:http://baozitraining.org/blog/Object-oriented-design-question/ OO设计问题是电面或者onsite中常考的问题,尤其对以Java为 ...

  6. Java中浮点数的精度问题 【转】

    当您在计算Money的时候,请看好了!!!要不损失了别后悔!!! 现象1: public static void main(String[] args) { System.out.println(0. ...

  7. 【转】java中PriorityQueue优先级队列使用方法

    优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...

  8. Python学习 Part7:类

    Python学习 Part7:类 1. 作用域和命名空间 命名空间(namespace)就是一个从名称到对象的映射. 命名空间的一些实例:内置名称集(函数,像abs(),和内置异常名称),一个模块中的 ...

  9. cw2vec理论及其实现

    导读 本文对AAAI 2018(Association for the Advancement of Artificial Intelligence 2018)高分录用的一篇中文词向量论文(cw2ve ...

  10. 非常适用的Sourceinsight插件,提高效率【强力推荐】

    转自:http://www.cnblogs.com/heiyue/p/6225975.html 一直使用sourceinsight编辑C/C++代码,sourceinsight是一个非常好用的编辑工具 ...