ssm整合之六 时间日期装换

举报
tea_year 发表于 2021/12/30 00:14:28 2021/12/30
【摘要】 package com.util; import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Collection;import java.util.Date;import java.util.Map; /** * @Tit...

  
  1. package com.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Collection;
  5. import java.util.Date;
  6. import java.util.Map;
  7. /**
  8. * @Title: ObjectTools.java
  9. * @Package org.platform.tools
  10. * @Description: <p>用来帮助校验数据是否为空</p>
  11. * @author Sniper
  12. * @date 2015-12-11 上午11:58:19
  13. * @version V1.0
  14. **/
  15. public class ObjectTools
  16. {
  17. /*格式化字符串 yyyy-MM-dd */
  18. public static final String YYYY_MM_DD = "yyyy-MM-dd";
  19. /*格式化字符串 yyyy-MM-dd hh:mm:ss */
  20. public static final String YYYY_MM_DD_HH = "yyyy-MM-dd hh:mm:ss";
  21. /**
  22. * @Title: notNull
  23. * @Description: <p>校验Object类型数据否为空,
  24. * <br/>如果为空则返回false,反之返回true</p>
  25. * @param @param obj
  26. * @return boolean 返回类型
  27. * @throws
  28. **/
  29. public static boolean notNull(Object obj)
  30. {
  31. if(obj == null) return true;
  32. return true;
  33. }
  34. /**
  35. * @Title: notNull
  36. * @Description: <p>验证集合是否为null或为集合的长度是否为0<br>
  37. * 如果为null或集合为空,则返回false;反之返回true</p>
  38. * @param @param coll
  39. * @param @return 参数说明
  40. * @return boolean 返回类型
  41. * @throws
  42. **/
  43. public static boolean notNull(Collection<Object> coll)
  44. {
  45. if(coll == null) return false;
  46. if(coll.isEmpty()) return false;
  47. return true;
  48. }
  49. /**
  50. * @Title: notNull
  51. * @Description: <p>验证字符串是否为Null或为空;<br/>
  52. * 如果为null或为空则返回false;反之返回true</p>
  53. * @param @param str
  54. * @param @return 参数说明
  55. * @return boolean 返回类型
  56. * @throws
  57. **/
  58. public static boolean notNull(String str)
  59. {
  60. if(str == null)return false;
  61. if("".equals(str)) return false;
  62. return true;
  63. }
  64. /**
  65. * @Title: notNull
  66. * @Description: <p>检验数组是否为空,<br>
  67. * 如果为null或数组长度为0,返回false;反之返回true</p>
  68. * @param @param obj
  69. * @param @return 参数说明
  70. * @return boolean 返回类型
  71. * @throws
  72. **/
  73. public static boolean notNull(Object[] obj)
  74. {
  75. if(obj == null) return false;
  76. if(obj.length == 0) return false;
  77. return true;
  78. }
  79. /**
  80. * @Title: notNull
  81. * @Description: <p>校验map集合是否为Null或空,<br/>
  82. * 如果为Null或空,则返回false;反之返回true</p>
  83. * @param @param map
  84. * @param @return 参数说明
  85. * @return boolean 返回类型
  86. * @throws
  87. **/
  88. public static boolean notNull(Map<Object,Object> map)
  89. {
  90. if(map == null) return false;
  91. if(map.size() == 0) return false;
  92. return true;
  93. }
  94. /**
  95. * @Title: dateToString
  96. * @Description: <p>格式化日期类型,返回一个yyyy-MM-dd hh:mm:ss格式的字符串</p>
  97. * @param @param date
  98. * @param @return 参数说明
  99. * @return String 返回类型
  100. * @throws
  101. **/
  102. public static String dateToString(Date date)
  103. {
  104. return dateToString(date, YYYY_MM_DD_HH);
  105. }
  106. /**
  107. * @Title: dateToString
  108. * @Description: <p>格式化日期类型,返回一个按照参数格式化好的字符串</p>
  109. * @param @param date
  110. * @param @param formatString
  111. * @param @return 参数说明
  112. * @return String 返回类型
  113. * @throws
  114. **/
  115. public static String dateToString(Date date , String formatString)
  116. {
  117. if(notNull(date) && notNull(formatString))
  118. {
  119. SimpleDateFormat format = new SimpleDateFormat(formatString);
  120. return format.format(date);
  121. }
  122. return null;
  123. }
  124. /**
  125. * @Title: stringToDate
  126. * @Description: <p>将一个指定好格式的字符串转成一个日期类型</p>
  127. * @param @param str
  128. * @param @param fomatString
  129. * @param @return 参数说明
  130. * @return Date 返回类型
  131. * @throws
  132. **/
  133. public static Date stringToDate(String str , String fomatString)
  134. {
  135. if(notNull(str) && notNull(fomatString))
  136. {
  137. SimpleDateFormat format = new SimpleDateFormat(fomatString);
  138. Date d = null;
  139. try
  140. {
  141. d = format.parse(str);
  142. } catch (ParseException e)
  143. {
  144. System.out.println("格式化错误!");
  145. }
  146. return d;
  147. }
  148. return null;
  149. }
  150. /**
  151. * @Title: stringToDate
  152. * @Description: <p>返回一个yyyy-MM-dd hh:mm:ss格式的日期类型</p>
  153. * @param @param str
  154. * @param @return 参数说明
  155. * @return Date 返回类型
  156. * @throws
  157. **/
  158. public static Date stringToDate(String str)
  159. {
  160. return stringToDate(str, YYYY_MM_DD_HH);
  161. }
  162. }

 

 

 


  
  1. package com.util;
  2. import java.util.Date;
  3. import org.springframework.core.convert.converter.Converter;
  4. public class DateConverter implements Converter<String, Date>
  5. {
  6. public Date convert(String str)
  7. {
  8. return ObjectTools.stringToDate(str, ObjectTools.YYYY_MM_DD);
  9. }
  10. }

  
  1. <mvc:annotation-driven conversion-service="conversionService"/>
  2. <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  3. <property name="converters">
  4. <list>
  5. <bean class="com.util.DateConverter"></bean>
  6. </list>
  7. </property>
  8. </bean>

在配置文件,添加对时间日期类的处理.

在客户端取值,会显示CST等系统默认格式:

可以使用如下方式转换:

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

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

 

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/71241677

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。