zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JSON 中的毫秒时间( LONG) ,转换成年月日

JSONJSON 时间 转换成 long 毫秒 年月日
2023-09-14 09:04:39 时间
//String json = "[{logId:null,userId:null,operationTime:1449812923000,pageId:null,operationId:null,ipAddress:null,functionPage:系统设置,operationName:系统用户管理,operationUser:admin}]"; JSONObject jsonObj = new JSONObject(); jsonObj.put("logId", "null"); jsonObj.put("logId","null"); jsonObj.put("userId","null"); jsonObj.put("operationTime",1449812923000L); jsonObj.put("pageId","null"); jsonObj.put("operationId","null"); jsonObj.put("ipAddress","null"); jsonObj.put("functionPage","系统设置"); jsonObj.put("operationName","系统用户管理"); jsonObj.put("operationUser","admin"); System.out.println("这是你的JSON串:"+jsonObj); //long time = 1449812923000L; Long time = jsonObj.getLong("operationTime"); System.out.println("这里输出的是截取的时间:"+time+",类型是:"+time.TYPE); Date date = new Date(time); GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String sb=format.format(gc.getTime()); System.out.println(sb); System.out.println(date.getTime());


JSON 反序列化 Long 变 Integer 或 Double 问题 工作中可能会遇到对 Map String,Object 进行 JSON 序列化,其中值中包含 Long 类型的数据,反序列化后强转 Long 时报类型转换异常的问题。 本文简单探讨下该问题,并给出解决方案,如果你想直接看建议,直接翻到第三部分即可。
java中得long能表示的范围比js中number大,也就意味着部分数值在js中存不下(变成不准确的值). 解决办法一: 使用ToStringSerializer的注解,让系统序列化 时,保留相关精度 @JsonSerialize(using=ToStringSerializer.class) private Long createdBy; 上述方法需要在每个对象都配上该注解,此方法过于繁锁。