Spring MVC JSON自定义类型转换(续)
来源:互联网 发布:java web前台框架 编辑:程序博客网 时间:2024/06/11 16:14
通过配置全局的日期转换来避免使用麻烦的注解。
首先用到了一个简单的日期工具类DateUtil.java
- /**
- * DateUtil类
- *
- * @author liuzh
- */
- public class DateUtil {
- public static final String Y_M_D = "yyyy-MM-dd";
- public static final String Y_M_D_HM = "yyyy-MM-dd HH:mm";
- public static final String Y_M_D_HMS = "yyyy-MM-dd HH:mm:ss";
- public static final String YMD = "yyyyMMdd";
- public static final String YMDHM = "yyyyMMddHHmm";
- public static final String YMDHMS = "yyyyMMddHHmmss";
- public static final String ymd = "yyyy/MM/dd";
- public static final String ymd_HM = "yyyy/MM/dd HH:mm";
- public static final String ymd_HMS = "yyyy/MM/dd HH:mm:ss";
- /**
- * 智能转换日期
- *
- * @param date
- * @return
- */
- public static String smartFormat(Date date) {
- String dateStr = null;
- if (date == null) {
- dateStr = "";
- } else {
- try {
- dateStr = formatDate(date, Y_M_D_HMS);
- //时分秒
- if (dateStr.endsWith(" 00:00:00")) {
- dateStr = dateStr.substring(0, 10);
- }
- //时分
- else if (dateStr.endsWith("00:00")) {
- dateStr = dateStr.substring(0, 16);
- }
- //秒
- else if (dateStr.endsWith(":00")) {
- dateStr = dateStr.substring(0, 16);
- }
- } catch (Exception ex) {
- throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex);
- }
- }
- return dateStr;
- }
- /**
- * 智能转换日期
- *
- * @param text
- * @return
- */
- public static Date smartFormat(String text) {
- Date date = null;
- try {
- if (text == null || text.length() == 0) {
- date = null;
- } else if (text.length() == 10) {
- date = formatStringToDate(text, Y_M_D);
- } else if (text.length() == 13) {
- date = new Date(Long.parseLong(text));
- } else if (text.length() == 16) {
- date = formatStringToDate(text, Y_M_D_HM);
- } else if (text.length() == 19) {
- date = formatStringToDate(text, Y_M_D_HMS);
- } else {
- throw new IllegalArgumentException("日期长度不符合要求!");
- }
- } catch (Exception e) {
- throw new IllegalArgumentException("日期转换失败!");
- }
- return date;
- }
- /**
- * 获取当前日期
- * @param format
- * @return
- * @throws Exception
- */
- public static String getNow(String format) throws Exception{
- return formatDate(new Date(), format);
- }
- /**
- * 格式化日期格式
- *
- * @param argDate
- * @param argFormat
- * @return 格式化后的日期字符串
- */
- public static String formatDate(Date argDate, String argFormat) throws Exception {
- if (argDate == null) {
- throw new Exception("参数[日期]不能为空!");
- }
- if (StringUtils.isEmpty(argFormat)) {
- argFormat = Y_M_D;
- }
- SimpleDateFormat sdfFrom = new SimpleDateFormat(argFormat);
- return sdfFrom.format(argDate).toString();
- }
- /**
- * 把字符串格式化成日期
- *
- * @param argDateStr
- * @param argFormat
- * @return
- */
- public static Date formatStringToDate(String argDateStr, String argFormat) throws Exception {
- if (argDateStr == null || argDateStr.trim().length() < 1) {
- throw new Exception("参数[日期]不能为空!");
- }
- String strFormat = argFormat;
- if (StringUtils.isEmpty(strFormat)) {
- strFormat = Y_M_D;
- if (argDateStr.length() > 16) {
- strFormat = Y_M_D_HMS;
- } else if (argDateStr.length() > 10) {
- strFormat = Y_M_D_HM;
- }
- }
- SimpleDateFormat sdfFormat = new SimpleDateFormat(strFormat);
- //严格模式
- sdfFormat.setLenient(false);
- try {
- return sdfFormat.parse(argDateStr);
- } catch (ParseException e) {
- throw new Exception(e);
- }
- }
- }
需要用到的是两个智能转换日期的方法。关于转换的格式和规则,请看这两个方法,如果不符合你需要的,可以自行修改。
然后继承SimpleDateFormat写一个智能转换日期的类SmartDateFormat
- /**
- * Description: 智能日期转换
- * Author: liuzh
- */
- public class SmartDateFormat extends SimpleDateFormat {
- @Override
- public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
- return new StringBuffer(DateUtil.smartFormat(date));
- }
- @Override
- public Date parse(String text) throws ParseException {
- return DateUtil.smartFormat(text);
- }
- }
最后在Spring MVC的xml中配置:
- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <property name="objectMapper">
- <bean class="org.codehaus.jackson.map.ObjectMapper">
- <property name="dateFormat">
- <!-- 智能日期转换 -->
- <bean class="packageName.SmartDateFormat"/>
- </property>
- </bean>
- </property>
- </bean>
至于“智能”转换,完全就是简单的if/else判断,可以查看最上面的代码。
0 0
- Spring MVC JSON自定义类型转换(续)
- Spring MVC JSON自定义类型转换(续)
- Spring MVC JSON自定义类型转换(续)
- Spring MVC JSON自定义类型转换
- Spring MVC JSON自定义类型转换
- Spring MVC JSON自定义类型转换
- spring mvc Json转换
- spring mvc 返回Json数据时日期类型转换问题
- Spring MVC JSON 实现JsonSerializer Date类型转换
- spring mvc 类型转换
- Spring MVC自定义类型转换器
- spring mvc中类型转换
- spring mvc的类型转换
- Spring mvc参数类型转换
- Spring MVC处理JSON数据(3)-自定义消息转换器
- spring mvc返回json 类型的 数据
- spring mvc 返回json类型设置
- spring MVC (7) 定义自定义类型转换器
- JNI开发最佳实践
- python模块之 paramiko
- Spring MVC JSON自定义类型转换
- 身份证简单校验js正则表达式
- log4j:WARN Please initialize the log4j system properly解决办法
- Spring MVC JSON自定义类型转换(续)
- LLC学习笔记2(BOF)
- 自定义BaseActivity
- css 未知尺寸元素水平垂直居中(只针对inline,inline-block,table-cells)
- 窗口最小化后,可在另一程序中点击程序图标,让程序显示出来,唯一程序运行
- bzoj2818GCD
- 补(第三天下午)
- input只能输入数字等
- Java基础