问题描述:

java里面时间类型转换成json数据就成这样了:
"createTime":{"date":30,"day":3,"hours":15,"minutes":14,"month":3,"nanos":0,"seconds"
:38,"time":1209539678000,"timezoneOffset":-480,"year":108}
期望的结果:

将date转化成yyyy-MM-dd形式

解决方案:

注册时间字段处理器,使用jsonconfig即可:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
JSONObject jsonObj = JSONObject.fromObject(map, jsonConfig);

class DateJsonValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd";

public DateJsonValueProcessor() {
}

public DateJsonValueProcessor(String format) {
this.format = format;
}

public Object processArrayValue(Object value, JsonConfig jsonConfig) {
String[] obj = {};
if (value instanceof Date[]) {
SimpleDateFormat sf = new SimpleDateFormat(format);
Date[] dates = (Date[]) value;
obj = new String[dates.length];
for (int i = 0; i < dates.length; i++) {
obj[i] = sf.format(dates[i]);
}
}
return obj;
}

public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if (value instanceof Date) {
String str = new SimpleDateFormat(format).format((Date) value);
return str;
}
return value.toString();
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

}

以上来源于网络

java时间类型转换 JsonValueProcessor的更多相关文章

  1. Java时间类型转换

    String转Long Long long=new SimpleDateFormat("yyyyMMddHHmmss").parse(String).getTime(); Long ...

  2. mysql跟java时间类型转换

    参照这个就行了,这个对应注入类型.===========java注入数据库==========java类型 mysql类型 成功与否date date yesdate time nodate time ...

  3. java时间格式转换

    package org.shineway.com; import java.text.ParseException; import java.text.SimpleDateFormat; import ...

  4. Java里面类型转换总结

    1.String 转 int int i = Integer.valueOf(my_str).intValue(); int i = Integer.parseInt(str); 2.String 转 ...

  5. 数据库时间类型和 util 包下时间类型转换

    Java 中的类型 1. java.sql 包下给出三个数据库相关的日期时间类型,分别是 java.sql.Date, 表示日期,只有年月日,没有时分秒. java.sql.Time, 表示时间, 只 ...

  6. JAVA强制类型转换(转载+自己的感想) - stemon

    JAVA强制类型转换(转载+自己的感想) - stemon 时间 2013-10-29 15:52:00  博客园-Java原文  http://www.cnblogs.com/stemon/p/33 ...

  7. Java String类型转换成Date日期类型

    插入数据库时,存入当前日期,需要格式转换 import java.text.SimpleDateFormat; formatter = new SimpleDateFormat( "yyyy ...

  8. java时间类简单总结

    java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个):  日期格式为:年月日时分秒(不包含毫秒部分) ...

  9. inner join ,left join ,right join 以及java时间转换

    1.inner join ,left join 与 right join (from 百度知道) 例表aaid adate1    a12    a23    a3表bbid  bdate1     ...

随机推荐

  1. hdu 4091 Zombie’s Treasure Chest 贪心+枚举

    转自:http://blog.csdn.net/a601025382s/article/details/12308193 题意: 输入背包体积n,绿宝石体积s1,价值v1,蓝宝石体积s2,价值v2,宝 ...

  2. Mysql 使用存储过程添加新字段

    -- 1, 注意SQL 语句开始处不要空格 -- 2, 在使用 [--] 进行注释时,后面请加空格 USE `test`; -- lastUpdateTime drop procedure if ex ...

  3. python--爬取http://www.kuaidaili.com/并保存为xls

    代码如下: 复制在python3上先试试吧^_^ # -*- coding: utf-8 -*- """ Created on Mon Jun 12 13:27:59 2 ...

  4. iOS推送的开启与关闭

    开启: 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationType ...

  5. CSV文件导出2

    public void exportCSVFile( HttpServletResponse response, ResultSet rs,String fileName,String headers ...

  6. go1.11新特性,mark一下

    包管理新特性: export GO111MODULE=on #开启modules go mod init # 创建go.mod (我是在项目根目录下输入的命令) ls // 可以看下创建成功 cat ...

  7. What should do in Production

    Using Compose in production https://docs.docker.com/compose/production/

  8. mmap和MappedByteBuffer

    1.MappedByteBuffer是DirectByteBuffer的子类 2.MappedByteBuffer使用的是mmap技术.MappedByteBuffer将文件映射为内存,也可能会被存储 ...

  9. pyserial安装

    参考网址:pyserial.sourceforge.net/pyserial.html#installation Download the archive from http://pypi.pytho ...

  10. mybatis注解@selectKey对于db2数据库的使用

    在新增时返回当前新增的主键. 数据库:DB2 用的是mybatis的@selectKey 代码: @InsertProvider(type = Test.class,method="inse ...